上一个标签值不正确,没有数据传递给其他人下拉列表

时间:2016-02-01 08:22:22

标签: c# asp.net dynamic webforms controls

我想征求您的意见,为什么每当我按下btnAdd按钮时,我的最后一个标签将继续增加2.

Pic

除此之外,每当我按下btnAdd按钮时,有人可以建议我如何将下拉列表值增加1。

例如:

  • 如果我按两次“添加按钮”
  • 然后第一个问题的dropdownlist值只有一个值,例如41
  • 然后第二个问题的dropdownlist值将有两个值,例如41,42

至于现在,我尝试使用viewstate来保存dropdownlist值并检索它,它仍然不起作用。

我的HTML代码:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs" Inherits="DynamicControlV.Main" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <div>

            <asp:Button ID="btnAdd" runat="server" Text="Button" OnClick="AddTextBox"/>

            <br />

            <asp:PlaceHolder ID="txtPh" runat="server"></asp:PlaceHolder>
        </div>

    </form>
</body>
</html>

我的代码背后

    using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DynamicControlV
{
    public partial class Main : System.Web.UI.Page
    {

        int maxId = 41;

        ArrayList listOfId = new ArrayList();



        protected void Page_PreInit(object sender, EventArgs e)
        {

            var tools = new List<string> {
                        "TextBox",
                        "DropDownList",
                        "RadioButton",
                        "Button",
                        "Panel"
                    };
            tools.Sort();






            List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
            int i = 1;




            foreach (string key in keys)
            {

                this.CreateLabel("lblDynamic" + i, maxId+ i);
                this.CreateTextBox("txtDynamic" + i);
                this.CreateLabelTools("Tools : ");
                this.CreateToolsDdl("ddlToolsDynamic" + i, tools);
                this.CreateGroupIdLabel("Group ID Value : ");
                this.CreateGroupIdTextBox("txtGroupIdDynamic" + i);
                this.CreateParentQnIdLabel("Parent Qn Id : ");



                this.CreateParentQnIdDdl("ddlParentQnIdDynamic" + i, listOfId);


                i++;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Initialize dropdown list value
            //string result1 = lblQuesID.Text;
            //int prevId1 = Int32.Parse(result1);
            listOfId.Add(maxId).ToString();
        }

        protected void AddTextBox(object sender, EventArgs e)
        {

            var tools = new List<string> {
                        "TextBox",  
                        "DropDownList",
                        "RadioButton",
                        "Button",
                        "Panel"
                    };
            tools.Sort();




            int index = txtPh.Controls.OfType<TextBox>().ToList().Count + 1;





            int id = maxId + index;



            this.CreateLabel("lblDynamic" + index, id);
            this.CreateTextBox("txtDynamic" + index);
            this.CreateLabelTools("Tools : ");
            this.CreateToolsDdl("ddlToolsDynamic" + index, tools);
            this.CreateGroupIdLabel("Group ID Value : ");
            this.CreateGroupIdTextBox("txtGroupIdDynamic" + index);
            this.CreateParentQnIdLabel("Parent Qn Id : ");





            this.CreateParentQnIdDdl("ddlParentQnIdDynamic" + index, listOfId);
            listOfId.Add(maxId + index).ToString();


        }

        private void CreateTextBox(string id)
        {
            TextBox txt = new TextBox();
            txt.ID = id;

            txtPh.Controls.Add(txt);


        }


        private void CreateLabel(string id, int qnId)
        {

            string value = qnId.ToString();

            Label lbl = new Label();
            lbl.ID = id;
            lbl.Text = value;
            txtPh.Controls.Add(lbl);


        }

        private void CreateLabelTools(string text)
        {
            Label lbl = new Label();
            lbl.Text = text;
            txtPh.Controls.Add(lbl);


        }
        private void CreateToolsDdl(string id, List<string> data)
        {
            DropDownList ddl = new DropDownList();
            ddl.ID = id;
            ddl.DataSource = data;
            ddl.DataBind();
            txtPh.Controls.Add(ddl);

        }

        private void CreateGroupIdLabel(string text)
        {
            Label lbl = new Label();
            lbl.Text = text;
            txtPh.Controls.Add(lbl);


        }

        private void CreateGroupIdTextBox(string id)
        {
            TextBox txt = new TextBox();
            txt.ID = id;

            txtPh.Controls.Add(txt);



        }

        private void CreateParentQnIdLabel(string text)
        {
            Label lbl = new Label();
            lbl.Text = text;
            txtPh.Controls.Add(lbl);


        }

        private void CreateParentQnIdDdl(string id, ArrayList data)
        {


            DropDownList ddl = new DropDownList();
            ddl.ID = id;
            ddl.DataSource = data;
            ddl.DataBind();
            txtPh.Controls.Add(ddl);

            Literal lt = new Literal();
            lt.Text = "<br />";
            txtPh.Controls.Add(lt);
        }
    }
}

0 个答案:

没有答案