如何获取动态创建的文本框值 [英] How to get dynamically created textbox value

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

问题描述



我想在asp.net中动态创建文本框值



这是我在此创建的代码代码文本框已创建但当我将在文本中输入值并从动态创建的文本框中检索值时,它会给出错误

对象引用未设置为对象的实例





以下是我的代码...



Hi,
I want to get dynamically created textbox value in asp.net

This is code which I have created in this code text box are created but when I will input the value in the text and retrieve the value from dynamically created text box it give error
Object reference not set to an instance of an object


Below is my code...

protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Generate initial textboxes 
            GenerateButton_Click(sender, e);
        }
    }
    protected void GenerateButton_Click(object sender, EventArgs e)
    {
        //Generate textboxes 
        int z = int.Parse(HowManyToGenerateTextBox.Text);

        Panel1.Controls.Clear();

        for (int i = 0; i < z; i++)
        {
            TextBox s = new TextBox();
            s.ID = "tb" + i.ToString();
            Session[s.ID] = s;
            Panel1.Controls.Add(s);
        }
    }
    protected void CalculateButton_Click(object sender, EventArgs e)
    {
        //Calculate sum 
        int sum = 0;

        for (int i = 0; i < Session.Count; i++)
        {
            TextBox txt = (TextBox)Panel1.FindControl("tb" + i.ToString());
            ResultLabel.Text = txt.Text;

           
            if (ResultLabel.Text != null)
                sum += int.Parse(ResultLabel.Text);
        }

        ResultLabel.Text = sum.ToString();
    }

推荐答案

试试这个

try this
protected void Page_Load(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        GenerateButton_Click(sender, e);
    }
    public TextBox[] s;
    protected void GenerateButton_Click(object sender, EventArgs e)
    {
        int z = Convert.ToInt32(TextBox1.Text);
        Panel1.Controls.Clear();
        Array.Resize(ref s, z);
        for (int i = 0; i < z; i++)
        {
            s[i] = new TextBox();
            s[i].ID = "tb" + i.ToString();
            s[i].Text = "tb" + i.ToString();
            Session[s[i].ID] = s;
            Panel1.Controls.Add(s[i]);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
                Response.Write(s[0].Text);
    }


试试这个,



TextBox txt =(TextBox)Panel1。 FindControl(tb+ i);







可能是这个链接可以帮助你:





http://stackoverflow.com/questions/12560074/handle-dynamic-control-in-asp-net-findcontrol [ ^ ]


在你的代码中,当没有回发时写入GenerateTextBox。当您点击计算按钮时,您的整个页面将会回发,您的动态控件将会丢失,因此您需要保留动态控件。



请查看以下内容链接,它肯定会解决你的问题。



保留ASP.NET应用程序中动态创建的控件的状态 [ ^ ]
In your code, it is written that GenerateTextBox when there is no post back. When you will click on Calculate button so your complete page will be postback and your dynamic controls will be lost, So you need to retain the dynamic controls.

Check out below link, It will definitely solve your problem.

Retaining State for Dynamically Created Controls in ASP.NET applications[^]


这篇关于如何获取动态创建的文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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