2013-12-18 40 views
0

我想从动态生成的文本框中获取值。 下面给出的是我的代码,但每次都得到空值。如何从动态生成的多个文本框在asp.net中获取多个值c#

的Default.aspx:

<asp:Panel ID="Panel1" runat="server" style="width:460px"> 
<asp:PlaceHolder id="Area1" runat="server"></asp:PlaceHolder> 
    </asp:Panel> 


<asp:Button ID="Button1" runat="server" Text="Button" 
    onclick="Button1_Click1" /> 
<asp:Button ID="Button2" runat="server" Text="save" onclick="Button2_Click" /> 

Default.aspx.cs

protected void Button1_Click1(object sender, EventArgs e) 
{ 
    CreateTextBox(); 

} 
public void CreateTextBox() 
{ 

    int rowCount = 0; 
    //initialize a session. 
    rowCount = Convert.ToInt32(Session["clicks"]); 

    rowCount++; 

    //In each button clic save the numbers into the session. 
    Session["clicks"] = rowCount; 


    //Create the textboxes and labels each time the button is clicked. 
    for (int i = 0; i < rowCount; i++) 
    { 

     TextBox TxtBoxU = new TextBox(); 

     TextBox TxtBoxE = new TextBox(); 

     Label lblU = new Label(); 
     Label lblE = new Label(); 

     TxtBoxU.ID = "TextBoxU" + i.ToString(); 
     TxtBoxE.ID = "TextBoxE" + i.ToString(); 

     lblU.ID = "LabelU" + i.ToString(); 
     lblE.ID = "LabelE" + i.ToString(); 


     lblU.Text = "Header " + (i + 1).ToString() + " : "; 
     lblE.Text = "Value " + (i + 1).ToString() + " : "; 

     //Add the labels and textboxes to the Panel. 
     Area1.Controls.Add(lblU); 
     Area1.Controls.Add(TxtBoxU); 

     Area1.Controls.Add(lblE); 
     Area1.Controls.Add(TxtBoxE); 
    } 
} 
protected void Button2_Click(object sender, EventArgs e) 
{ 
    int count=Convert.ToInt32(Session["clicks"]); 
    for(int j=0;j<count;j++) 
    { 

     TextBox aa = (TextBox)Area1.FindControl("TextBoxU"+j); 
     Response.Write(aa.Text); 

     TextBox bb = (TextBox)Area1.FindControl("TextBoxE" + j); 
     Response.Write(bb.Text); 

    } 
} 

的话,请给这个查询的妥善解决。 谢谢。

+0

您可以遍历Area1.Controls并从每个“(控制文本框)”中读取ID和文本值,您会发现... – Hinek

回答

1

我认为你必须在Page_Load事件中调用CreateTextBox();函数。

Button2_Click您的Button2_Click会导致页面重新加载,并且下次您的页面加载您的文本框并且标签不会被创建,并且当您尝试通过它们的Id访问它们时,您将获得空值。

您需要检查哪些事件引发了回帖,并且对于Button2_Click您需要拨打 CreateTextBox();函数。

的Page_Load

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (IsPostBack) 
     { 
      string eventName = getPostBackControlName();//Checks for the event that 
      //Caused postBack 
      if (eventName == "Button2") 
      CreateTextbox(); 

     } 
     } 

getPostbackControlName - Here

private string getPostBackControlName() 
     { 
      Control control = null; 
      //first we will check the "__EVENTTARGET" because if post back made by  the controls 
      //which used "_doPostBack" function also available in Request.Form collection. 
      string ctrlname = Page.Request.Params["__EVENTTARGET"]; 
      if (ctrlname != null && ctrlname != String.Empty) 
      { 
       control = Page.FindControl(ctrlname); 
      } 
      // if __EVENTTARGET is null, the control is a button type and we need to 
      // iterate over the form collection to find it 
      else 
      { 
       string ctrlStr = String.Empty; 
       Control c = null; 
       foreach (string ctl in Page.Request.Form) 
       { 
        //handle ImageButton they having an additional "quasi-property" in their Id which identifies 
        //mouse x and y coordinates 
        if (ctl.EndsWith(".x") || ctl.EndsWith(".y")) 
        { 
         ctrlStr = ctl.Substring(0, ctl.Length - 2); 
         c = Page.FindControl(ctrlStr); 
        } 
        else 
        { 
         c = Page.FindControl(ctl); 
        } 
        if (c is System.Web.UI.WebControls.Button || 
          c is System.Web.UI.WebControls.ImageButton) 
        { 
         control = c; 
         break; 
        } 
       } 
      } 
      return control.ID; 

     } 

protected void Button1_Click1(object sender, EventArgs e) 
{ 
    CreateTextBox(); 

} 

protected void Button2_Click(object sender, EventArgs e) 
{ 
    int count=Convert.ToInt32(Session["clicks"]); 
    for(int j=0;j<count;j++) 
    { 

     TextBox aa = (TextBox)Area1.FindControl("TextBoxU"+j); 
     Response.Write(aa.Text); 

     TextBox bb = (TextBox)Area1.FindControl("TextBoxE" + j); 
     Response.Write(bb.Text); 

    } 
} 
相关问题