asp.net – UserControl有IsPostBack,但是Control没有

我正在尝试解决Visual Studio中的错误,the suggestion is to stop using UserControls and use Control instead..

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

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?

如何用Control替换UserControl?

系列

这个问题是正在进行的Stackoverflow系列中的一个,“模板化用户控件”:

> How to add a Templating to a UserControl?
> How to inherit from Control,rather than UserControl?
> UserControl has IsPostBack,but Control does not
> UserControl does not have public property named ContentTemplate
> How do i specify CodeFileBaseClass from web.config?

解决方法

Control有一个Page属性,它有一个IsPostback属性.这应该为您提供所需的价值.
public class MyControl : Control{
    protected override void OnInit( EventArgs e ){
        if( this.Page.IsPostBack ){
            // do something
        }
    }
}

MSDN Reference

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...