UserControl 有 IsPostBack,但 Control 没有UserControl、IsPostBack、Control

由网友(既然无缘何须誓言)分享简介:我正在尝试解决 Visual Studio 中的一个错误,the建议是停止使用 UserControls 并改用 Control..i'm trying to solve a bug in Visual Studio, the suggestion is to stop using UserControls and...

我正在尝试解决 Visual Studio 中的一个错误,the建议是停止使用 UserControls 并改用 Control..

i'm trying to solve a bug in Visual Studio, the suggestion is to stop using UserControls and use Control instead..

所以我将我所有的 UserControl 转换为 Control,例如:

So i'm converting all my UserControl into just Control, e.g.:

public partial class Controls_UserManagement_GroupManager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)

变成

public partial class Controls_UserManagement_GroupManager : System.Web.UI.Control
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)

除了没有Control.IsPostBack?

如何将 UserControl 替换为 Control?

这个问题是 Stackoverflow 系列中的一个,模板化用户控件":

This question is one in the ongoing Stackoverflow series, "Templating user controls":

如何向用户控件添加模板?如何继承自Control,而不是继承自UserControl?UserControl 有 IsPostBack,但 Control 没有UserControl 没有名为 ContentTemplate 的公共属性如何从 web.config 指定 CodeFileBaseClass?

推荐答案

Control 有一个 Page 属性,它有一个 IsPostback 属性.这应该为您提供所需的价值.

Control has a Page property, which has an IsPostback property. This should give you the value you need.

public class MyControl : Control{
    protected override void OnInit( EventArgs e ){
        if( this.Page.IsPostBack ){
            // do something
        }
    }
}

MSDN 参考

阅读全文

相关推荐

最新文章