今天做网站的是遇到个问题,特此记录一下
要实现的效果如下图:
下拉框控件的数目是不固定的,是根据某个ID从数据库中读取的,然后要显示在页面上面,并且数据要绑定上去。
主要卡住的地方是,如何将控件循环出来,以及将用户选择的内容保存到数据库里面。
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<span class="white1">
<%# Eval("Name") %></span><br />
<table width="270" border="0">
<tr>
<td width="169">
<%#BlindsOptions(Eval("ID"))%>
</td>
<td width="91">
<img src="../Images/wh.gif" width="14" height="14" alt ="" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
后台
#region 绑定Repeater控件
/// <summary>
/// 绑定Repeater控件
/// </summary>
/// <param name="whereStr"></param>
protected void BindRep(string whereStr)
{
whereStr = " ID IN(" + ViewState["BlindsOptionsIds"].ToString() + ")";
T_BlindsOptionsBLL obj = new T_BlindsOptionsBLL();
DataSet ds = obj.GetList(whereStr);
rep.DataSource = ds;
rep.DataBind();
}
#endregion
#region 循环显示DropDownList控件,并绑定相应的数据
protected string BlindsOptions(object ID)
{
string returnStr = "";
T_BlindsOptionsItemBLL itemBll = new T_BlindsOptionsItemBLL();
DataSet ds = itemBll.GetList("OptionID=" + Convert.ToInt32(ID));
int count = ds.Tables[0].Rows.Count;
returnStr += "<select name='select_" + ID.ToString() + "' id='select_" + ID.ToString() + "'>";
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
returnStr += "<option value='" + ds.Tables[0].Rows[i]["ID"] + "' >" + ds.Tables[0].Rows[i]["Name"] + "</option>";
}
}
returnStr += "</select>";
return returnStr;
}
#endregion
得到下拉框控件的内容,该方法GetBlindsOptions()得到的数据,保存到数据表中的某个字段里面。
protected string GetBlindsOptions(object objID)
{
string strItem="";
if (objID != null)
{
T_CurtainsInfoBLL bll = new T_CurtainsInfoBLL();
T_CurtainsInfo model = bll.GetModel(Convert.ToInt32(objID));
string[] boID=model.BlindsOptionsIds.Split(',');
for (int i = 0; i < boID.Length; i++)
{
strItem += Request["select_" + boID[i] + ""] + ",";
}
}
string returnStr = strItem.Substring(0, strItem.Length - 1);
return returnStr;
}