简体   繁体   中英

Need help asp.net other textbox

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 im 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

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; 
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM