visibility attribute
-
I think this is a simple question but Im new to asp.net so give me a break!!! i have a drop down menu and i want to make different labels visible when different values are selected from the dropdown menu, i dont wan to have to use a button to submit tho, any suggestions?
-
I think this is a simple question but Im new to asp.net so give me a break!!! i have a drop down menu and i want to make different labels visible when different values are selected from the dropdown menu, i dont wan to have to use a button to submit tho, any suggestions?
You can set the
AutoPostBack
attribute of theDropDownList
control to true, then code for theSelectedIndexChanged
event. Something like this should work:<% @Page Language="c#" %>
<script runat="server">
void ddSelectionChange(Object sender, EventArgs e)
{
// hide all labels to start with
lbl1.Visible = false;
lbl2.Visible = false;
lbl3.Visible = false;// determine the value of the selected drop down list item string sID = dd.SelectedItem.Value; // make that label visible if (sID != null && sID != "") { Control c = FindControl(sID); c.Visible = true; }
}
</script><html>
<head></head>
<body><form runat="server"> <asp:DropDownList id="dd" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddSelectionChange" > <asp:ListItem Value="lbl1" Text="First Label" Selected="True" /> <asp:ListItem Value="lbl2" Text="Second Label" /> <asp:ListItem Value="lbl3" Text="Third Label" /> </asp:DropDownList> <asp:Label id="lbl1" runat="server" visible="true" text="This is Label 1" /> <asp:Label id="lbl2" runat="server" visible="false" text="This is Label 2" /> <asp:Label id="lbl3" runat="server" visible="false" text="This is Label 3" /> </form>
</body>
</html> -
You can set the
AutoPostBack
attribute of theDropDownList
control to true, then code for theSelectedIndexChanged
event. Something like this should work:<% @Page Language="c#" %>
<script runat="server">
void ddSelectionChange(Object sender, EventArgs e)
{
// hide all labels to start with
lbl1.Visible = false;
lbl2.Visible = false;
lbl3.Visible = false;// determine the value of the selected drop down list item string sID = dd.SelectedItem.Value; // make that label visible if (sID != null && sID != "") { Control c = FindControl(sID); c.Visible = true; }
}
</script><html>
<head></head>
<body><form runat="server"> <asp:DropDownList id="dd" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddSelectionChange" > <asp:ListItem Value="lbl1" Text="First Label" Selected="True" /> <asp:ListItem Value="lbl2" Text="Second Label" /> <asp:ListItem Value="lbl3" Text="Third Label" /> </asp:DropDownList> <asp:Label id="lbl1" runat="server" visible="true" text="This is Label 1" /> <asp:Label id="lbl2" runat="server" visible="false" text="This is Label 2" /> <asp:Label id="lbl3" runat="server" visible="false" text="This is Label 3" /> </form>
</body>
</html>