2

我改变了我的问题,因为它可能不被理解。也对不起我的英语...

动态创建将它们放入数组的文本框。

我的一段代码:

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();
    }
}

如何获得文本框的值?具体来说,我想在数据库中传递数据。如果我得到值,标签是一个测试。

谢谢!!!

4

5 回答 5

1

不确定您是否知道,但是您正在for 循环的每次迭代中进行PlaceHolder1Panel1也许你打算Panel1.Controls.Add(PlaceHolder1);在 for 循环之后做?
无论如何,您已将这些 TextBox 控件放入表单中。如果您尝试通过回发访问它们,则需要通过它们的 setID访问它们,或者通过它们的 asp 容器访问它们(同时设置runat="server"):

x = (p1.Controls[placeHolderIndex].Controls[textBoxIndex] as TextBox).Text;

或者你可以试试

x = (p1.FindControl("placeHolder_ID").FindControl("textBox_ID") as TextBox).Text;

再次确保他们都是runat="server"

txtName回发后不会保留其价值。

让你的第二个循环做一些比覆盖设置字符串更有用的事情;可能将每个值添加到列表中

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
}
于 2012-05-30T00:11:17.423 回答
1

引用:

txtName = new TextBox[authors];
int authors = 5;

authors在创建数组后声明,因此您的错误显示authors尚不存在,后来 txtName 不存在(因为它没有正确实例化)。将其更改为:

int authors = 5;
txtName = new TextBox[authors];

.

此外,为了正确起见,更改Stringstring,

应该这样做

于 2012-05-30T00:16:54.180 回答
0

您需要在正确的时间足够早地添加控件(通常是 OnInit),以便控件正确地从回发中获取值。

初始化:

Form1.Controls.Add(myTextbox);

页面加载:

var text = myTextbox.Text;

查看如何:使用 Visual C# .NET 在 ASP.NET 中动态创建控件或有关“ASP.Net 如何添加动态控件”搜索条件的许多其他说明。

于 2012-05-30T00:07:18.143 回答
0

要检索占位符中的控件,请使用Placeholder1.FindControl("[Name of Control] ")

在创建控件时,您可能希望将控件命名为独特且可引用的名称。

或者,如果您知道控件的索引,则可以使用Placeholder1.Controls([Index]引用它们。)

于 2012-05-30T00:10:10.267 回答
0

我使用不同的方法创建了对象txtName[i],这是我的问题。

public void dataofOtherAuthor() 
    { 
...
txtName[i] = new TextBox(); 
...}

private void UploadForm() 
    { 
...
Label1.Text = txtName[i].Text; 
...
}
于 2012-05-30T12:12:47.033 回答