user466663
user466663

Reputation: 845

How to dynamically replace one set of server controls with another in asp.net webforms

I am working on asp.net webforms. I have a usercontrol which has several server controls. There are three related dropdown boxes. If there are multiple values on all these dropdown boxes, I have to let the user choose values from them and then I save them in database. If each one of them has only one value, then I have to show the values as read only - say as asp.net labels. These values also need to be saved in the database. Please let me know how to replace one set of controls with another and get their values.

Thanks

Upvotes: 1

Views: 838

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67918

So to show one set of controls over the other, consider the following HTML:

<asp:DropDownList ID="List1" runat="server" Visible="false" />
<asp:DropDownList ID="List2" runat="server" Visible="false" />
<asp:DropDownList ID="List3" runat="server" Visible="false" />
<asp:Label ID="Label1" runat="server" Visible="false" />
<asp:Label ID="Label2" runat="server" Visible="false" />
<asp:Label ID="Label3" runat="server" Visible="false" />

if you want the drop down lists visible then run this in the code-behind where relevant:

List1.Visible = true;
List2.Visible = true;
List3.Visible = true;

and in contrast, if you want the labels visible then run this code:

Label1.Visible = true;
Label2.Visible = true;
Label3.Visible = true;

Please note that when a control in ASP.NET is not visible - it's not rendered. So the control is not included in the HTML. This is why I set all of them to invisible to start with.

Upvotes: 1

Related Questions