我想从动态生成的文本框中获取价值。 下面给出的是我的代码,但每次获得null值。
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 :(得分:1)
我认为你还必须在CreateTextBox();
事件中调用Page_Load
函数。
您的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);
}
}