[英]How to use a Page variable in User Control?
I have a page with a private variable: AccountData
. 我有一个带有私有变量的页面:
AccountData
。
This handles all of the account data and I save it in the viewstate between postbacks. 这处理所有帐户数据,我将它们保存在回发之间的viewstate中 。
I have a User Control named AccountFavorites
that renders all of the account Favorite topics. 我有一个名为
AccountFavorites
的用户控件,该控件呈现所有帐户“收藏夹”主题。
When a user decides he wants to cancel one favorite topic he clicks on an asp:LinkButton
and it posts back to the server to change the value in the Database. 当用户决定要取消一个喜欢的主题时,他单击
asp:LinkButton
并将其回发到服务器以更改数据库中的值。
However, the Control doesn't know what AccountData
is, only the page does. 但是,控件不知道什么是
AccountData
,只有页面知道。
How can I use the AccountData
in a postback of the Control? 如何在控件的回发中使用
AccountData
?
Can I make the postback go to the page instead? 我可以转回该页面吗?
<myprefix:AccountFavorites runat="server" id="TheAccountFavorites" />
In your page set the info... 在您的页面中设置信息...
protected void Page_Load(object sender, EventArgs e)
{
TheAccountFavorites.TheAccountData=MyAccountData;//assuming you'll load up MyAccountData
}
In your control, expose AccountData as a public property. 在您的控件中,将AccountData公开为公共属性。 This will allow you to access it from the page.
这将允许您从页面访问它。
public AccountData TheAccountData {get;set;}
This is assuming AccountData
was the type name and not something else, like a string. 假设
AccountData
是类型名称,而不是其他名称,例如字符串。 But you can change AccountData to string and it will work. 但是您可以将AccountData更改为字符串,它将起作用。
Since it is page that owns/parents the user control, it is preferable to for the page to know about the specifics of the control, not the other way around. 由于是拥有/父用户控件的页面,因此最好让页面了解控件的详细信息,而不是相反。 Add
AccountData
property to the control and in the page's Page_Load
do: 将
AccountData
属性添加到控件,并在页面的Page_Load
执行以下操作:
uc.AccountData = AccountData;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.