我在使用linkbutton添加的某个文本框中获取字符串时遇到了一些问题。此代码可以通过单击链接按钮添加文本框,并在面板上添加这些文本框,但我不知道如何获取用户在这些添加的文本框中写入的字符串,而且我也无法保留字符串当我点击链接按钮添加一个文本框时,它被写在文本框中。你能帮我吗?这是我的代码:
public partial class _Default : Page
{
Label myLabel1;
Label myLabel2;
protected void Page_Load(object sender, EventArgs e)
{
myLabel1 = new Label();
myLabel2 = new Label();
Panel1.Controls.Add(myLabel1);
Panel2.Controls.Add(myLabel2);
if (!Page.IsPostBack)
{
//Remove the session when first time page loads.
Session.Remove("clicks");
Session.Remove("clicks2");
}
}
private void BuildTextBoxes(int rowCount1, int rowCount2)
{
for (int i = 0; i < rowCount1; i++)
{
TextBox TxtBoxU = new TextBox();
TxtBoxU.ID = "TextBoxU" + i.ToString();
//Add the labels and textboxes to the Panel.
Panel1.Controls.Add(TxtBoxU);
}
myLabel1.Text = rowCount1 + "";
for (int i = 0; i < rowCount2; i++)
{
TextBox TxtBoxU = new TextBox();
TxtBoxU.ID = "TextBoxU" + i.ToString();
//Add the labels and textboxes to the Panel.
Panel2.Controls.Add(TxtBoxU);
}
myLabel2.Text = rowCount2 + "";
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
int rowCount1 = 0;
//initialize a session.
rowCount1 = Convert.ToInt32(Session["clicks"]);
rowCount1++;
//In each button clic save the numbers into the session.
Session["clicks"] = rowCount1;
BuildTextBoxes(rowCount1, Convert.ToInt32(Session["clicks2"]));
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
int rowCount2 = 0;
//initialize a session.
rowCount2 = Convert.ToInt32(Session["clicks2"]);
rowCount2++;
//In each button clic save the numbers into the session.
Session["clicks2"] = rowCount2;
BuildTextBoxes(Convert.ToInt32(Session["clicks"]), rowCount2);
}
}
非常感谢你。
答案 0 :(得分:3)
要阅读文本框,您需要在回发时执行以下操作:
var result = Request.Form["textboxId"]
答案 1 :(得分:3)
您需要做几件事。由于您是动态生成这些文本框,因此每次处理页面时都需要生成它们(甚至是回发)。它们还需要在加载视图状态之前生成。
每次加载页面时,您需要在面板中重新创建文本框,这应该在page_init函数中发生(需要在加载视图状态之前发生)。
有关ASP.Net页面生命周期的更多信息: ASP.NET Page Life Cycle Overview
以下代码是我认为您正在寻找的:
Label myLabel1;
Label myLabel2;
/// <summary>
/// Add the dynamic controls to the page before the viewstate is
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void page_init(object sender, EventArgs e)
{
myLabel1 = new Label();
myLabel2 = new Label();
Panel1.Controls.Add(myLabel1);
Panel2.Controls.Add(myLabel2);
var box1Count = 0;
box1Count = Convert.ToInt32(Session["clicks"]);
var box2Count = 0;
box2Count = Convert.ToInt32(Session["clicks2"]);
BuildTextBoxes(box1Count, box2Count);
}
/// <summary>
/// Ensure first time loads properly setup the page.
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Remove the session when first time page loads.
Session.Remove("clicks");
Session.Remove("clicks2");
//Set the Text Boxes and lables to zero.
BuildTextBoxes(0, 0);
}
}
/// <summary>
/// Add any new text boxes to the screen.
/// </summary>
/// <param name="rowCount1">The total number of text boxes in the first group.</param>
/// <param name="rowCount2">The total number of text boxes in the second group.</param>
private void BuildTextBoxes(int rowCount1, int rowCount2)
{
var panel1Count = Panel1.Controls.Count; //Current number of text boxes
panel1Count--; //Remove the Label control from the count
for (int i = panel1Count; i < rowCount1; i++)
{
TextBox TxtBoxU = new TextBox();
TxtBoxU.ID = "TextBox1U" + i.ToString(); //Ensure a globally unique name.
Panel1.Controls.Add(TxtBoxU); //Add the labels and textboxes to the Panel.
}
myLabel1.Text = rowCount1.ToString();
var panel2Count = Panel2.Controls.Count; //Current number of text boxes
panel2Count--; //Remove the Label control from the count
for (int i = panel2Count; i < rowCount2; i++)
{
TextBox TxtBoxU = new TextBox();
TxtBoxU.ID = "TextBox2U" + i.ToString(); //Ensure a globally unique name;
Panel2.Controls.Add(TxtBoxU); //Add the labels and textboxes to the Panel.
}
myLabel2.Text = rowCount2 + "";
}
/// <summary>
/// Add another textbox to the first group.
/// </summary>
protected void LinkButton1_Click(object sender, EventArgs e)
{
int rowCount1 = 0;
//initialize a session.
rowCount1 = Convert.ToInt32(Session["clicks"]);
rowCount1++;
//In each button click save the numbers into the session.
Session["clicks"] = rowCount1;
BuildTextBoxes(rowCount1, Convert.ToInt32(Session["clicks2"]));
}
/// <summary>
/// Add another textbox to the second group.
/// </summary>
protected void LinkButton2_Click(object sender, EventArgs e)
{
int rowCount2 = 0;
//initialize a session.
rowCount2 = Convert.ToInt32(Session["clicks2"]);
rowCount2++;
//In each button clic save the numbers into the session.
Session["clicks2"] = rowCount2;
BuildTextBoxes(Convert.ToInt32(Session["clicks"]), rowCount2);
}
答案 2 :(得分:0)
+1用于解释所有这些都需要在加载viewstate之前发生。如果有人还没有阅读关于Infinities Loop的动态控件和Viewstate文章,我强烈建议他们这样做: -
两者都非常出色,并将细节解释到最后的细节。