且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从asp.net的下拉列表中动态添加不同的控件

更新时间:2023-12-06 10:11:16

现在更新的代码如下:

 protected void ddlIds_SelectedIndexChanged(object sender,EventArgs e)
{
switch(ddlIds.SelectedItem.Text)
{
caseTextbox:
var txtBox = string.Empty;
if(hdnValue.Value!=&& hdnValue.Value.Contains(txtBox))
{
var value = hdnValue.Value.Split('^') ;
int latestID = CountStringOccurrences(hdnValue.Value,txtBox)+ 1;
txtBox =txtBox+ latestID.ToString();
hdnValue.Value = hdnValue.Value +^+ txtBox;
}
其他
{
txtBox =txtBox1;
hdnValue.Value = hdnValue.Value +^+ txtBox;
}
休息;
caseCheckBox:
var chkbox = string.Empty;
if(hdnValue.Value!=&& hdnValue.Value.Contains(chkBox))
{
var value = hdnValue.Value.Split('^') ;
int latestID = CountStringOccurrences(hdnValue.Value,chkBox)+ 1;
chkbox =chkBox+ latestID.ToString();
hdnValue.Value = hdnValue.Value +^+ chkbox;
}
else
{
chkbox =chkBox1;
hdnValue.Value = hdnValue.Value +^+ chkbox;
}
休息;
}


if(hdnValue.Value!=)
{
var val = hdnValue.Value.Split('^');
foreach(val中的var ID)
{
if(IDs.Contains(chk))
{
CheckBox chkBox = new CheckBox();
chkBox.ID = IDs;
Panel1.Controls.Add(chkBox);
}

else if(IDs.Contains(txt))
{
TextBox TxtBox = new TextBox();
TxtBox.ID = ID;
Panel1.Controls.Add(TxtBox);
}

}
}
}





在回帖中处理控件



这个函数我用过链接ttp://www.dotnetperls.com/string-occurrence

 public static int CountStringOccurrences(string text,string pattern)
{
int count = 0;
int i = 0;
while((i = text.IndexOf(pattern,i))!= -1)
{
i + = pattern.Length;
count ++;
}
返回计数;
}





如果你愿意,可以将它标记为解决方案


假设你有DDL as -



 <   asp:DropDownList     ID   =   ddlSample    runat   =  server    AutoPostBack   =  True    OnSelectedIndexChanged   =  ddlSample_SelectedIndexChanged    >  
< asp:ListItem > 文本框< / asp:ListItem >
< asp:ListItem > 按钮< / asp:ListItem >
< asp:ListItem > ComboBox < / asp:ListItem >
< asp:ListItem > DropdownList < / asp:ListItem >
< / asp:DropDownList >





 受保护  void  ddlSample_SelectedIndexChanged( object  sender,EventArgs e)
{
if (ddlSample.SelectedItem.Text == Textbox
{
TextBox TxtBoxU = new TextBox();
TxtBoxU.ID = TextBoxU + i.ToString();
// 将文本框添加到Panel。
Panel1.Controls.Add (TxtBoxU);
}
}







请参考:



http:// www。 dotnettips4u.com/2013/03/dynamically-creating-text-boxes-using-c.html [ ^ ]


Yash,

你可以使用技巧,我为你提供样品你可以添加你的逻辑



在Aspx中使用:

 <   asp:DropDownList     ID   =  ddlIds    runat   = 服务器    AutoPostBack   =  True    OnSelectedIndexChanged   =  ddlIds_SelectedIndexChanged   >  
< asp:ListItem > 文本框< / asp:ListItem >
< asp:ListItem > 按钮< / asp:ListItem >
< asp:ListItem > CheckBox < / asp:ListItem >
< asp:ListItem > DropdownList < / asp:ListItem >
< / asp:DropDownList >
< asp:Panel ID = Panel1 runat = 服务器 > < / asp:Panel >
< asp:HiddenField ID = hdnValue runat = 服务器 / >





在代码背后使用此逻辑



 protected void ddlIds_SelectedIndexChanged(object sender,EventArgs e)
{
TextBox TxtBox = new TextBox();
CheckBox chkBox = new CheckBox();

switch(ddlIds.SelectedItem.Text)
{
caseTextbox:
TxtBox.ID =txtBox;
hdnValue.Value = hdnValue.Value +^+ TxtBox.ID;
休息;
caseCheckBox:
chkBox.ID =chkBox;
hdnValue.Value = hdnValue.Value +^+ chkBox.ID;
休息;
}


if(hdnValue.Value!=)
{
var val = hdnValue.Value.Split('^');
foreach(val中的var ID)
{
if(IDs.Contains(chk))
{
chkBox.ID = IDs;
Panel1.Controls.Add(chkBox);
}

否则if(IDs.Contains(txt))
{
TxtBox.ID = IDs;
Panel1.Controls.Add(TxtBox);
}

}
}
}





希望这会如果这是解决方案,请为您做好,请将其标记为解决方案


Hello

I am creating a page in asp.net website on which i add the controls dynamically on selection from drop down list, but on every selection from the drop down list the previous control lost due to post back of drop down list.
I want to add the controls dynamically in my panel on selection from drop down list. For example: if i select the text box from the drop down list,the control should be add to the panel then again if i select the button from drop down list, the button should be added to the panel. Now the panel should have the text box and button. Now if i select the drop down list from the drop down list, it should be added to the panel i.e panel should display the text box ,button and drop down list in it.

Can any body suggest me or help me to figure it out.

Thanks & Regards

Now Updated code is this :
protected void ddlIds_SelectedIndexChanged(object sender, EventArgs e)
       {
           switch (ddlIds.SelectedItem.Text)
           {
               case "Textbox":
                   var txtBox = string.Empty;
                   if (hdnValue.Value != "" && hdnValue.Value.Contains("txtBox"))
                   {
                       var value = hdnValue.Value.Split('^');
                       int latestID = CountStringOccurrences(hdnValue.Value, "txtBox") + 1;
                       txtBox = "txtBox" + latestID.ToString();
                       hdnValue.Value = hdnValue.Value + "^" + txtBox;
                   }
                   else
                   {
                       txtBox = "txtBox1";
                       hdnValue.Value = hdnValue.Value + "^" + txtBox;
                   }
                   break;
               case "CheckBox":
                   var chkbox = string.Empty;
                   if (hdnValue.Value != "" && hdnValue.Value.Contains("chkBox"))
                   {
                       var value = hdnValue.Value.Split('^');
                       int latestID = CountStringOccurrences(hdnValue.Value, "chkBox") + 1;
                       chkbox = "chkBox" + latestID.ToString();
                       hdnValue.Value = hdnValue.Value + "^" + chkbox;
                   }
                   else
                   {
                       chkbox = "chkBox1";
                       hdnValue.Value = hdnValue.Value + "^" + chkbox;
                   }
                   break;
           }


           if (hdnValue.Value != "")
           {
               var val = hdnValue.Value.Split('^');
               foreach (var IDs in val)
               {
                   if (IDs.Contains("chk"))
                   {
                       CheckBox chkBox = new CheckBox();
                       chkBox.ID = IDs;
                       Panel1.Controls.Add(chkBox);
                   }

                   else if (IDs.Contains("txt"))
                   {
                       TextBox TxtBox = new TextBox();
                       TxtBox.ID = IDs;
                       Panel1.Controls.Add(TxtBox);
                   }

               }
           }
       }



Handle the control in post back

This function I have used from link ttp://www.dotnetperls.com/string-occurrence

public static int CountStringOccurrences(string text, string pattern)
        {
            int count = 0;
            int i = 0;
            while ((i = text.IndexOf(pattern, i)) != -1)
            {
                i += pattern.Length;
                count++;
            }
            return count;
        }



Marked it as solution if you like it


Suppose you have DDL as -

<asp:DropDownList ID="ddlSample" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlSample_SelectedIndexChanged" >
    <asp:ListItem>Textbox</asp:ListItem>
    <asp:ListItem>Button</asp:ListItem>
    <asp:ListItem>ComboBox</asp:ListItem>
    <asp:ListItem>DropdownList</asp:ListItem>
</asp:DropDownList>



protected void ddlSample_SelectedIndexChanged(object sender, EventArgs e)
      {
          if (ddlSample.SelectedItem.Text == "Textbox")
          {
              TextBox TxtBoxU = new TextBox();
              TxtBoxU.ID = "TextBoxU" + i.ToString();
              //Add the textbox to the Panel.
              Panel1.Controls.Add(TxtBoxU);
          }
      }




refer this :

http://www.dotnettips4u.com/2013/03/dynamically-creating-text-boxes-using-c.html[^]


Yash ,
you can use a trick, I am providing you a sample You can add your logic

In Aspx Use :
<asp:DropDownList ID="ddlIds" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlIds_SelectedIndexChanged" >
    <asp:ListItem>Textbox</asp:ListItem>
    <asp:ListItem>Button</asp:ListItem>
    <asp:ListItem>CheckBox</asp:ListItem>
    <asp:ListItem>DropdownList</asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="Panel1" runat="server"></asp:Panel>
<asp:HiddenField ID="hdnValue" runat="server"/>



In code behind use this logic

protected void ddlIds_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox TxtBox = new TextBox();
            CheckBox chkBox = new CheckBox();

            switch (ddlIds.SelectedItem.Text)
            {
                case "Textbox":
                    TxtBox.ID = "txtBox";
                    hdnValue.Value = hdnValue.Value + "^" + TxtBox.ID;
                    break;
                case "CheckBox":
                    chkBox.ID = "chkBox";
                    hdnValue.Value = hdnValue.Value + "^" + chkBox.ID;
                    break;
            }


            if (hdnValue.Value != "")
            {
                var val = hdnValue.Value.Split('^');
                foreach (var IDs in val)
                {
                    if (IDs.Contains("chk"))
                    {
                        chkBox.ID = IDs;
                        Panel1.Controls.Add(chkBox);
                    }

                    else if (IDs.Contains("txt"))
                    {
                        TxtBox.ID = IDs;
                        Panel1.Controls.Add(TxtBox);
                    }

                }
            }
        }



Hope this will do for you if that is the solution please mark it as solution