Repeater中绑定下拉菜单的2种方法

1.第一种方法,Repeater中绑定DropDownList实现。

<asp:DropDownList ID="ddlfixs" runat="server" appenddatabounditems="true">
      <asp:ListItem Value="">请选择治具</asp:ListItem></asp:DropDownList>
      <asp:HiddenField ID="hdfixid" runat="server" Value='<%# Eval("Fixid")%>' />
        protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DropDownList ddlfixs = e.Item.FindControl("ddlfixs") as DropDownList;
            //HtmlSelect ddltools = e.Item.FindControl("ddltools") as HtmlSelect;

            WebClass.Toolfix tmpp = new WebClass.Toolfix();
            ddlfixs.DataSource = tmpp.ListFixShow();
            ddlfixs.DataTextField = "Names";
            ddlfixs.DataValueField = "Id";
            ddlfixs.DataBind();

            ddlfixs.SelectedValue = ((HiddenField)e.Item.FindControl("hdfixid")).Value;
        }
取值:
            string strfixs = "";
            foreach (RepeaterItem rptItem in myRepeater.Items)
            {
                DropDownList ddl = rptItem.FindControl("ddlfixs") as DropDownList;
                strfixs += ddl.SelectedValue + ",";
            }
            strfixs = strfixs.Remove(strfixs.Length - 1);

这种方法很不好,会生成;<select name="myRepeater$ctl02$ddlfixs" id="myRepeater_ddlfixs_2">类似的代码,如果javascript控制添加多行操作,很难控制。

2.第二方法,从后台生成HTML代码。

<%# BinGxData(Eval("Gxid").ToString())%>
        public string BinGxData(string strgxid)
        {
            WebClass.TooList tmp = new WebClass.TooList();
            DataTable dt=tmp.ListToolTable();
            string strMsg = "<select name='ddltools' id='ddltools'>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string str1 = (strgxid == dt.Rows[i]["id"].ToString() ? "selected='selected'" : "");
                strMsg = strMsg + "<option value='" + dt.Rows[i]["id"].ToString() + "' " + str1 + ">" + dt.Rows[i]["names"].ToString() + "</option>";
            }
            strMsg = strMsg + "</select>";
            return strMsg; 
        }

取值好简单:Request.Form["ddltools"];即可。
此方法干净利索,尤其在添加多行操作中很好控制。

posted on 2011-07-04 16:30  ★金★  阅读(1059)  评论(0编辑  收藏  举报

导航