使用会话状态加载初始值 [英] Using Session State to Load Initial Value

查看:69
本文介绍了使用会话状态加载初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在使用会话状态在页面之间传递值时遇到问题.我的问题是这样的:我正在使用带有radmenu的母版页将值传输到自定义"页,并且希望将此值预加载到radcombobox中,以便将所有相应的数据加载到该页上.我已经按照预期的方式工作,但是,一旦页面加载了会话状态值,我就无法将组合框更改为另一个值.当我单击组合框以更改值并依次更新页面上的所有数据时,该值将恢复为最初在页面加载时加载的会话状态值,因此我无法更改页面上的数据使用组合框的页面,我只能在radmenu上单击另一个值来更新选择.我正在使用代码进行所有更改,并且在下面加载了代码.如果有任何令人困惑的地方,或者您需要更多信息,请告诉我.我敢肯定这可能是一个简单的修复程序,只是我所忽略的事情,但是我对此还比较陌生,我将不胜感激.提前谢谢.

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));
    }
}

推荐答案

首先,一旦选择组合框,您的Session["MenuValue"]便不会更改.有两种解决您的问题的方法.

1.如果您不想更改Session变量,则可以像这样更改代码并保留其余代码.

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.

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.如果要更改Session变量,请在ComboBox SelectedIndexChanged
下分配值



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

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.


感谢您的回复.它工作得很好.我使用了(!IsPostBack)方法,它可以按我的方式工作.

我知道这通常是简单的事情,而我却忽略了它.再次感谢您的帮助.
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.


这篇关于使用会话状态加载初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆