ASP.NET - Software Snapshot of a Software Management List

I have two dropdownlists on my form - ddl1 and ddl2. Together they determine the visibility of the -txt1 text field. For this, I do this check:

if (ddl1.SelectedIndex==2 && ddl2.SelectedIndex>2) { if (!txt1.Visible) {txt1.Visible=true;// And then I want to call postback} } else { if (txt1.Visible) {txt1.Visible=false;// And then I want to call postback} } 

As you can see, I want to publish the page on the server only if the above conditions are true. The above code is fired in the SelectedIndexChanged event of both dropdownlists. How can I or can this be achieved on condition?

+6
source share
2 answers

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

+7
source

If AutoPostBack = True, which should be for your events to fire, just call funciton when your condition is met. ASP.NET always sends back, you just need to handle this condition, otherwise you will have to process the validation using JavaScript and manually publish the page:

  if (ddl1.SelectedIndex==2 && ddl2.SelectedIndex>2) { if (!txt1.Visible) { txt1.Visible=true;// And then I want to call postback //dowork } } else { if (txt1.Visible) { txt1.Visible=false;// And then I want to call postback //do work } } 
+1
source

Source: https://habr.com/ru/post/914720/


All Articles