[英]ASP.net Call function in Default.aspx.cs from the Site.Master.cs class
[英]How do I send a string or value from the Site.Master.cs to my Default.aspx.cs to the value/string in a label/textbox?
謝謝閱讀。 我想設置在Label1(在Site.Master.cs)中獲得的值,以傳遞到Label2(在Default.aspx.cs中)。 我想在以后的項目中使用它作為值來交叉引用SQL db中的數據。
我似乎無法繞開這一步。
在Site.Master.cs中
namespace flexpoolWeb
{
public partial class SiteMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
var windowsIdentity = HttpContext.Current.User.Identity;
if (windowsIdentity == null)
throw new InvalidOperationException("WindowsIdentity is null");
string nameWithoutDomain = HttpContext.Current.User.Identity.Name.Split('\\').Last();
//serverside code httpContext haalt ingelogde gebruiker op in de sessie.
Label1.Text = String.Format(nameWithoutDomain);
Label2.Text = MyMaster.UserName;
}
public string UserName
{
get { return Label1.Text; }
set { Label1.Text = value; }
}
}
}
在Default.aspx.cs中:
namespace flexpoolWeb
{
public partial class _Default : System.Web.UI.Page //instead of : Page
{
private SiteMaster MyMaster => ((SiteMaster)Master);
}
}
提前致謝。
編輯:從Skye MacMaster添加了答案
您的母版頁源自MasterPage。 因此,您需要將MasterPage的Cast Master鍵入您的母版名稱。 我猜是Site或SiteMaster。 另外,我通常會創建一個私有屬性,以避免產生一堆難看的類型轉換。
public partial class Default : System.Web.UI.Page
{
private SiteMaster MyMaster => ((SiteMaster) Master);
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = MyMaster.UserName;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.