In my project i have kept source info in dropdownlist. Like source of info: in dropdownlist three items website, Newspaper and others. If user select Others item, then only other text box should be visible otherwise should be invisible. For that i have set in page load event
lblother.visible=false; txtother.visible=false;
And in Btnsubmit event i have written the condition like. if(dropdownlistinfo.selectedindex==2) { lblother.visible=true; txtother.visible=true; } But in my case i m not getting my desire output. Its always invis开发者_StackOverflowible when i am selecting Others item from drowdownlist also. Pls somebody help me where is my mistake?
Thanks, Sumit
I think the problem is here.
if (!IsPostBack)
{
lblother.visible = false;
txtother.visible = false;
}
This will work if you set Selected property of the default list item.
<asp:DropDownList ID="DropDownList" runat="server">
<asp:ListItem Text="Website" Selected="True"></asp:ListItem>
<asp:ListItem Text="Newspaper"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblOther" runat="server" Text="Other"></asp:Label>
<asp:TextBox ID="txtOther" runat="server"></asp:TextBox>
Hide the controls in the Page Load event.
protected void Page_Load(object sender, EventArgs e)
{
this.txtOther.Visible = false;
this.lblOther.Visible = false;
}
Then show the controls in the button click event.
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 2)
{
this.txtOther.Visible = true;
this.lblOther.Visible = true;
}
}
精彩评论