C# 如何从内容页面访问母版页上的 .Net 元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 16:26:35  来源:igfitidea点击:

How to access .Net element on Master page from a Content page?

提问by Eddie

Is it possible to access an element on a Master page from the page loaded within the ContentPlaceHolderfor the master?

是否可以从主页面中加载的页面访问主页面上的元素ContentPlaceHolder

I have a ListView that lists people's names in a navigation area on the Master page. I would like to update the ListView after a person has been added to the table that the ListView is data bound to. The ListViewcurrently does not update it's values until the cache is reloaded. We have found that just re-running the ListView.DataBind()will update a listview's contents. We have not been able to run the ListView.DataBind()on a page that uses the Master page.

我有一个 ListView,它在母版页的导航区域中列出了人们的姓名。我想在将一个人添加到 ListView 数据绑定到的表后更新 ListView。在ListView重新加载缓存之前,当前不会更新它的值。我们发现只要重新运行ListView.DataBind()将更新列表视图的内容。我们无法在ListView.DataBind()使用母版页的页面上运行 。

Below is a sample of what I wanted to do but a compiler error says

下面是我想做的一个示例,但编译器错误说

"PeopleListView does not exist in the current context"

“PeopleListView 在当前上下文中不存在”

GIS.master - Where ListView resides

GIS.master - ListView 所在的位置

...<asp:ListView ID="PeopleListView"...

GISInput_People.aspx - Uses GIS.master as it's master page

GISInput_People.aspx - 使用 GIS.master 作为母版页

GISInput_People.aspx.cs

GISInput_People.aspx.cs

AddNewPerson()
{
    // Add person to table
    ....

    // Update Person List
    PeopleListView.DataBind();
    ...
}

What would be the best way to resolve an issue like this in C# .Net?

在 C# .Net 中解决此类问题的最佳方法是什么?

采纳答案by palmsey

I believe you coulddo this by using this.Master.FindControl or something similar, but you probably shouldn't - it requires the content page to know too much about the structure of the master page.

我相信你可以通过使用 this.Master.FindControl 或类似的东西做到这一点,但你可能不应该 - 它要求内容页面对母版页的结构了解太多。

I would suggest another method, such as firing an event in the content area that the master could listen for and re-bind when fired.

我会建议另一种方法,例如在内容区域中触发一个事件,主控可以在触发时监听并重新绑定。

回答by Brandon Wood

Assuming your master page was named MyMaster:

假设您的母版页名为 MyMaster:

(Master as MyMaster).PeopleListView.DataBind();

Edit:since PeopleListView will be declared protected by default, you will either need to change this to public, or create a public property wrapper so that you can access it from your page.

编辑:由于默认情况下 PeopleListView 将被声明为受保护,因此您需要将其更改为 public,或者创建一个公共属性包装器,以便您可以从您的页面访问它。

回答by Kev

Assuming the control is called "PeopleListView" on the master page

假设该控件在母版页上称为“PeopleListView”

ListView peopleListView = (ListView)this.Master.FindControl("PeopleListView");
peopleListView.DataSource = [whatever];
peopleListView.DataBind();

But @palmseyis more correct, especially if your page could have the possibility of more than one master page. Decouple them and use an event.

但是@ palmsey更正确,特别是如果您的页面可能有多个母版页。将它们解耦并使用事件。

回答by Adam Carr

One think to remember is the following ASP.NET directive.

一个认为要记住的是以下 ASP.NET 指令。

<%@ MasterType attribute="value" [attribute="value"...] %>

MSDN Reference

MSDN 参考

It will help you when referencing this.Master by creating a strongly typed reference to the master page. You can then reference your ListView without needing to CAST.

通过创建对母版页的强类型引用,它将在引用 this.Master 时为您提供帮助。然后您可以引用您的 ListView 而无需 CAST。

回答by icaptan

you can access with the code this.Master.FindControl(ControlID) which control you wish. It returns the reference of the control, so that the changes are effective. about firing an event could not be possible each situation.

您可以使用代码 this.Master.FindControl(ControlID) 访问您想要的控件。它返回控件的引用,以便更改生效。不可能在每种情况下触发事件。

回答by BrainCoder

Option 1 :you can create public property of your master page control

选项 1:您可以创建母版页控件的公共属性

public TextBox PropMasterTextBox1
{
    get { return txtMasterBox1; }
    set { txtMasterBox1 = value; }
}

access it on content page like

在内容页面上访问它

Master.PropMasterTextBox1.Text="SomeString";

Option 2: on Master page:

选项 2:在母版页上:

public string SetMasterTextBox1Text
{  
    get { return txtMasterBox1.Text; }
    set { txtMasterBox1.Text = value; }
}

on Content Page:

在内容页面上:

Master.SetMasterTextBox1Text="someText";

option 3 : you can create some public method that works for you

选项 3:您可以创建一些适合您的公共方法



these approach is not so useful but it helps if you just want to use some limited and predefined control

这些方法不是很有用,但如果您只想使用一些有限和预定义的控件,它会有所帮助