会话状态有问题吗? [英] Problem in session state?

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

问题描述

我的项目布局:

2 Pages.aspx

4个按钮,bt1,bt2,bt3,bt4

3图像控制im1,im2,im3

3标签



我正在使用处理程序从db中检索6个图像。当页面加载3个图像及其名称显示在页面中..



Layout of my project:
2 Pages.aspx
4 buttons, bt1, bt2, bt3,bt4
3 image control im1, im2,im3
3 Labels

I am retrieving 6 image from db using handler. When the page loads 3 image along with their name is shown in the page..

page_load
{
image1.imageurl="handler.aspx?pid=0";
image2.imageurl="handler.aspx?pid=1";
image3.imageurl="handler.aspx?pid=2";
}



当用户再次点击bt4时,另外3张图片会在同一位置加载以替换之前的数据


and when again a user clicks on bt4 another 3 images loads in the same place replacing the previous data

bt4_click
{
image1.imageurl="handler.aspx?pid=3";
image2.imageurl="handler.aspx?pid=4";
image3.imageurl="handler.aspx?pid=5";
}





现在我希望每当用户点击bt1(比方说)图像(im1)以及名称时被传递到另一页..

i有这样的代码:



Now i want that whenever a user clicks on bt1 (say) the image(im1) along with the name is passed on to another page..
i have written codes like this:

//storing values
 protected void button1_click(object sender, EventArgs e)
    {
        Session["img"] = Image1.ImageUrl;
        Session["bk1"] = Label1.Text;
        Response.Redirect("Details.aspx");
    }
   
    protected void button2_click(object sender, EventArgs e)
    {
        Session["img"] = Image2.ImageUrl;
        Session["bk1"] = Label2.Text;
        Response.Redirect("Details.aspx");
    }
    protected void button3_click(object sender, EventArgs e)
    {
        Session["img"] = Image3.ImageUrl;
        Session["bk1"] = Label3.Text;
        Response.Redirect("Details.aspx");
    }
}

// calling like this
 if (Session["img"] != null && Session["bk1"] != null)
        {
            Image1.ImageUrl = Session["img"].ToString();
            TextBox1.Text = Session["bk1"].ToString();
        }

protected void Page_Load(object sender, EventArgs e)
    {
         
        string strConn = "Data Source=SWAYAM;Initial Catalog=Swayam;Integrated Security=SSPI; ";
        SqlConnection con = new SqlConnection(strConn);
        con.Open();
        //panel1
        string st = "select top 1 name,author,price from image4 order by Row_id desc";
        SqlCommand cmd = new SqlCommand(st, con);
        SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        string name = "";
        string author = "";
        string price = "";
        
        if (sdr.Read())
        {
            name = sdr["name"].ToString();
            author = sdr["author"].ToString();
            price = sdr["price"].ToString();
            
        }
        sdr.Close();
        con.Close();

        //panel2
        SqlConnection con2 = new SqlConnection(strConn);
        con2.Open();
        string st2="Select name, author, price from image4 where Row_id=(select max(Row_id)-1 from image4)";
        SqlCommand cmd2 = new SqlCommand(st2, con2);
        SqlDataReader sdr2 = cmd2.ExecuteReader(CommandBehavior.CloseConnection);
        string name2="";
        string author2="";
        string price2="";

        if (sdr2.Read())
        {
            name2 = sdr2["name"].ToString();
            author2 = sdr2["author"].ToString();
            price2 = sdr2["price"].ToString();
        }
        sdr2.Close();
        con2.Close();

        //panel3
        SqlConnection con3 = new SqlConnection(strConn);
        con3.Open();
        string st3 = "Select name, author, price from image4 where Row_id=(select max(Row_id)-2 from image4)";
        SqlCommand cmd3 = new SqlCommand(st3, con3);
        SqlDataReader sdr3 = cmd3.ExecuteReader(CommandBehavior.CloseConnection);
        string name3 = "";
        string author3 = "";
        string price3 = "";

        if (sdr3.Read())
        {
            name3 = sdr3["name"].ToString();
            author3 = sdr3["author"].ToString();
            price3 = sdr3["price"].ToString();
        }
        sdr3.Close();
        con3.Close();

        Label1.Text = name;
        Label2.Text = author;
        Label3.Text =  "Rs."+ price;
        Label4.Text = name2;
        Label5.Text = author2;
        Label6.Text = "Rs."+ price2;
        Label7.Text = name3;
        Label8.Text = author3;
        Label9.Text = "Rs."+ price3;
        Image1.ImageUrl="~/Image handler.ashx?pid=0";
        Image2.ImageUrl = "~/Image handler.ashx?pid=1";
        Image3.ImageUrl = "~/Image handler.ashx?pid=2";
}



我的问题是会话只携带前三个图像的值,标签不是bt4_click加载的新数据????????????? b b b b b b b b b b b b b b b b b''''''''''''''''''''''''''''''''' ..

但是当由于bt4_click而改变图像时,前三个图像的存储数据被传递。不是新的..




任何建议或帮助表示赞赏


My problem here is that session is only carrying the values of the first three images and labels not the new data which is loaded by bt4_click???

The problem is that session is storing the imageurl and the data of the first three images that are displayed on page load...
but when the images are changed due to bt4_click the stored data of first three images is passed on. not of the new ones..


any suggestions or help is appreciated

推荐答案

特别感谢 Sunasara Imdadhusen



这里缺少我的page_load

A special thanks to Sunasara Imdadhusen

Here what was lacking in my page_load
void page_load()
{
if(!ispostback)
{
//code
}





现在工作正常



:)



it works fine now

:)


这篇关于会话状态有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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