Click here to Skip to main content
15,886,834 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello,

I am having an issue with using Session State to pass a value between pages. My issue is this: I am using a master page with a radmenu to transfer a value to my Customization page, and I want this value to be preloaded into my radcombobox so that it loads all of the corresponding data onto the page. I have this working the way it is supposed to, however, once the pageloads with the session state value I am unable to change the combobox to another value. When I click on the combobox to change the value and in turn update all of the data on the page the value reverts back to the session state value that was initially loaded on the page load and therefore I can't change the data on the page using the combobox, I can only click another value on my radmenu to update the selection. I am using code behind to do all of my changes, and I have my code behind loaded below. If anything is confusing or you need more information please let me know. I'm sure that it is probably an easy fix and something that I am just overlooking, but I am relatively new to this and I would appreciate any help. Thanks in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Pages_HatCustomizerPage : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        string MenuValue = Session["MenuValue"].ToString();
        if (MenuValue == null)
        {
            HatStyleComboBox.SelectedValue = "1";
        }
        else
        {
            HatStyleComboBox.SelectedValue = MenuValue;
        }
        
        HatComboImageChange();
        DescriptionChange();
        SizeChange();
    }
    protected void HatColorPanelBar_ItemClick(object sender, Telerik.Web.UI.RadPanelBarEventArgs e)
    {
        HatChangeInit();
    }
    protected void HatChangeInit()
    {
        string imgPathFrontAngle;
        imgPathFrontAngle = "~/Images/Hats/" + HatColorPanelBar.SelectedItem.Value + ".png";
        string imgPathFront;
        imgPathFront = "~/Images/Hats/" + HatColorPanelBar.SelectedItem.Value + "Front.png";
        string imgPathBack;
        imgPathBack = "~/Images/Hats/" + HatColorPanelBar.SelectedItem.Value + "Back.png";
        if (ViewSelector.SelectedValue == "1")
        {
            MainHatImage.ImageUrl = imgPathFrontAngle;
            LeftHatImage.ImageUrl = imgPathFront;
            RightHatImage.ImageUrl = imgPathBack;
        }
        if (ViewSelector.SelectedValue == "2")
        {
            MainHatImage.ImageUrl = imgPathFront;
            LeftHatImage.ImageUrl = imgPathFrontAngle;
            RightHatImage.ImageUrl = imgPathBack;
        }
        if (ViewSelector.SelectedValue == "3")
        {
            MainHatImage.ImageUrl = imgPathBack;
            LeftHatImage.ImageUrl = imgPathFrontAngle;
            RightHatImage.ImageUrl = imgPathFront;
        }
    }
    protected void HatStyleComboBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
            string MenuValue = HatStyleComboBox.SelectedItem.Value;
            HatStyleComboBox.SelectedValue = MenuValue;
            HatComboImageChange();
            DescriptionChange();
            SizeChange();
    }
    protected void HatComboImageChange()
    {
        
        
        DataView dvSql2 = (DataView)SqlDataSource3.Select(DataSourceSelectArguments.Empty);
        string ComboSelect = dvSql2[0][3].ToString();
        string imgPathFrontAngle;
        imgPathFrontAngle = "~/Images/Hats/" + ComboSelect + ".png";
        string imgPathFront;
        imgPathFront = "~/Images/Hats/" + ComboSelect + "Front.png";
        string imgPathBack;
        imgPathBack = "~/Images/Hats/" + ComboSelect + "Back.png";
        if (ViewSelector.SelectedValue == "1")
        {
            MainHatImage.ImageUrl = imgPathFrontAngle;
            LeftHatImage.ImageUrl = imgPathFront;
            RightHatImage.ImageUrl = imgPathBack;
        }
        if (ViewSelector.SelectedValue == "2")
        {
            MainHatImage.ImageUrl = imgPathFront;
            LeftHatImage.ImageUrl = imgPathFrontAngle;
            RightHatImage.ImageUrl = imgPathBack;
        }
        if (ViewSelector.SelectedValue == "3")
        {
            MainHatImage.ImageUrl = imgPathBack;
            LeftHatImage.ImageUrl = imgPathFrontAngle;
            RightHatImage.ImageUrl = imgPathFront;
        }
    }
    protected void DescriptionChange()
    {
        DataView dvSql3 = (DataView)SqlDataSource3.Select(DataSourceSelectArguments.Empty);
        HatStyleLabel.Text = dvSql3[0][2].ToString();
        HatSerialLabel.Text = dvSql3[0][1].ToString();
        HatDescriptionLabel.Text = dvSql3[0][4].ToString();
    }
    protected void SizeChange()
    {
        DataView dvSql4 = (DataView)SqlDataSource4.Select(DataSourceSelectArguments.Empty);
        if (dvSql4.Count == 1)
        {
            SizeLabel1.Text = dvSql4[0][2].ToString();
            SizeLabel2.Text = "";
            SizeLabel3.Text = "";
        }
        if (dvSql4.Count == 2)
        {
            SizeLabel1.Text = dvSql4[0][2].ToString();
            SizeLabel2.Text = dvSql4[1][2].ToString();
            SizeLabel3.Text = "";
        }
        if (dvSql4.Count == 3)
        {
            SizeLabel1.Text = dvSql4[0][2].ToString();
            SizeLabel2.Text = dvSql4[1][2].ToString();
            SizeLabel3.Text = dvSql4[2][2].ToString();
        }
    }
    protected void ViewSelector_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (HatColorPanelBar.SelectedItem == null)
        {
            HatComboImageChange();
        }
        else
        {
            HatChangeInit();
        }
    }
    protected void AddLogoButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("LogoCustomizer.aspx?HatSelectionID=" + Server.UrlEncode(HatStyleComboBox.SelectedItem.Value) + "&HatSelection=" + Server.UrlEncode(HatColorPanelBar.SelectedItem.Value));
    }
}
Posted

First of all your Session["MenuValue"] is not changed once you select the combobox. There are two solution for your problem.

1. If you don't want to change your Session variable then change your code like this and keep the rest of your code.

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack) // Changed Code
       {
           string MenuValue = Session["MenuValue"].ToString();
           if (MenuValue == null)
           {
               HatStyleComboBox.SelectedValue = "1";
           }
           else
           {
               HatStyleComboBox.SelectedValue = MenuValue;
           }

           HatComboImageChange();
           DescriptionChange();
           SizeChange();
       }
   }

protected void HatStyleComboBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        //string MenuValue = HatStyleComboBox.SelectedItem.Value; // No need to assign
        //HatStyleComboBox.SelectedValue = MenuValue; // No need to assign
        HatComboImageChange();
                DescriptionChange();
        SizeChange();
    }


2. If you want to change your Session variable, then assign value under ComboBox SelectedIndexChanged

C#
protected void HatStyleComboBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        //string MenuValue = HatStyleComboBox.SelectedItem.Value; // No need to have it
        Session["MenuValue"] = HatStyleComboBox.SelectedValue; // Changed Code
        HatComboImageChange();
                DescriptionChange();
        SizeChange();
    }


I hope this will help you well.
 
Share this answer
 
v3
Comments
saxenaabhi6 11-May-11 21:25pm    
my 5 to what i think is the best answer.
Wonde Tadesse 6-Dec-11 10:40am    
Thanks
Thank you for your response. It worked perfectly. I used the (!IsPostBack) method and it works just as I wanted.

I knew it was going to be something simple that I overlooked, it usually is. Once again thank you for your help.
 
Share this answer
 
Comments
Wonde Tadesse 11-May-11 23:02pm    
You're most welcome. But you should add the comment under my solution by clicking <color="blue">Add Comment<color>

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900