如何获取从动态创建的文本框输入的值? [英] How do I get values that entered from dynamically created textbox?
问题描述
我创建了一个函数,用于从下拉列表中获取用户的值,并根据我应创建的文本框的数字。
我通过在aspx页面中创建一个面板,在for循环中创建了动态文本框。
现在我如何获得在文本框中输入的值?
我尝试过以下代码,但这不起作用..
//代码到创建动态文本框
public void users(int cnt)
{
for(int i = 1; i< = cnt; i ++)
{
TextBox txtuserAlias = new TextBox();
txtuserAlias.Text =;
txtuserAlias.ID =txtUserAlias+ i;
pnldisplayform.Controls.Add(txtuserAlias);
}
}
//调用文本框中输入值的代码
public void adduser ()
{
int cnt1 = Int32.Parse(DropDownNoOfUsers.SelectedValue.ToString());
for(int i = 1; i< = cnt1; i ++)
{
TextBox txtuserAlias = new TextBox();
txtuserAlias.ID =txtUserAlias+ i ;
some method = txtuserAlias.Text;
}
}
你能帮忙吗?
在此先感谢。
I have created a function to get the value from user in a drop down and based on the number I should create few text boxes.
I have did created dynamic textboxes in a for loop by creating a panel in aspx page.
Now how can i get the values that entered in the text boxes?
I have tried the below code but that doesn't works..
// code to create dynamic text boxes
public void users(int cnt)
{
for (int i = 1; i <= cnt; i++)
{
TextBox txtuserAlias = new TextBox();
txtuserAlias.Text = "";
txtuserAlias.ID = "txtUserAlias" + i;
pnldisplayform.Controls.Add(txtuserAlias);
}
}
//code to call the value that entered in the text box
public void adduser()
{
int cnt1 = Int32.Parse(DropDownNoOfUsers.SelectedValue.ToString());
for (int i = 1; i <= cnt1; i++)
{
TextBox txtuserAlias = new TextBox();
txtuserAlias.ID = "txtUserAlias" + i;
some method = txtuserAlias.Text;
}
}
Could you please help with the same.
Thanks in advance.
推荐答案
您好,
如果您将TextBox添加到pnldisplayform,那么您应该能够使用pnldisplayform再次找到它们。 FindControl [ ^ ]方法。
这样的事情可以让你入门。
Hi,
If you adding your TextBoxes to pnldisplayform, then you should be able to find them again using the pnldisplayform.FindControl[^] method.
Something like this should get you started.
for (int i = 1; i <= cnt1; i++)
{
//Find TextBox in Panel Container ...
TextBox txtuserAlias = (TextBox)pnldisplayform.FindControl("txtUserAlias" + i);
//Check we have found a TextBox ...
if (txtuserAlias != null)
{
someMethod(txtuserAlias.Text);
}
//Clear control reference for next iteration ...
txtuserAlias=null;
}
...希望它有所帮助。
... hope it helps.
尝试这段代码可能会对你有所帮助:
查看:
try this code may be it will help u :
View:
<asp:DropDownList ID="DropDownNoOfUsers" runat="server" AutoPostBack="True">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Panel ID="pnldisplayform" runat="server">
</asp:Panel>
<asp:Button runat="server" ID="Btn_Value" OnClick="Btn_Value_Click" Text="GetValue"/>
<br />
<asp:ListBox ID="ListUser" runat="server" Width="200"></asp:ListBox>
代码背后:
Code Behind :
private List<string> _users;
protected void Page_Load(object sender, EventArgs e)
{
var cnt = Int32.Parse(DropDownNoOfUsers.SelectedValue);
Users(cnt);
}
public void Users(int cnt)
{
for (var i = 1; i <= cnt; i++)
{
var txtuserAlias = new TextBox { Text = "", ID = "txtUserAlias" + i };
txtuserAlias.Attributes.Add("runat", "Server");
txtuserAlias.EnableViewState = true;
pnldisplayform.Controls.Add(txtuserAlias);
}
}
public List<string> GetUsers()
{
return pnldisplayform.Controls.OfType<TextBox>().Select(box => box.Text).ToList();
}
protected void Btn_Value_Click(object sender, EventArgs e)
{
_users = GetUsers();
foreach (var user in _users)
{
ListUser.Items.Add(user);
}
}
这篇关于如何获取从动态创建的文本框输入的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!