简体   繁体   English

asp.net自动在webform上生成文本框ID

[英]asp.net auto generated textboxes ID on webform

I am using C# on Visual Studio . 我在Visual Studio上使用C# I want to generate a webform with auto numbered Textboxes depending on the input from the previous page. 我想生成一个带有自动编号Textboxeswebform ,具体取决于上一页的输入。 What is the best way to do this loop? 这个循环的最佳方法是什么?

For example: 例如:

Input is 4 输入是4

The next page should generate textboxes with ID of 下一页应生成ID为的文本框

  • "name1" “NAME1”
  • "name2" “NAME2”
  • "name3" “NAME3”
  • "name4" “NAME4”

Something like this : 像这样的东西:

<asp:TextBox ID="name1" runat="server"></asp:TextBox>
<asp:TextBox ID="name2" runat="server"></asp:TextBox>
<asp:TextBox ID="name3" runat="server"></asp:TextBox>
<asp:TextBox ID="name4" runat="server"></asp:TextBox>

Part 2 of my question is that if I want to call them when a Button is click, how should I use a loop the get those ID? 我的问题的第2部分是,如果我想在单击Button时调用它们,我应该如何使用循环来获取这些ID?

Use for loop and PlaceHolder control to create dynamic TextBox controls 使用for循环和PlaceHolder控件来创建动态TextBox控件

<asp:PlaceHolder ID="phDynamicTextBox" runat="server" />

int inputFromPreviousPost = 4;
for(int i = 1; i <= inputFromPreviousPost; i++)
{
    TextBox t = new TextBox();
    t.ID = "name" + i.ToString();
}

//on button click retrieve controls inside placeholder control
protected void Button_Click(object sender, EventArgs e)
{
   foreach(Control c in phDynamicTextBox.Controls)
   {
       try
       {
           TextBox t = (TextBox)c;

           // gets textbox ID property
           Response.Write(t.ID);
       }
       catch
       {

       }
   }
}

You could create those controls in the Page Init event handler by say loop for the number of times the control needs to made available. 您可以通过say循环在Page Init事件处理程序中创建控件需要提供的次数。

Please remember since these are dynamic controls they need to be recreated during postbacks and will not be done automatically. 请记住,因为这些是动态控件,所以需要在回发期间重新创建它们,并且不会自动完成。

Further Dynamic controls and postback 进一步动态控制和回发

Check this code. 检查此代码。 In first page... 在第一页......

protected void Button1_Click(object sender, EventArgs e)
  {          
    Response.Redirect("Default.aspx?Name=" + TextBox1.Text);
  }

In second page you can get the value from querystring and create controls dynamically 在第二页中,您可以从querystring获取值并动态创建控件

protected void Page_Load(object sender, EventArgs e)
   {
      if (Request.QueryString["Name"] != null)
          Response.Write(Request.QueryString["Name"]);

            Int32 howmany = Int32.Parse(Request.QueryString["Name"]);

            for (int i = 1; i < howmany + 1; i++)
            {
                TextBox tb = new TextBox();
                tb.ID = "name" + i;
                form1.Controls.Add(tb);
            }
    }
for ( int i=0; i<4; i++ )
{
   TextBox t = new TextBox();
   t.ID = "name" + i.ToString();

   this.Controls.Add( t );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM