How to pass one session vale one page to another in asp.net c#


In this article, I am passing the session value according to username in the URL.

Source Code:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <fieldset style="width:550px">
    <legend>Sign In </legend> 
        <table class="style1">
   

            <tr>
                <td align="right">
                    User Name</td>
                <td>
                    <asp:TextBox ID="txtusername" runat="server" Width="180px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Password</td>
                <td>
                    <asp:TextBox ID="txtpwd" runat="server" Width="180px" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnsignin" runat="server" onclick="btnsignin_Click"
                        Text="Sign in" Width="180px" />
                </td>
            </tr>
        </table>
        </fieldset>
    </div>
    </form>
</body>
</html>



Code behind (c#):-

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;

public partial class sessionvalue : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsignin_Click(object sender, EventArgs e)
    {
       
        if (txtusername.Text == "Codesoluation" && txtpwd.Text == "12345678")
        {
            Session["username"] = txtusername.Text;
            string username = Session["username"].ToString();
            Response.Redirect("copytextbox.aspx?username=" + username);
        }
        else
        {
            string message = "Invalid Username or Password. Please relogin.";
            ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
        }
    }
}


Output:-



How to pass one session vale one page to another in asp.net c# How to pass one session vale one page to another in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 11:30:00 AM Rating: 5

No comments:

Powered by Blogger.