0

I have a textbox and dropdown with items All, abc, etc. i wanted javascript code that disables the textbox when all is selected and enable it when anything else is selected.
I worked it out with that code but the issue is that if form is posted the textbox is disabled even when abc or anything else is selected.

<script type="text/javascript">
    function changeddl() {
        document.getElementById("txtsearch").disabled = document.getElementById("ddlcolumn").value == "All";
    }
</script>

aspx

<asp:DropDownList ID="ddlcolumn" runat="server" CssClass="ddl" Width="120px" AppendDataBoundItems="true" ClientIDMode="Static" ValidationGroup="Search" onchange="changeddl();" >
    <asp:ListItem>All</asp:ListItem>
    <asp:ListItem>abc</asp:ListItem>
    </asp:DropDownList>

     <asp:TextBox ID="txtsearch" runat="server" CssClass="txt" Width="200px"
     ValidationGroup="Search" ClientIDMode="Static" SkinID="txt"></asp:TextBox>
syed mohsin
  • 2,918
  • 2
  • 22
  • 46

3 Answers3

1

Try calling the changeddl() function on body onload

<body onload="changeddl()">
pravprab
  • 2,263
  • 3
  • 26
  • 41
0

Based on this answer,

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;


if(e === "All"){
     document.getElementById("txtsearch").style.display = "none"; // However you want to 'disable'
 }
Community
  • 1
  • 1
TheGeekZn
  • 3,516
  • 8
  • 47
  • 87
0

On postback of your page, write the following code:

protected void Page_Load(object sender, EventArgs e)
{
     if(ddlcolumn.SelectedValue == "All")
     {
          txtsearch.Attributes.Add("disabled","disabled");
     }
     else
     {
          txtsearch.Attributes.Remove("disabled");
     }
}
Usman Khalid
  • 2,982
  • 8
  • 37
  • 65