?
Solved

Session variable ..

Posted on 2006-05-02
7
Medium Priority
?
383 Views
Last Modified: 2006-11-18
I have two pages A and B.

ON page A, I have three drop down boxes (  month , year, and unit_abbr and they have cascading and Auto post backs) and click button to run a report. What I am trying to do is keeping selected drop down list values after the postbacks. The problem right now is when I come back to page A after I browse page B , the selected values in dropdown boxes are set back to the default ( in my case  most recent month, year, unit_num). How can I keep the selected values inside of dropdown lists while I am browsing around different pages? If I select month = 1 year 2006 unit_abbr = D11 to run the report on page A, then go to the different page to browse around different report and when I come back to Page A, the selected parameters in those drop down lists should be set to month = 1 year 2006 unit_abbr = D11

Here is my code using session variable:
<tr>


<td style="width: 100px; height: 23px">

<asp:Label ID="Label4" runat="server" Text="Select Year:" ForeColor="Black" Font-Bold="True" Font-Names="Arial" Font-Size="8pt"/></td>

<td style="width: 123px; height: 23px">

<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" DataTextField="year" DataValueField="year" Font-Names="Tahoma" Font-Size="8pt" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged1"/></td>

<td style="width: 12px; height: 23px">

</td>

<td rowspan="2" style="width: 211px; text-align: center;">

<asp:Label ID="AccountStatus" runat="server" Font-Size="16pt" Visible="False" Width="87px"></asp:Label></td>

</tr>

<tr>

<td style="width: 100px; height: 22px;">

<asp:Label ID="Label5" runat="server" Text="Select Month:" ForeColor="Black" Font-Bold="True" Font-Names="Arial" Font-Size="8pt"/></td>

<td style="width: 123px; height: 22px;">

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="month" DataValueField="month" Font-Names="Tahoma" Font-Size="8pt" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"/></td>

<td style="width: 12px; height: 22px;">

</td>

</tr>

<tr>

<td style="width: 100px; height: 22px;">

<asp:Label ID="Label7" runat="server" Text="Select Region:" ForeColor="Black" Font-Bold="True" Font-Names="Arial" Font-Size="8pt"></asp:Label></td>

<td style="width: 123px; height: 22px;">

<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource6" DataTextField="region_name" DataValueField="Region_num" Font-Names="Tahoma" Font-Size="8pt" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged">

</asp:DropDownList></td>

<td style="width: 12px; height: 22px;">

&nbsp;</td>

<td style="width: 211px; height: 22px;">

</td>

</tr>


<tr>
 

--Here is code behind

public

partial class Dashboard : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{



if (!Page.IsPostBack && ((this.Session["MMselectedvalue"] != null) && (this.Session["YYselectedvalue"] != null)


if (!Page.IsPostBack && ((this.Session["MMselectedvalue"] != null) && (this.Session["YYselectedvalue"] != null) && (this.Session["Unitselectedvalue"]) != null) && (this.Session["Regionselectedvalue"] != null))
{


this.DropDownList1.SelectedIndex = Convert.ToInt32(this.Session["MMselectedvalue"]);

this.DropDownList2.SelectedIndex = Convert.ToInt32(this.Session["YYselectedvalue"]);

this.DropDownList3.SelectedIndex = Convert.ToInt32(this.Session["Unitselectedvalue"]);

this.DropDownList4.SelectedIndex = Convert.ToInt32(this.Session["Regionselectedvalue"]);
GridView4.DataBind();

}


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{


this.Session["MMselectedvalue"] = this.DropDownList1.SelectedIndex;
}

 


protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{


this.Session["YYselectedvalue"] = this.DropDownList2.SelectedIndex;
}


protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{


this.Session["Unitselectedvalue"] = this.DropDownList3.SelectedIndex;

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{


this.Session["Regionselectedvalue"] = this.DropDownList4.SelectedIndex;
 


But, the problem is the data controls of drop downboxe ( sql datasource) are overwriting the session varialbles, so it set back to the default value...
 
How can I keep the  session variables after the data binding ( sql datasource for drop downboxes)? ,and How can I ditermine whether it's just a postback ( changing selected values on the same page) or the user is actually changing the page( navigate different pages and come back to the same page).?
 
0
Comment
Question by:jung1975
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:kyle_clark
ID: 16590715
Why don't you pass the variables on the query string instead?

www.mysite.com/b.aspx?month=1&year=2006&unit_abbr=d_11

//declare variables, I use strings for an example
string month,year,unit_abbr;

//must cast to correct type (whatever that may be)
month = Request["month"].ToString();
year=Request["year"].ToString();
unit_abbr=Reqeust["unit_abbr"].ToString();

Then, on the page load of A you can use these to set the index of the different drop downs.  It may make more sense to continue using the session variables to pass to b and pass the index on the query string.  When you leave b and come back to a, just pass the variables back.  The syntax is simple, whatever.aspx and a ? then the name of the first variable, after that add an & between each variable (as I did above).  Use the request object (like above) to retrieve the variables.  Make sure to wrap this in a try/catch block because if you request a variable from the query string that is not there, it throws an error.  If the ids are there then set the selected index to that id or however you are doing it.  From this you should be able to achieve what you were looking for.

That may be your problem/solution or first just try:

In your page load use if(!Page.IsPostBack)

or it may just be if(!IsPostBack)

Then if it is not a postback do one action (the action you want to happen when the user first hits the page which would be bind the boxes), otherwise do something else (which usually is nothing at all).  By using that if statement the binding will only occur the first time the user is transferred to the page.

It sounds like you had two problems, hopefully using what I have wrote will help you fix the problem.
0
 

Author Comment

by:jung1975
ID: 16590886
Thanks,but I am using a sitemap to navigate each page.. Can I still use the query string to achieve what  I am looking for?

Is there anyway you can provide me more detailed code examples?

0
 
LVL 2

Expert Comment

by:kyle_clark
ID: 16590925
I'm not quite sure what you are trying to do, but what you are describing should be able to be achived by using Session Variables.  When you check for post back you shouldn't check for those other conditions with it.

Just check for post back, then check the other conditions, and instead of using if statements, use a try catch block and catch the exception if the variables are null.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:jung1975
ID: 16591228
Thanks, I think I almost figured it out.. except the dropdownlist3 is still set back to the default value ( doesn't remember previous selected value). Is it because the dropdownbox3 is populated by the drop down box4 ( cascading dropdown box)?  How can I work around this?

--Here is the code for  dropdownbox3
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource4" DataTextField="unitname_abbr" DataValueField="unit_abbr" Width="352px" Font-Names="Tahoma" Font-Size="8pt" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="True"/><br />

  <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Finance_AppsConnectionString %>" SelectCommand="&#13;&#10;select distinct(unit_name) as unit_name,unit_abbr&#13;&#10;,  ( convert(varchar(5), unit_abbr) + ' - ' + convert(varchar(50), unit_name))as unitname_abbr&#13;&#10; from revenue_aggregate&#13;&#10;where region_num = @region_num&#13;&#10;group by unit_name, unit_abbr&#13;&#10;order by unit_abbr">
         <SelectParameters>
             <asp:ControlParameter ControlID="DropDownList4" Name="region_num" PropertyName="SelectedValue" />
         </SelectParameters>
     </asp:SqlDataSource>

--------------------------------------------------Updted code
        if (!Page.IsPostBack)
        {
            try
            {
                this.DropDownList1.SelectedIndex = Convert.ToInt32(this.Session["MMselectedvalue"]);
                this.DropDownList2.SelectedIndex = Convert.ToInt32(this.Session["YYselectedvalue"]);
                this.DropDownList3.SelectedIndex = Convert.ToInt32(this.Session["Unitselectedvalue"]);
                this.DropDownList4.SelectedIndex = Convert.ToInt32(this.Session["Regionselectedvalue"]);

            }

            catch
            {
            }

        }

        else
        {
        }

0
 
LVL 2

Accepted Solution

by:
kyle_clark earned 2000 total points
ID: 16591359
You are correct about what is causing that behavior.  Since four is bound by the value of three, I would suggest writing a function called bindFour or whatever you would like.  When the page is loaded call the function to get the dataset to bind to four then use the session variable to set it's selected index.  Also, all of this is assuming that the data is coming out of the database in the same order everytime.  You may want to take the data and find its index in the list and then set the selectedindex to that.  Or that may be irrelevant.   Hope this helps.
0
 

Author Comment

by:jung1975
ID: 16591384
Thanks! Is there anyway you can provide some code examples? or tell me where I can find this kind of examples?
0
 
LVL 2

Expert Comment

by:kyle_clark
ID: 16591733
Google for data list binding, there are plenty of examples out there.  Check out the msdn library you can probably find it there.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question