2013-12-18 2 views
0

동적으로 생성 된 텍스트 상자에서 값을 가져오고 싶습니다. 내 코드는 다음과 같습니다.하지만 항상 null 값을 얻습니다.동적에서 여러 값을 얻는 방법 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는

SO
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 (텍스트 상자)가 발견 된 ID와 텍스트 값을 읽을 수 있습니다. – Hinek

답변

1

CreateTextBox();에 전화해야한다고 생각합니다. Page_Load 이벤트도 있습니다.

Button2_ClickButton2_Click 페이지가 새로 고침되고 다음 번에 페이지에 텍스트 상자가로드되고 레이블이 만들어지지 않을 때 해당 ID로 액세스하려고하면 null 값이 반환됩니다.

어떤 이벤트가 게시물을 제기했는지 확인해야하며 Button2_ClickCreateTextBox();으로 전화해야합니다.

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(); 

} 

Here -

getPostbackControlName

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

     } 
     } 

를 Page_Load에서

관련 문제