64949

asp.net Dropdownlist conditional postback

I have a dropdownlist and have an event selectedIndexChanged which postsback, i want to be able to show a message to the user whenever he changes the value in dropdownlist, based on the input from the message i will decide if i have to postback or not.

The message shown would be are you sure? if he selects yes i would continue with postback, if he says no, i would cancel the postback and assign the previous value as selected.

I have searched alot but cant figure out a solution to this, i think if there is a javascipt function which determines if a postback is required or not that could help i guess

Thanks

Answer1:

// get a reference to the DropDownList var selectlistId = '<%= ddlYourList.ClientID %>', selectlist = document.getElementById(selectlistId); // attach to the onchange event selectlist.onchange = function() { // decide whether to execute the __doPostBack event, which submits the // form back to the server if(confirm("Are you sure you want to do this?")){ __doPostBack(selectlistId, ''); } };

Answer2:

You can stop can cancel postback of dropdownlist very simply.Just add this javascript on page load event.

protected void Page_Load(object sender, EventArgs e) { DropDownList1.Attributes.Add("OnChange", "if (!confirm('Change this?')){return};"); }

Answer3:

In order to accomplish this task you will need to use a CustomValidator with custom client side Javascript to control the post back.

You can read this article on 4Guys discussing the different validators with a client side validator JavaScript sample to get an idea.

But the core solution would be using a custom validator to control the post back only when the form is valid.

Recommend

  • Javascript to change background colour and font size
  • JavaScript function and UI updates
  • How to transfer php code from javascript to PHP?
  • How can I increase a number by activating a button to be able to access a function each time the but
  • Using same constraints in multiple classes
  • Is it possible to get the word under the mouse cursor in a ``?
  • how to populate a SQLite database and use that database in phonegap?
  • How to get the date of next specified day of week
  • c++ regex_replace not doing intended substitution
  • Outputting SharePoint Hyperlink Column as URL
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Is there some graphical way to create my own configuration file on SonarLint?
  • Bigquery event streaming and table creation
  • Convert SQLite database to XML
  • ASP.NET MVC Application won't update some controllers
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • What is the purpose of TaskExecutor in spring?
  • How does this usort cmp function actually work?
  • Inline R code in YAML for rmarkdown doesn't run
  • Problem while Building a Setup Project for a windows Service?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • How can I sort a a table with VBA with given text condition?
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • How to use an array of arrays with array_map(…) in PHP?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Is there a javascript serializer for JSON.Net?
  • Javascript simulate pressing enter in input box
  • Read text file and split every line in MSBuild
  • Fetching methods from BroadcastReceiver to update UI
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal