C# 如何从文本框获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10808314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get value from a TextBox
提问by Jimmysnn
I change my question because it probably was not understood. Also sorry for my English...
我改变了我的问题,因为它可能没有被理解。也对不起我的英语...
Dynamically create TextBoxes which put them in array.
动态创建将它们放入数组的 TextBox。
A piece of my code:
我的一段代码:
public partial class NewArticleForm : System.Web.UI.Page
{
private Label[] lblName;
private TextBox[] txtName;
private Label[] lblSurname;
private TextBox[] txtSurname;
private PlaceHolder PlaceHolder1;
public int NumberOfOtherAuthors()
{
Int32 index = Convert.ToInt32(NumberList.SelectedValue);
return index;
}
public void dataofOtherAuthor()
{
int authors;
int i = 0;
int j=1
authors = NumberOfOtherAuthors();
lblName = new Label[authors];
txtName = new TextBox[authors];
lblSurname = new Label[authors];
txtSurname = new TextBox[authors];
PlaceHolder1 = new PlaceHolder();
for (i = 0; i < authors; i++)
{
Label authorInformation = new Label();
authorInformation.Text = "Information for Author " + j.ToString() + " :";
lblName[i] = new Label();
lblName[i].Text = "Name:";
txtName[i] = new TextBox();
lblSurname[i] = new Label();
lblSurname[i].Text = "Surname:";
txtSurname[i] = new TextBox();
PlaceHolder1.Controls.Add(authorInformation);
PlaceHolder1.Controls.Add(lblName[i]);
PlaceHolder1.Controls.Add(txtName[i]);
PlaceHolder1.Controls.Add(lblSurname[i]);
PlaceHolder1.Controls.Add(txtSurname[i]);
// Add a spacer in the form of an HTML <BR> element.
Panel1.Controls.Add(PlaceHolder1);
j++; } }
protected void NumberList_SelectedIndexChanged(object sender, EventArgs e)
{
dataofOtherAuthor();
}
private void UploadForm()
{
int numberOfOtherAuthors = NumberOfOtherAuthors();
int i=0;
for (i = 0; i < numberOfOtherAuthors; i++)
{
Label1.Text = txtName[i].Text;
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
UploadForm();
}
}
How can I get the value of textboxes?? Specifically I want to pass the data in a database. Label is a test if I get the value.
如何获取文本框的值?具体来说,我想在数据库中传递数据。如果我得到值,标签是一个测试。
Thank you!!!
谢谢!!!
采纳答案by Nick Rolando
Not sure if you are aware or not, but you are placing PlaceHolder1in Panel1in every iteration of your for loop. Maybe you meant to do Panel1.Controls.Add(PlaceHolder1);after the for loop?
Anyway, you have placed these TextBox controls into the form. If you are trying to access them via postback, you need to either access them by their set ID, or access them through their asp containers (while setting runat="server"):
不知道你是否知道或没有,但你将PlaceHolder1在Panel1你的for循环的每个迭代。也许你打算Panel1.Controls.Add(PlaceHolder1);在 for 循环之后做?
无论如何,您已将这些 TextBox 控件放入表单中。如果您尝试通过回发访问它们,则需要通过它们的 setID访问它们,或者通过它们的 asp 容器(在 setting 时runat="server")访问它们:
x = (p1.Controls[placeHolderIndex].Controls[textBoxIndex] as TextBox).Text;
or you could try
或者你可以试试
x = (p1.FindControl("placeHolder_ID").FindControl("textBox_ID") as TextBox).Text;
again make sure they are all runat="server"
再次确保它们都是 runat="server"
txtNamewill not retain its value after a postback.
txtName回发后不会保留其值。
Have your second loop do something more useful than overwrite the set string as well; maybe add each value to a List
让你的第二个循环做一些比覆盖设置的字符串更有用的事情;可能将每个值添加到列表中
List<string> x = new List<string>();
for (i = 0; i < authors; i++)
{
x.Add((p1.Controls[placeHolderIndex].Controls[i] as TextBox).Text);
//increment placeHolderIndex if you don't change your design
}
回答by Alexei Levenkov
You need to add controls at correct time early enough (normally OnInit) for controls to correctly get values from the post back.
您需要在足够早的正确时间(通常是 OnInit)添加控件,以便控件从回发中正确获取值。
OnInit:
初始化:
Form1.Controls.Add(myTextbox);
Page_Load:
页面加载:
var text = myTextbox.Text;
Check out HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NETor many other explanations on "ASP.Net how to add dynamic controls" search criteria.
查看如何:使用 Visual C# .NET 在 ASP.NET 中动态创建控件或有关“ASP.Net 如何添加动态控件”搜索条件的许多其他说明。
回答by angus
To retrieve the controls in the placeholder, use Placeholder1.FindControl("[Name of Control]").
要检索占位符中的控件,请使用Placeholder1.FindControl("[Name of Control] ")。
You might want to name the controls to something unique and referenceable when you are creating them.
在创建控件时,您可能希望将控件命名为唯一且可引用的名称。
Alternatively you can reference them with Placeholder1.Controls([Index])if you know the index of the control.
或者,如果您知道控件的索引,则可以使用Placeholder1.Controls([Index]来引用它们)。
回答by Nick Rolando
quoting:
引用:
txtName = new TextBox[authors];
int authors = 5;
You declare authorsAFTER creating the array, thus your error showing that authorsdoesn't exist yet and later that txtName doesn't exist (as it wasn't instantiated properly). Change it to:
您在authors创建数组后声明,因此您的错误显示authors尚不存在,后来 txtName 不存在(因为它没有正确实例化)。将其更改为:
int authors = 5;
txtName = new TextBox[authors];
.
.
Also, for correctness, change Stringto string,
此外,为了正确性,更改String为string,
That should do it
那应该这样做
回答by Jimmysnn
I created objects using a different method from which I called txtName[i], that was my issue.
我使用我调用的不同方法创建了对象txtName[i],这是我的问题。
public void dataofOtherAuthor()
{
...
txtName[i] = new TextBox();
...}
private void UploadForm()
{
...
Label1.Text = txtName[i].Text;
...
}

