如何设置' textbox.Text'用户控制页面中的占位符值?

时间:2014-12-30 10:13:28

标签: c# asp.net textbox placeholder

我在占位符中创建了动态文本框。我设置了textboxID,但我想在texbox创建时为每个文本框设置textbox.Text值。

用户根据需要选择ddlUserSelected下拉列表中创建文本框的数量。 例如: 用户在下拉列表中选择3我创建了3个文本框,但我想设置每个textbox.text值。

这是我的代码:

<asp:DropDownList ID="ddlUserSelected" AutoPostBack="true" OnSelectedIndexChanged="ddlUserSelected_SelectedIndexChanged" runat="server">
 <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
 <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>

代码背后:

protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
    {
     for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
            {

                TextBox txtDate = new TextBox();
                Label lbl = new Label();          
                lbl.Text = "<br/>";        
                txtDate .Width = 70;
                txtDate .CssClass = "tbl";
                txtDate .ID = "txtDate" + a;                  
                PlaceHolder1.Controls.Add(txtDate);            
                PlaceHolder1.Controls.Add(lbl);

                //*******HERE IS: I WANT TO SET EACH TEXTBOX VALUE*******

            }             
}

在保存按钮点击事件中获取文本框值:

protected void btnSave_Click(object sender, EventArgs e)
{

   for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
    {              

      string vvv = Request.Form["ctl00$ctl00$cphcontent$cphmain_main$UserControl$txtDate" + a];  
        //After I send a List each value 
    }
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您必须在将TextBox添加到占位符之前设置值:

...
txtDate.Text = "Value";          
PlaceHolder1.Controls.Add(txtDate);            
PlaceHolder1.Controls.Add(lbl);

获取价值:

for (int i = 0; i < PlaceHolder1.Controls.Count; i++)
{
    Control ctrl = PlaceHolder1.Controls[i];
    if (ctrl is TextBox)
    {
        TextBox txt = (TextBox)ctrl;
        string value = txt.Text;
    }
}