从动态文本框中获取价值

时间:2012-06-05 23:29:02

标签: c# asp.net textbox

我有一些数组来保存我的控件。当调用函数初始化数组并在数组中保存我的控件。

代码:

private Label[] lblName;
    private TextBox[] txtName;
    private Label[] lblSurname;
    private TextBox[] txtSurname;
    private Label[] lblInstitution;
    private TextBox[] txtInstitution;
    private Label[] lblCountry;
    private TextBox[] txtCountry;
    private Label[] lblEmail;
    private TextBox[] txtEmail;
    private PlaceHolder PlaceHolder1;


    public int NumberOfOtherAuthors()
    {
        Int32 index = Convert.ToInt32(NumberList.SelectedValue);
        return index;
    }

    public void GUIofOtherAuthor()
    {
        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];
        lblInstitution = new Label[authors];
        txtInstitution = new TextBox[authors];
        lblCountry = new Label[authors];
        txtCountry = new TextBox[authors];
        lblEmail = new Label[authors];
        txtEmail = 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();
            lblInstitution[i] = new Label();
            lblInstitution[i].Text = "Institution:";
            txtInstitution[i] = new TextBox();
            lblCountry[i] = new Label();
            lblCountry[i].Text = "Country:";
            txtCountry[i] = new TextBox();
            lblEmail[i] = new Label();
            lblEmail[i].Text = "Email:";
            txtEmail[i] = new TextBox();

            PlaceHolder1.Controls.Add(new LiteralControl("<table>"));
            PlaceHolder1.Controls.Add(new LiteralControl("<span style=\"font-weight:bold;\" "));
            PlaceHolder1.Controls.Add(authorInformation);
            PlaceHolder1.Controls.Add(new LiteralControl("</span>"));
            PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
            PlaceHolder1.Controls.Add(lblName[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
            PlaceHolder1.Controls.Add(txtName[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
            PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
            PlaceHolder1.Controls.Add(lblSurname[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
            PlaceHolder1.Controls.Add(txtSurname[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
            PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
            PlaceHolder1.Controls.Add(lblInstitution[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
            PlaceHolder1.Controls.Add(txtInstitution[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
            PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
            PlaceHolder1.Controls.Add(lblCountry[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
            PlaceHolder1.Controls.Add(txtCountry[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
            PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
            PlaceHolder1.Controls.Add(lblEmail[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
            PlaceHolder1.Controls.Add(txtEmail[i]);
            PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
            PlaceHolder1.Controls.Add(new LiteralControl("</table><br /> "));
            Panel1.Controls.Add(PlaceHolder1);

            j++;
        }
    }

现在我想在另一个函数public void UploadForm(){...}中获取文本框的值。 我试试了

int i;
int numberOfOtherAuthors = NumberOfOtherAuthors();

for(i=0; i<numberOfOtherAuthors; i++)
{
   String a = txtname[i].text
}

我想要上传到数据库的Textboxes值,但是假设我们将它们保存为字符串。 当我这样做时,我有NullReferenceException。 那么如何获得这个文本框的价值??? 感谢

1 个答案:

答案 0 :(得分:1)

我注意到您的UploadForm()函数引用了txtname,而您的实际TextBox被声明为txtName。由于字段名称区分大小写,因此实际上并未定义字段“txtname”(小写“n”),因此它可能是动态页面中NullReferenceException的原因。