动态添加的dropdownlist在回发时损失价值

时间:2018-12-05 12:17:31

标签: c# asp.net dropdown postback dynamic-controls

嗨,我正在动态创建下拉列表,但是在回发时它失去了值,并且我无法在按钮单击事件中获取值。

private void CreateDropdownListEmployee(string id,Panel pnl, DataTable dt)
{
DropDownList drp = new DropDownList();
drp.ID = id;
drp.CssClass = "activeuserTxt";
drp.ClientIDMode = ClientIDMode.Static;
drp.DataSource = dt;
drp.DataTextField = "name";
drp.DataValueField = "id";
drp.DataBind();
pnl.Controls.Add(drp);
}

对于动态texbox,我在PAge_Init上使用以下代码

protected void Page_Init(object sender, EventArgs e)
    {
        try
        {
            int SNID = 0;
            List<string> keysNetmr = Request.Form.AllKeys.Where(key => key.Contains("txt")).ToList();
            foreach (string key in keysNetmr)
            {
                this.CreateTextBox("txtNermr" + SNID, 100, pnlSN);
                SNID++;
            }
        }
        catch (Exception ex) { }
    }

private void CreateTextBox(string id, int width, Panel pnl)
    {
        TextBox txt = new TextBox();
        txt.ID = id;
        txt.Width = width;
        txt.Attributes.Add("autocomplete", "off");
        txt.CssClass = "activeuserTxt";
        pnl.Controls.Add(txt);
    }

有人可以帮我吗?

0 个答案:

没有答案