• 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"];即可。
    此方法干净利索,尤其在添加多行操作中很好控制。

  • 相关阅读:
    CSS换行和省略号
    html+canvas实现很真实的下雨雨落
    CSS取消鼠标点击事件
    js某时间与当前时间差
    uniapp微信小程序canvas隐藏
    canvas生成圆图和微信小程序canvas圆图
    uniapp微信小程序canvas绘图插入网络图片不显示
    uniapp微信小程序分享(朋友圈分享)
    关于sure 等同根词的演化
    Stress and rhythm in English pronunciation
  • 原文地址:https://www.cnblogs.com/cnaspnet/p/2097534.html
Copyright © 2020-2023  润新知