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

 

You are viewing reply posted by: pandeyism one year ago.
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.