11

Is there a reason why the ASP.NET session ending and restarting would interfere with(prevent) the SelectedIndexChanged event firing on a dropdownlist?

The form is posting back but my breakpoint is not being hit?

Everything works perfectly prior to the session restarting.

Here's the asp for the control:

<asp:DropDownList ID="dlSort" runat="server" AutoPostBack="true" 
                  onselectedindexchanged="dlSort_SelectedIndexChanged">
</asp:DropDownList>

Here's a portion of the code:

protected void dlSort_SelectedIndexChanged(object sender, EventArgs e)
{
    PopulateItems();
    //Breakpoint above- not hit after session restarts, but hit prior to session end.
}

I'm left with an empty form as it's not getting repopulated...

Thanks in advance,

M

Edit 1:

Here is the code where the control is populated:

protected void Page_Load(object sender, EventArgs e)
{
    Form.Action = Request.RawUrl;//Required as page is rewritten
    if (!IsPostBack)
    {
        SetNoItemsMessage("");
        PopulateSortDropDown();
        PopulateItems();
    }
}

private void PopulateSortDropDown()
{
    clsProducts ops = new clsProducts();
    DataTable dt = ops.GetProductSortDropDownData();
    dlSortBy.DataSource = dt;
    dlSortBy.DataBind();
    dlSortBy.ClearSelection();
    dlSortBy.SelectedValue = "1";
}

Edit 2:

To clarify, the PopulateItems() method populates a data Repeater and should be run on the index change of the sort drop down(dlSort_SelectedIndexChanged) - this is not happening although the postback is occuring.

Page_Load executes a method that populates dlSort this is always being run.

I have examined the page extensively and everything other than the index change event fires.

Edit 3:

void Session_Start(object sender, EventArgs e)
{
    InitialiseCommonSessionVariables();//This piece of code sets default values for session variables that are used in every case.
}
Mack
  • 2,556
  • 1
  • 26
  • 44
  • Are there any session variables used in method PopulteItems()? – Mairaj Ahmad May 19 '15 at 04:07
  • Thanks, yes there are session variables used but the session is accessed through an object that checks for an initialised session prior to accessing individual variables. The main issue is that the dlSort_SelectedIndexChanged is not firing at all after the session is lost and re-established. – Mack May 19 '15 at 08:23
  • Post the code that does the initial population of the DDL. Things that might be relevant: where in the page lifecycle is it populated (e.g. Page_Load)? Is it only populated if IsPostBack is false? Is ViewState enabled? Your symptoms suggest that the DDL hasn't been populated on the postback. – Joe May 19 '15 at 16:19
  • @Joe see edits - Page_Load, IsPostBack is false, ViewState is enabled. – Mack May 20 '15 at 10:44
  • @Mack - "panelsort.Visible = true" in `PopulateSortDropDown` looks suspicious. This implies it may be false on a Postback, in which case the DDL that's contained in the panel may not be repopulated from ViewState. Also is the misspelt "dlSoryBy.DataBind" just a typo? – Joe May 20 '15 at 12:27
  • @Joe typo fixed - yeah, the panelsort is a bit misleading, the panel is a side panel that I am currently using for debugging this issue - I've removed it for clarity. – Mack May 20 '15 at 13:46
  • Can you add the code that "re-establishes" the session (whatever that means)? If you are issuing a new session cookie by self-redirecting then any postback targets will be lost and the event won't fire. – John Wu May 23 '15 at 01:12
  • Sorry @JohnWu I have been away this weekend, see edit 3. – Mack May 26 '15 at 07:51

3 Answers3

2

I experienced something similar and had to implement a workaround using the Page_PreRender event to overcome it.

In your case you may be able to test if PopulateItems() has already run and if not run it in pre-render.

ohoundj
  • 63
  • 1
  • 8
  • Hi @ohoundj, I have temporarily implemented something similar to this, but would like to understand and overcome the underlying issue. – Mack May 19 '15 at 08:25
  • Awarding bounty as the workaround is in place and seems successful, however I'm still interested in the root cause and would be interested in any further comments. – Mack May 26 '15 at 15:27
0

I think the reason might be in authentication settings. A post-back fired after your session has expired might lead you to a login page, due to reset authentication.

And even redirection might be done transparently, after a redirecting to the login page, you will loose all Post parameters, specified within the recent post-back request.

This means, that ASP.NET will have no way to detect which control has fired the post-back (it relies on EVENTTARGET parameter) and hence won't fire SelectedIndexChanged event.

  • I am experimenting with EVENTTARGET now and will post back with the results later. – Mack May 20 '15 at 11:46
  • I have implemented code to check the EVENTTARGET using @SurajSingh's answer from this post [link](http://stackoverflow.com/questions/20838022/eventtarget-is-empty-on-postback-of-button-click) and it is indeed dlSortBy. Page_Load is hit then (without running dlSort_SelectedIndexChanged), Page_PreRender is hit. – Mack May 20 '15 at 15:20
  • Looks really strange, just checked on a clean project with the same approach, and it seems to work fine - event is fired successfully even after session was expired. Can you check if Items are populated in your case on PageLoad after session has ended? – user2772146 May 21 '15 at 12:33
  • All the controls are populated. – Mack May 22 '15 at 08:08
0
            ASP.Net Code :
       ---------------

    <asp:DropDownList ID="ddList" runat="server" AutoPostBack="True" Height="65px" OnSelectedIndexChanged="ddList_SelectedIndexChanged" Width="198px">
        </asp:DropDownList>


C# Code :
---------

 public void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dropdown();
            }
        }
.
<your code goes here> ....

public void dropdown()
        {
            //String Sql statement
            string Sqlstr = "select CountryCode,Description from ca_countryMaster order by description";


            string DBCon = "Data Source=RAJA;Initial Catalog=CareHMS;Integrated Security=True;";
            SqlConnection SqlCon = new SqlConnection(DBCon);
            SqlCon.Open();

            SqlDataAdapter Sqlda = new SqlDataAdapter(Sqlstr, SqlCon);
            DataSet ds = new DataSet();
            Sqlda.Fill(ds);
            ddList.DataSource = ds.Tables[0];
            ddList.DataTextField = "Description";
            ddList.DataValueField = "CountryCode";
            ddList.DataBind();

            ds.Dispose();
            Sqlda.Dispose();
            SqlCon.Close();
        }
  • Hi @Suttipasanga this code seems to populate a dropdown list, this is not the issue I am experiencing, the fault is that the SelectedIndexChanged event of the dropdown is not firing on postback after the session is restarted. – Mack May 22 '15 at 08:29