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,948
  • 2
  • 23
  • 47

3 Answers3

1

Try calling the changeddl() function on body onload

<body onload="changeddl()">
pravprab
  • 2,301
  • 3
  • 26
  • 43
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,696
  • 10
  • 55
  • 91
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
  • 3,032
  • 9
  • 41
  • 66