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는
SOprotected 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);
}
}
,이 쿼리의 적절한 해상도를 제공하십시오. 감사합니다.
Area1.Controls를 반복하고 ID (텍스트 상자)가 발견 된 ID와 텍스트 값을 읽을 수 있습니다. – Hinek