Click here to Skip to main content
12,359,751 members (66,274 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 asp.net 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)
                ClearSessionDataTable();
 
            if (AccountID == 0)
            {
                this.fvAccount.ChangeMode(FormViewMode.Insert);
            }
        }

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.

OR

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,
Thanks,
Aaron


UPDATE:

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"
    runat="server"
    OkControlID="btnOkBUs" PopupControlID="dialogBUs"
    TargetControlID="btnFilterBUs">
</ajax:ModalPopupExtender>
<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"
            ContextTypeName="US.QBE.BAR.Business.BARDataClassesDataContext"
            TableName="AccountBusinessUseTypes"
            Select="new (AccountBusinessUseTypeID, AccountBusinessUseTypeValue)" OrderBy="AccountBusinessUseTypeValue">
        </asp:LinqDataSource>
        <asp:CheckBoxList ID="cblBUs" runat="server"
            DataSourceID="BUsDataSource" DataTextField="AccountBusinessUseTypeValue"
            DataValueField="AccountBusinessUseTypeID"
            OnPreRender="BUs_PreRender">
        </asp:CheckBoxList>
    </div>
    <hr />
    <br />
    <b>Business Use Types:</b>
    <br />
    <br />
    <asp:Label ID="lblCheckedBUs" runat="server" Text="None"></asp:Label>
    <br />
</asp:Panel>
<asp:Label ID="lblSelectedBUs" runat="server"
     CssClass="modLblSelected" Text="None">
</asp:Label>
<h5><asp:Label runat="server" Text="*" /></h5>
Posted 28-Apr-11 5:30am
Updated 28-Apr-11 8:21am
v3

1 solution

Rate this: bad
 
good
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 Smile | :)
  Permalink  
v2
Comments
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 http://msdn.microsoft.com/en-us/magazine/cc163724.aspx

http://www.tutorialized.com/view/tutorial/Personalization/10366

This one compares session and personalization http://www.developer.com/net/asp/article.php/3520226/ASPNET-20-Profile-Simple-User-Personalization-for-Your-Web-Apps.htm

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.
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: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:

<pre>
.MultiSelect
{
width: 13px;
height: 13px;
background-image: url(images/multiselect.gif);
}
</pre>
Monjurul Habib 28-Apr-11 13:26pm
   
nice help.my5
Albin Abel 28-Apr-11 15:04pm
   
Thanks Monjurul Habib.

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.160621.1 | Last Updated 28 Apr 2011
Copyright © CodeProject, 1999-2016
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