-1

Error IMG I have label in master page to display login id. I want to pass the value to that label which will eventually appear in all the content pages. How to do it.

Masterpage code:

<ul class="nav navbar-nav navbar-right">
         <li><a href="#login_form" id="login_pop" runat="server"><span class="glyphicon glyphicon-user">
         </span>
         <asp:Label ID="lblUserName" runat="server" Text="">
         </asp:Label> </a>
         </li>
         </ul>
santosh
  • 71
  • 8

1 Answers1

0

You can try this:

Step 1: Suppose you have button and on button click you are redirecting the users. So before redirection, take the username and store it in the session. Something like this:

 protected void Button1_Click(object sender, EventArgs e) {  
    SqlConnection con = new SqlConnection(@YourConnectionString");  
    SqlCommand cmd = new SqlCommand("select * from tbl_data where username=@username and word=@word", con);  // use Stored Procedure to avoid SQL injection hacks
    cmd.Parameters.AddWithValue("@username", TextBox1.Text);  

    SqlDataAdapter sda = new SqlDataAdapter(cmd);  
    DataTable dt = new DataTable();  
    sda.Fill(dt);  
    con.Open();  
    int i = cmd.ExecuteNonQuery();  
    con.Close();  

    if (dt.Rows.Count > 0) { 
        Session["username"] = TextBox1.Text; // store the value of username in Session
        Response.Redirect("Redirectform.aspx");  
        
    } else {  
       // something else

    }  
  } 

Your LayoutPage:

In this Layout Page, go to the PageLoad event in c# (backend)

    protected void Page_Load(object sender, EventArgs e)
    {
          lblUserName.Text = Session["username].ToString();
    }

Note: The C# code for Login is just a demo. Use your own code.

Rohan Rao
  • 2,505
  • 3
  • 19
  • 39
  • Thanks. I have already done everything that you have mentioned. The only issue i am having is passing that session value captured to a label in master page. When i try to pass session value to label in master page, i am getting the error, "the name lblusername does not exists in current context". – santosh Aug 17 '20 at 12:23
  • It is lblUserName not lblusername... Asp controls are case sensitive – Rohan Rao Aug 17 '20 at 12:38
  • Added, Please check – santosh Aug 17 '20 at 12:51
  • @santosh do one thing. Remove the code of label and then re-write it. And then build it. It's not your fault it is the bug of Visual Studio. (I have also come across this few times) – Rohan Rao Aug 17 '20 at 14:15