Click here to Skip to main content
12,898,392 members (55,127 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET session viewstate , +
Hello All,

Here is my scenario:

I am using VS2010 and have a ASP.NET web application. On my page I am using a WebUserControl that uses a modelpopupextender to allow a user to select contacts from a list. All of this is contained in a formview.

After a user selects some contacts they click the submit button and their selection goes to a ListView that is bound to a DataTable that I store in the session.

All of the above works great.

The problem is that when the user inserts the object with the contacts the DataTable information is kept and if you do another insert the previous objects re-appear when they should not. I can't clear the session after the insert incase the user closes the page without inserting and another open retains the session or another screen re-loads the data.

What I want to do is the following:

protected void Page_Load(object sender, EventArgs e)
            if (!this.IsPostBack)
            if (AccountID == 0)

But...... the load says that it is not a postback no matter what button I click on the page. So the session information is cleared at every event. Also the Viewstate is not kept between postbacks.

What I need is a way to determine the initial load of the page so I can clear the session.


I need a better way to temporary store a dynamic amount of information without submitting them to the database until the user clicks saves.

I hope some of this makes sense,


It turns out that my Page_load is fireing more than 1 time per post back. I have it narrowed down the the culprit but am not sure why it is causeing this. - When I take the below out of my WebUserControl the page load problem is fixed. FYI - the below code creates a pop-up that displays a list of checkboxes which allows a user to select more than one item - it it pretty involved with javascript.

<asp:ImageButton ID="btnFilterBUs" runat="server"
    CssClass="MultiSelect" />
<ajax:ModalPopupExtender ID="btnFilterBUs_ModalPopupExtender"
    OkControlID="btnOkBUs" PopupControlID="dialogBUs"
<asp:Panel ID="dialogBUs" runat="server" CssClass="modPnl" style="display:none;">
    <b>Select Business Use Types To Display:</b><br />
    <br />
    <asp:LinkButton ID="btnOkBUs" runat="server" style="float:right" >OK</asp:LinkButton>
    <a href="javascript:clearCheckboxes('<%=lblSelectedBUs.ClientID %>', '<%=lblCheckedBUs.ClientID %>', BusinessUseTypeCBA);" style="float:left">Clear Selection</a>
    <br />
    <br />
    <div class="modDiv">
        <asp:LinqDataSource ID="BUsDataSource" runat="server"
            Select="new (AccountBusinessUseTypeID, AccountBusinessUseTypeValue)" OrderBy="AccountBusinessUseTypeValue">
        <asp:CheckBoxList ID="cblBUs" runat="server"
            DataSourceID="BUsDataSource" DataTextField="AccountBusinessUseTypeValue"
    <hr />
    <br />
    <b>Business Use Types:</b>
    <br />
    <br />
    <asp:Label ID="lblCheckedBUs" runat="server" Text="None"></asp:Label>
    <br />
<asp:Label ID="lblSelectedBUs" runat="server"
     CssClass="modLblSelected" Text="None">
<h5><asp:Label runat="server" Text="*" /></h5>
Posted 28-Apr-11 5:30am
Updated 28-Apr-11 8:21am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you know about Personalization you can very well to store and retrieve user specific informations instead of session. However those objects needs to be serializable. This is an alternate suggestion. If you already know about personalization in .Net i don't need to write more on it :)
Nishant Sivakumar 28-Apr-11 12:03pm
Decent response. My vote of 5!
Albin Abel 28-Apr-11 12:04pm
Thanks Nishant
wooga111 28-Apr-11 12:12pm
I am not familiar with personalization. I will take a look to see if it is something I can use. If you have any good reference material I would appreciate it. Otherwise it's google to the rescue :)
Albin Abel 28-Apr-11 12:37pm
Here are few links

This one compares session and personalization

wooga111 28-Apr-11 13:22pm
I really like how personalization works but I am not sure it will solve my problem. I will still want to clear out the saved DataTable on an initial object mode - this is only a problem on insert with the formview. I can't clear the information because !IsPostBack is false for things other than the initial page load. (it fires during page button clicks). The saved DataTable needs to be cleared only once at the beginning. Any code I write had it clearing at every event.
Monjurul Habib 28-Apr-11 13:26pm
nice help.my5
wooga111 28-Apr-11 13:51pm
I just realized my page_load is fireing more than once. If I can stop that from happening I don't think I will have a problem.
Albin Abel 28-Apr-11 14:07pm
Are you saying the page_load firing more than once in a single page cycle and !IsPostback true at every time?. If so post your code snippets. We will try to help.
wooga111 28-Apr-11 14:19pm
Unfortunately yes - I have pasted in the asp that is causing the problem. Once I remove it everything works...
wooga111 28-Apr-11 14:28pm
ACK! I can't believe it but it is the image button that is breaking everything... I'm going to do a full scale test and let you know what is going on!
wooga111 28-Apr-11 14:31pm
Yeah that was it - I changed it to a reg button and removed the CSS style and WIN! My problem is solved. I can't thank you enough for your help Albin! Also thank you for teaching me what personalization is!
Albin Abel 28-Apr-11 15:03pm
Glad to know you have solved the problem. But it is still a puzzle why an Image button given that issue and also the css link to the modal popup. I tried with an image button it works fine. What you had at the css style of it. I will try to emulate the problem here and update you later.
Albin Abel 28-Apr-11 15:04pm
Thanks Monjurul Habib.
Albin Abel 28-Apr-11 15:45pm
I got this same behaviour in IE, strangely not in firefox. Let us consider this as a bug as of now and will do research later. Thanks for notifying this.
wooga111 28-Apr-11 17:47pm
This was my css:

width: 13px;
height: 13px;
background-image: url(images/multiselect.gif);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 28 Apr 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100