Skip to content Skip to sidebar Skip to footer

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 o

Solution 1:

// get a reference to the DropDownListvar 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 serverif(confirm("Are you sure you want to do this?")){
     __doPostBack(selectlistId, '');
  }
};

Solution 2:

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

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

Solution 3:

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.

Post a Comment for "Asp.net Dropdownlist Conditional Postback"