You must Sign In to post a response.
  • Category: .NET

    How to show username and password in one label control after redirecting from one page to another

    hi all,
    I have a small application in that login form with user id and password is there so after entering userid and password and click on a button it has to redirect to next page so in next page one button and label control is there so when u click on button the username and password what we have entered in first page should show in second page in label control below is my code where i am wrong help me

    webform1.aspx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

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

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    Session["uname"] = TextBox1.Text;
    Session["password"] = TextBox2.Text;
    if (TextBox1.Text == "ravi" && TextBox2.Text == "123456")
    {

    Response.Redirect("~/WebForm2.aspx");
    }
    }
    }
    }


    webform2.aspx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication4
    {
    public partial class WebForm2 : System.Web.UI.Page
    {
    int i=1;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    Label1.Text = Session["uname"].ToString();
    Label1.Text = Session["password"].ToString();

    //Label1.Text = Request.QueryString["[0][1]"].ToString();
    ////Label1.Text = Request.QueryString["password"].ToString();
    }
    }
    }
  • #765772
    Hello,

    You can concatenate the username and password as shown below.

    Label1.Text=Session["uname"].ToString()+" " +Session["password"].ToString();
    OR
    Label1.Text="Username:- " +Session["uname"].ToString()+" Password :-" +Session["password"].ToString();

    Hope this will resolve your issue.

  • #765774
    Hi
    Your code is working fine.
    do one checking is nullorempty before assign session value from the textbox control.

    otherwise pass query string for this,

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765784
    Hi,

    Your code looks like good, my suggestion is debug the code and check whether session having value or not, don't assign session value without checking null case.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765795
    Hai VELIDIRAVIRAM,
    After redirecting to other page, as you are sending the values through sessions, so you can get them in page_load event.
    Your page 2 code needs to be changed:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication4
    {
    public partial class WebForm2 : System.Web.UI.Page
    {
    int i=1;
    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.Text =(Session["uname"] !=null)? Session["uname"].ToString(): string.Empty ;
    Label1.Text = (Session["password"] !=null)? Session["password"].ToString(): string.Empty ;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    // no code is required here
    }
    }
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #765803
    Unlike Asp.net there are very limited ways in MVC to transfer variable from one page to another page.
    you may use ViewData, ViewBag and TempData here.
    the controller allocates in the ViewBag (which is a wrapper around ViewData), and passes ViewBag/ViewData to controller action (via property) and the view (via ViewContext). if the caontroller calls
    return View(view,model)
    then the model is placed in ViewBag.Model property (ViewData["Model"}) and in the view, Model is just a typed wrapper for ViewBag.Model.
    using HttpContext, Session, hidden fields, databases is all the same as WebForms. using encryption, serialization and hidden fields you can emulate webforms evil viewstate (which is often larger tha the pages html).

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765977
    Hello,

    Please try with the below code :-

    webform2.aspx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication4
    {
    public partial class WebForm2 : System.Web.UI.Page
    {
    int i=1;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    Label1.Text = "Username :- " + Convert.ToString(Session["uname"]) + " " + "Password :- " + Convert.ToString(Session["password"]) ;


    }
    }
    }

    Hope it will solve your problem.

    Thanks


Sign In to post your comments