UserControl has IsPostBack, but Control does not

落花浮王杯 提交于 2019-12-29 01:53:08

问题


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

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)

becomes

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

Except that there is no Control.IsPostBack?

How do i replace UserControl with Control?

Series

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

  • 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?

回答1:


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 Reference



来源:https://stackoverflow.com/questions/12321621/usercontrol-has-ispostback-but-control-does-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!