如何动态创建使用ASP.NET文本框,然后保存在数据库中的价值? [英] How to Dynamically create textboxes using ASP.NET and then save their values in Database?

查看:121
本文介绍了如何动态创建使用ASP.NET文本框,然后保存在数据库中的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个调查网站。我要动态地添加文本框,然后让它们的值在数据库中。

I am creating a survey site. I want to add the textboxes dynamically and then get their values in the database.

现在让我们说我从下拉4文本框来动态地在那里。

Now let's say I select 4 textboxes from the dropdown to be there dynamically.

上下拉列表中选择的 code:

     protected void NumDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "TextBox")
        {

            int j;
            i = int.Parse(NumDropDown.SelectedValue);
            Session["i"] = i;
            switch (i)
            {
                case 1:
                    t = new TextBox[i];
                    Session["textBox"] = t;
                    for (j = 0; j < i; j++)
                    {
                        t[j] = new TextBox();
                        t[j].ID = "txtCheckbox" + j.ToString();
                        Panel1.Controls.Add(t[j]);

                    }
                    break;
                case 2:
                    t = new TextBox[i];
                    Session["textBox"] = t;
                    for (j = 0; j < i; j++)
                    {
                        t[j] = new TextBox();
                        t[j].ID = "txtCheckbox" + j.ToString();
                        Panel1.Controls.Add(t[j]);

                    }
                    break;

                case 3:
                    t = new TextBox[i];
                    Session["textBox"] = t;
                    for (j = 0; j < i; j++)
                    {
                        t[j] = new TextBox();
                        t[j].ID = "txtCheckbox" + j.ToString();
                        Panel1.Controls.Add(t[j]);

                    }
                    break;
                case 4:
                    t = new TextBox[i];
                    List<TextBox> MyTextBoxes;
                    for (j = 0; j < i; j++)
                    {
                        t[j] = new TextBox();
                        t[j].ID = "txtCheckbox" + j.ToString();
                        Panel1.Controls.Add(t[j]);
                        try
                        {
                            MyTextBoxes = (List<TextBox>)Session["AddedTextBox"];
                            MyTextBoxes.Add(t[j]);
                            Session["AddedTextBox"] = MyTextBoxes;
                        }
                        catch
                        {
                            MyTextBoxes = new List<TextBox>();
                            MyTextBoxes.Add(t[j]);
                            Session["AddedTextBox"] = MyTextBoxes;
                        }
                    }
                    break;
            }

        }
    }

2),然后在这里我像A,B,C,D的文本框中输入的值,然后单击地址:

2) Then Here I entered the values in the textBox like a, b,c,d and click ADD:

code为单击Add点击:

1)首先我检查是那里的会议Page_Init:

1) First i checked the session to be there on Page_Init :

    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["AddedTextBox"] != null)
        {
            string a;
            string b;
            string c;
            string d;

            int listCount = ((List<TextBox>)Session["AddedTextBox"]).Count;
            foreach (TextBox t in ((List<TextBox>)Session["AddedTextBox"]))
            {
                if (listCount == 1)
                {

                }
                if (listCount == 2)
                {

                }
                if (listCount == 3)
                {

                }
                if (listCount == 4)
                {
                    if (t.ID == "txtCheckbox0")
                    {
                        a = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        b = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        c = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        d = t.Text;
                    }

                }
            }
        }

但这里的问题是,我不明白的文本值,他们似乎是空的。
请帮我解决这个问题。

But the problem here is that I don't get the text values, they Appear to be empty. Please help me to solve this issue.

推荐答案

这听起来像古老的经典问题,asp.net动态地将控件添加到页面。

This sounds like the age-old classic problem with asp.net adding controls dynamically to a page.

问题是与视图状态和控件上重建的回发。

The problem is to with viewstate and reconstruction of the controls on postback.

您需要运行在页面生命周期中产生的回发的在正确的时间将控制在同一code ,以确保回传值匹配的服务器端控件

You need to run the same code that generated the controls on postback at the correct time in the page lifecycle to ensure postback values match server-side controls.

当然,如果你想有一个哈克快捷方式,直接访问的Request.Form [textCheckbox+指数]

Of course if you want a hacky shortcut, access the Request.Form["textCheckbox" + index] values directly

关于这个问题的有用的文章。

这篇关于如何动态创建使用ASP.NET文本框,然后保存在数据库中的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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