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

时间:2020-11-29 21:12:34

Is it possible to access an element on a Master page from the page loaded within the ContentPlaceHolder for 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 ListView currently 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的内容。我们无法在使用Master页面的页面上运行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

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中解决此类问题的最佳方法是什么?

6 个解决方案

#1


16  

I believe you could do 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.

我建议使用另一种方法,例如在内容区域中触发主人可以侦听的事件,并在触发时重新绑定。

#2


3  

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

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

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

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

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

#3


2  

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

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

#4


1  

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

人们想要记住的是以下ASP.NET指令。

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

MSDN Reference

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。

#5


0  

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)进行访问。它返回控件的引用,以便更改生效。关于发射一个事件是不可能的每种情况。

#6


-1  

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,或者创建公共属性包装器以便您可以从页面访问它。

#1


16  

I believe you could do 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.

我建议使用另一种方法,例如在内容区域中触发主人可以侦听的事件,并在触发时重新绑定。

#2


3  

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

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

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

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

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

#3


2  

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

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

#4


1  

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

人们想要记住的是以下ASP.NET指令。

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

MSDN Reference

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。

#5


0  

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)进行访问。它返回控件的引用,以便更改生效。关于发射一个事件是不可能的每种情况。

#6


-1  

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,或者创建公共属性包装器以便您可以从页面访问它。