如何从一个TextBox获得价值 [英] How to get value from a TextBox

查看:154
本文介绍了如何从一个TextBox获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I change my question because it probably was not understood. Also sorry for my English...

动态创建一个摆阵文本框。

Dynamically create TextBoxes which put them in array.

我的一块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.

感谢您!

推荐答案

不知道你是否知道或没有,但你把 PLACEHOLDER1 面板1 在for循环的每次迭代。也许你的意思做 Panel1.Controls.Add(PLACEHOLDER1); 后循环结果?
无论如何,你已经把这些TextBox控件到窗体。如果您正试图通过回传访问它们,你需要自己设定为访问它们 ID ,或通过其ASP容器访问它们(在设置 =服务器

Not sure if you are aware or not, but you are placing PlaceHolder1 in Panel1 in 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"):

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

或者你可以尝试

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

再次确保它们都是 =服务器

txtName的回发后,将不保留其值。

txtName will not retain its value after a postback.

让你的第二个循环做的东西比覆盖组字符串,以及更多有用的;也许每个值添加到列表

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
}

这篇关于如何从一个TextBox获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆