Send UserName to other page from Login page in ASP.Net

Last Reply one year ago By pandeyism

Posted one year ago

Hello friends,

I have been trying to redirect username and user email from the Login page to Page2.

This is what the Login page looks like and it works.

       function register(connect) {
           $("#Start").click(function () {
               var name = $("#userName").val();
               var email = $('#userEmail').val();
               if (name.length > 0 && email.length > 0) {
                   window.location.href = "Page2.aspx";
               }
               else {
                   alert("Enter username or password details");
               }
           });
       }

Then in code behind of the Login page I added this:

protected void Start_Click(object sender, EventArgs e)
{
    string name = userName.ToString();
    Session["Name"] = name.ToString();
}

In Page2 code behind I added this code but the useName and userEmail don’t show up. I added Alert box to test if the username gets redirected but no luck. What am I doing wrong?

   protected void Page_Load(object sender, EventArgs e)
   {
       if (!this.Page.User.Identity.IsAuthenticated)
       {
           if (Session["Name"] != null)
           {
               Label1.Text = Session["Name"].ToString();
               ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Label1.Text.ToString() + "');", true);
           }
       }
       else  
       {
           FormsAuthentication.RedirectToLoginPage();
       }
   }

 

Posted one year ago

Hi power,

Please refer below sample.

HTML

Default.aspx

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    function register() {
        var name = $("#userName").val();
        var email = $('#userEmail').val();
        if (name.length > 0 && email.length > 0) {
            return true;
        }
        else {
            alert("Enter username or password details");
            return false;
        }
    }
</script>
<div>
    Name :
    <asp:TextBox ID="userName" runat="server" />
    <br />
    Email :
    <asp:TextBox ID="userEmail" runat="server" />
    <asp:Button Text="Submit" runat="server" OnClientClick="return register();" OnClick="Start_Click" />
</div>

Page2.aspx

<div>
    <asp:Label ID="Label1" runat="server" />
</div>

Code

Deafult.aspx.cs

protected void Start_Click(object sender, EventArgs e)
{
    string name = userName.Text.ToString();
    Session["Name"] = name;
    Response.Redirect("Page2.aspx");
}

Page2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["Name"] != null)
    {
        Label1.Text = Session["Name"].ToString();
    }
}

Screenshot


Posted one year ago

Hi pandeyism,

The sample you provided me triggers error as follows.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  

Then I googled the error and some people suggest to add 'EnableEventValidation="false"' and I added it in my Page 2 and it worked. Since you know more, I have one question for you. Is it a good idea to add 'EnableEventValidation="false"' in Page2? Is there a better solution to fix this type of error?

Thanks for the help. 


Posted one year ago

Hi power,

There are two way to solve this error.

You can set it in the @Page Directive of the page in the following way.

<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation = "false" 

You can set it in the Web.Config in the following way.

<pages enableEventValidation ="false"></pages>

enableEventValidation will set in all the pages.