I'm not sure if I understand your problem, but you want to achieve postback only if a certain condition is met. you can connect the javascript function as a drop-down menu onchange = "return onchange ();" Set Autopostback = true;
function Onchange() { var ddl1 = document.getElementById('<%= ddl1.ClientID %>'); var ddl2 = document.getElementById('<%= ddl2.ClientID %>'); var txtbox = document.getElementById('<%= txtbox.ClientID %>'); if (ddl1.selectedIndex == 2 && ddl2.selectedIndex > 2) { txtbox.style.display = "inline"; __doPostBack(ddl1, ''); } else { txtbox.style.display = "none"; return false; } }
Aspx code should look like this.
<asp:DropDownList runat="server" AutoPostBack="true" ID="ddl1" onchange="return Onchange();" OnSelectedIndexChanged="ddl1_SelectedIndexChanged"> <asp:ListItem Text="text1" /> <asp:ListItem Text="text2" /> <asp:ListItem Text="text3" /> <asp:ListItem Text="text4" /> </asp:DropDownList> <asp:DropDownList runat="server" AutoPostBack="true" ID="ddl2" onchange="return Onchange();" OnSelectedIndexChanged="ddl1_SelectedIndexChanged"> <asp:ListItem Text="text1" /> <asp:ListItem Text="text2" /> <asp:ListItem Text="text3" /> <asp:ListItem Text="text4" /> </asp:DropDownList> <asp:TextBox runat="server" ID="txtbox" />
Tested and working ...
source share