简体   繁体   English

如何让我的内容页面操作母版页上的控件?

[英]How can I make my content page manipulate controls on the master page?

My content pages allow me to manipulate data and so, their code includes error handling blocks. 我的内容页面允许我操作数据,因此,他们的代码包括错误处理块。 What I'd like to do is set the text value for several labels in a modal window on the master page in order to display the error message to the user: 我想要做的是在母版页的模态窗口中设置几个标签的文本值,以便向用户显示错误消息:

<!-- master page -->
<div runat="server" id="divModalBg" class="modalbg"> </div>
<div runat="server" id="divErrorModal" class="modal">
    <asp:Label runat="server" id="lblErrorTitle"></asp:Label>
    <asp:Label runat="server" id="lblErrorMessage"></asp:Label>
    <asp:Button runat="server" id="btnRetry" text="Retry" />
</div>

I'm not sure how I would set the values for those labels in my content page... 我不确定如何在我的内容页面中设置这些标签的值...

// Users.aspx.cs
protected void btnUpdate_Click(object sender, EventArgs e)
{
    try
    {
        // do some stuff
    }
    catch(Exception ex)
    {
        // lblErrorTitle = "Update User";
        // lblErrorMessage = String.Format("Error actioning request: [ {0} ]", ex.Message);
        // btnRetry just closes the modal (sets visible to false) so that
        // the user can try again
    }
}

So how can I access the controls on the master page so that I can do this? 那么如何访问母版页上的控件以便我可以这样做呢?

I'm thinking properties on the master page, set them onPageLoad... 我正在考虑母版页上的属性,将它们设置在PageLoad上......

EDIT 编辑
As per mason 's answer below, I've done this... 根据mason在下面的回答,我已经做到了......

// Default.master
// properties for error handling
public virtual Label ErrorTitle { get { return lblErrorAction; } }
public virtual Label ErrorMessage { get { return lblErrorMessage; } }
public virtual Panel ErrorBG { get { return pnlErrorBackground; } }
public virtual Panel ErrorModal { get { return pnlErrorModal; } }

Here's my directive: 这是我的指示:

<%@ MasterType VirtualPath="~/Default.master" %>

And here's my code file 这是我的代码文件

// Users.aspx.cs
private void deleteUser(int userid)
{
    ImajUser u = new ImajUser(userid, true);
    try
    {
        u.Delete();

        lblerr.Text = "User was deleted successfully.";
        lblerr.ForeColor = System.Drawing.Color.Green;
    }
    catch (Exception ex)
    {
        Page.Master.ErrorTitle.Text = "Delete User";
        Page.Master.ErrorMessage.Text = String.Format("Error actioning request: [ {0} ]", ex.Message);
        Page.Master.ErrorBG.Visible = true;
        Page.Master.ErrorModal.Visible = true;

    }

Now getting the following error for each of these controls: 现在,为每个控件获取以下错误:

'System.Web.UI.MasterPage' does not contain a definition for 'ErrorTitle' and no extension method 'ErrorTitle' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?) 'System.Web.UI.MasterPage'不包含'ErrorTitle'的定义,并且没有可以找到接受类型'System.Web.UI.MasterPage'的第一个参数的扩展方法'ErrorTitle'(你是否缺少using指令?或汇编参考?)

Although Thor's answer would work, there's a much better way. 虽然托尔的答案会起作用,但还有更好的方法。 Set the master type on the content page by putting this in the ASPX file. 通过将其放在ASPX文件中,在内容页面上设置主类型。 Obviously, replace the TypeName with the name of the class from your master page's code behind. 显然,将TypeName替换为主页后面代码中的类名。

<%@ MasterType TypeName="MyMasterClassName" %>

This makes your master page strongly typed accessible from the content page. 这使您可以从内容页面访问强类型的母版页。 Then, to access controls on the MasterPage (which are by default private or protected , not sure which) you'll need to use properties to expose them as public . 然后,要访问MasterPage上的控件(默认情况下是privateprotected ,不确定哪个),您需要使用属性将它们publicpublic So put this on your master page code behind... 所以把它放在你的母版页代码背后......

public virtual Label LblErrorMessage { get {return lblErrorMessage;}}

Then from your code behind, you can access it like this... 然后从您的代码后面,您可以像这样访问它...

Master.LblErrorMessage.Text="Hello, world!";

You can access any public methods in your master page using this kind of contruct: 您可以使用以下类型的构造访问母版页中的任何公共方法:

In page.aspx: 在page.aspx中:

if (Page.Master != null)
                     ((YourMasterPageType)Page.Master).SetError("your error txt");

In your master page: 在您的母版页中:

public void SetError(string errMsg)
        {
            ErrCtrl.Visible = true;
            ErrCtrl.Text(errMsg);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为我的所有页面(如Zebble for Xamarin中的默认页面)制作一个母版页面? - How can I make a master page for all of my pages like default page in Zebble for Xamarin? 如何在运行时更改页面的母版页? - How can I change my page's Master Page at runtime? 如何从嵌套母版页中的内容页访问主母版页中的控件 - How to access controls in Main Master page from content page in Nested Master Page 当我的控件和数据来自母版页时,如何将数据传递到服务器并将页面重定向到下一个内容页? - How can pass data to server and page redirected to next content page as my control and data is from master page? 内容控件必须是内容页面中的顶级控件或引用母版页的嵌套母版页 - Content controls have to be top-level controls in a content page or a nested master page that references a master page 如何使母版页中的 div 高度在内容页中展开? - How to make the height of a div within a master page expand in a content page? 如何在母版页上使内容页成为默认焦点 - How to make the Default focus in content page from master page 如何从母版页访问页面控件? - How to access page controls from master page? 如何在我的母版页中添加iframe - How can i add iframe in my master page 我如何从内容页面到母版页面加载创建javascript - how can i create javascript from content page to master page load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM