[英]C#, How to get HTML generated textbox values during foreach Control loop and then show the result?
Behind the code C#, when the user select 3(dropdownlist) then press execute button, it will auto generate 3 textboxes. 在代码C#后面,当用户选择3(下拉列表)然后按下执行按钮时,它将自动生成3个文本框。 After user fill out names on 3 textboxes then click request button, I want the 3 names that user entered display on different result textbox. 用户在3个文本框中填写名称后,单击“请求”按钮,我希望用户输入的3个名称显示在不同的结果文本框中。 How do I do that? 我怎么做?
Here are C# codes, 这是C#代码,
protected void ExecuteCode_Click(object sender, EventArgs e)
{
int amount = Convert.ToInt32(DropDownListIP.SelectedValue);
for (int num = 1; num <= amount; num++)
{
HtmlGenericControl div = new HtmlGenericControl("div");
TextBox t = new TextBox();
t.ID = "textBoxName" + num.ToString();
div.Controls.Add(t);
div1.Controls.Add(div);
}
ButtonRequest.Visible = true;
}
protected void ButtonRequest_Click(object sender, EventArgs e)
{
string str = "";
foreach (Control c in phDynamicTextBox.Controls)
{
try
{
TextBox t = (TextBox)c;
// gets textbox ID property
//Response.Write(t.ID);
str = t.Text;
}
catch
{
}
}
TextBoxFinal.Text = str;
}
Then HTML codes, 那么HTML代码,
<div id="div1" runat="server">
<asp:PlaceHolder ID="phDynamicTextBox" runat="server" />
</div>
One option is: when you create the textbox you save the Id in a list in session, then you through the list and use it: 一个选项是:当您创建文本框时,将Id保存在会话中的列表中,然后通过列表并使用它:
TextBox myTextbox = (TextBox)FindControl("name");
example: 例:
List<string> list = (List<string>)Session["myList"];
TextBox myTextbox;
foreach (string item in list)
{
myTextbox = (TextBox)FindControl(item);
//in myTextbox you have the atribute Text with the informatcion
}
Sorry for my english. 对不起我的英语不好。
you cannot access to control that create dynamically on postback, but you can try get input value from request like this 您无法访问在回发时动态创建的控件,但您可以尝试从请求获取输入值,如下所示
protected void ExecuteCode_Click(object sender, EventArgs e)
{
List<string> tbids = new List<string>();
int amount = Convert.ToInt32(DropDownListIP.SelectedValue);
for (int num = 1; num <= amount; num++)
{
HtmlGenericControl div = new HtmlGenericControl("div");
TextBox t = new TextBox();
t.ID = "textBoxName" + num.ToString();
div.Controls.Add(t);
phDynamicTextBox.Controls.Add(div);
tbids.Add(t.ID);
}
Session["tbids"] = tbids;
ButtonRequest.Visible = true;
}
protected void ButtonRequest_Click(object sender, EventArgs e)
{
string str = "";
var tbids = (List<string>)Session["tbids"];
foreach (var id in tbids)
{
try
{
str += Request[id]+" "; //here get value tb with id;
}
catch
{
}
}
TextBoxFinal.Text = str;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.