首页 > 解决方案 > 在 ContentPage- ASP.NET 上设置标签值

问题描述

错误 IMG我在母版页中有标签来显示登录 ID。我想将值传递给最终将出现在所有内容页面中的标签。怎么做。

母版代码:

<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>

标签: c#asp.net

解决方案


你可以试试这个:

第 1 步:假设您有按钮并且单击按钮时您正在重定向用户。因此,在重定向之前,获取用户名并将其存储在会话中。像这样的东西:

 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

    }  
  } 

您的布局页面:

在这个Layout Page中,进入c#中的PageLoad事件(后端)

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

注意:登录的 C# 代码只是一个演示。使用您自己的代码。


推荐阅读