Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropDownList not posting back, even with AutoPostBack set

Obscure solution at end of post

Using asp.net page with C# codebehind, I have successfully built and populated a DropDownList.

What I would like to do is capture a new value selected from the dropdownlist (preferrably using a postback to my codebehind). The 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>

with the 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 a number of different tactics. Most recently, I tried to register the client-side onChange event in the codebehind and turned off the AutoPostBack.

in the ASP.Net page:

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

in the codebehind:

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

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

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

        __doPostBack(ddlVals, '');
    }
</script>

This also failed, although I thought it was going somewhere when I saw the javascript execute properly.

Looking in the codebehind, though, it's still not working. When I put a breakpoint in the Page_Load IsPostBack is false! But it's supposed to be a postback!? It was posted back using __doPostBack and (seperately) automatically with AutoPostBack="true"

So I dug deeper.

According to this MSDN article (http://msdn.microsoft.com/en-us/library/ms178141(v=VS.85).aspx) based on the results on the page load I'm doing a "Server Transfer" rather than the desired Postback (IsPostBack is false, PreviousPage is, as expected, the same page that should be posting back, IsCallback is false, and IsCrossPagePosting is false).

What could be going on to hyjack the AutoPostBack and the __doPostBack to make it look and act like a "Server Transfer"?

What can I set/check on the parent control/page to make sure it's allowing postbacks?

EDIT:

The Page_Load looks something like:

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 at //set some variables the Page.IsPostBack is always false even after AutoPostBack.

EDIT 2:

The answer was in the Server Transfer. In a distant control loaded from the master page, the URL is inspected and rerouted before it hits the page, effectively negating my postback. I didn't see it before because I had added the breakpoints only in the target page.

like image 310
Peach Avatar asked Nov 11 '11 20:11

Peach


2 Answers

I would check to make sure that you don't have validation somewhere interfering with the postback. To check this, set CausesValidation to false on the DropDownList.

like image 184
James Johnson Avatar answered Sep 29 '22 04:09

James Johnson


Are you resetting the value of your dropdown in your PageLoad?

Also you may want to think about using an UpdatePanel so that it doesnt reload the whole page.

like image 41
Josh Mein Avatar answered Sep 29 '22 04:09

Josh Mein