views:

101

answers:

2

Hi all, 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 invisible when i am selecting Others item from drowdownlist also. Pls somebody help me where is my mistake?

Thanks, Sumit

A: 

I think the problem is here.

if (!IsPostBack)
{
    lblother.visible = false;
    txtother.visible = false;
}
Mehdi Golchin
Hi Mr. Mehdi Though i have written your if condition its not working pls give me the alternate solution.
Sumit
A: 

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; 
    } 
}
Phaedrus
Hi its working under dropdown_selectedindex change event. For this i had to set autopostback=true in dropdownlist control. Now the problem is when i am first time running my application and selecting other item from dropdownlist Other lable and Other text box is visible, but when i select website or newspaper from drowpdownlist the other text box and lable immediately should be in visible. but its not happening. Pls solve this problem....thanks
Sumit
Thanks Mr.Phaedrus its working fine...
Sumit
This should work the same using the SelectedIndexChanged event. Are txtOther.Visibile = false and lblOther.Visibile = false in the page load event wrapped in a !IsPostPack, if so remove these statements from inside the block.
Phaedrus