简体   繁体   中英

how to use session to passing values between page c# ASP.NET web Forms

I'm a newbie in C# web api and I want to pass values between pages by using Session .
Here is my code.

page1.aspx

<asp:Label ID="lb_front" runat="server"></asp:Label>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:Button ID="bt1" runat="server" OnClick="bt1_Click" />

page1.aspx.cs

protected void bt1_Click(object sender, EventArgs e)
{
    lb_front.Text = txt1.Text;
    Session["user"] = txt1.Text;
}

page2.aspx

<asp:Label ID="lb_top" runat="server"></asp:Label>

page2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    lb_top.Text = "<img src='./Images_top/'"+ Session["user"]+"'.jpg'></img>";
    lb_top.Text += Session["user"];
}

I want to use the session value in the path to display an image on the label, but this isn't working.

You have to set the image path dynamically like:

page2.aspx

<asp:Image runat="server" ID="image" />
<asp:Label ID="lb_top" runat="server"></asp:Label>

page2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    string sessionValue = (string)Session["user"];

    image.ImageUrl = "../Images_top/"+ sessionValue +".jpg"; // set image url/src from session

    lb_top.Text += sessionValue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM