DropDownList does not send back even if AutoPostBack is installed

Unclear solution at the end of the message

Using an asp.net page with C # codebehind, I successfully created and launched DropDownList.

What I would like to do is to capture a new value selected from the drop-down list (it is preferable to use postback for my code). Codebehind can then update other things on the page based on this newly selected dropdownlist value.

My first attempt was to use

<asp:DropDownList ID="myDDL" runat="server" AutoPostBack="true" OnSelectedIndexChanged="foo"></asp:DropDownList> 

using c # method

 public void foo(object sender, EventArgs e) { DropDownList ddl = sender as DropDownList; string myValue = ""; if (ddl != null) { myValue = ddl.SelectedValue; // Do some stuff } } 

This did not work. When the selected index was changed, it simply reloaded the page, but the IsPostBack flag was always false.

So, I sifted through SO and tried several different tactics. Most recently, I tried to register the onChange event on the client side in the code and disabled AutoPostBack.

on the ASP.Net page:

 <asp:DropDownList ID="myDDL" runat="server" AutoPostBack="false"></asp:DropDownList> 

in code:

 myDDL.Attributes.Add("onChange", "doSomeStuff(this);"); // Done on databind. 

I added client-side javascript to call the __doPostBack function page

 <script language="javascript" type="text/javascript"> function doSomeStuff(ddl) { var ddlVals = document.getElementById(ddl.id); __doPostBack(ddlVals, ''); } </script> 

This also failed, although I thought this was happening somewhere when I saw that javascript is executing correctly.

Looking at codebehind, it still doesn't work. When I set a breakpoint in Page_Load IsPostBack, that is a lie! But it must be a postback !? It was sent back using __doPostBack and (separately) automatically using AutoPostBack = "true"

So, I went deeper.

According to this MSDN article (http://msdn.microsoft.com/en-us/library/ms178141(v=VS.85).aspx) based on the results of loading the page, I do "Server Transfer", not required Postback (IsPostBack - false, PrePage, as expected, the same page that should be sent back, IsCallback - false, and IsCrossPagePosting - false).

What can happen for hyjack AutoPostBack and __doPostBack to make it look and act like a “server transfer”?

What can I install / check on the parent / page control to make sure it allows messages to be sent?

EDIT:

The load_page looks something like this:

 private SpecialDataObject _someData; private string foobar; public void Page_Load(object sender, EventArgs e) { //set some variables. this.foobar = "blah"; LoadSomeUnrelatedData(); if (!IsPostBack) { if (_someData == null) { LoadDataWithoutBinding(); } BindMyData(); } } 

With a breakpoint in //set some variables .IsPostBack page is always erroneous even after AutoPostBack.

EDIT 2:

The answer was in server transfer. In the remote control downloaded from the main page, the URL is checked and redirected before it gets to the page, which actually denies my postback. I have not seen this before because I added breakpoints only on the landing page.

+4
source share
4 answers

I would check to make sure you don’t have a check somewhere interfering with the postback. To verify this, set the CausesValidation parameter to false in the DropDownList.

+4
source

Will you reset the value of your dropdown in your PageLoad?

You might also consider using UpdatePanel so that it doesn't load the entire page.

+3
source

Is it inside an UpdatePanel? If yes, set ChildrenAsTriggers = "true"

0
source

Based on the attempts you mentioned, as well as comments on the update panel, I tried several things.

By setting the data source in the load event, you run it only once:

 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //set up data here } } 

You can use the Page.IsPostBack code and your method to get what you want:

 if (Page.IsPostBack) { //do page reload logic in here } protected void foo(object sender, EventArgs e) { //get your selected value here } 

(Note: both postback conditions are in the page load event)

EDIT:

Here is the whole setup, its basic, but you get the idea:

enter image description here

As you can see, when I made a selection from cat to dog, he acknowledged that there was a postback, so she skipped the data binding and set t. I could only assume that there is something else that I don’t see if you cannot make it ever return to the truth for you in the postback.

0
source

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


All Articles