开发者

Inherited class giving null exceptions

开发者 https://www.devze.com 2022-12-21 08:29 出处:网络
Here is my simple class, where i inherit from UserControl. public class Test:System.Web.UI.UserControl

Here is my simple class, where i inherit from UserControl.

public class Test:System.Web.UI.UserControl
{
    public void BindGrid()
    {
        Response.Write(IsPostBack);
    }
}

Which i call from the Page_Load event

protected void Page_Load(object sender, EventArgs e)
{
  new Test().BindGrid();
}

But i get a null reference exception if i tr开发者_StackOverflow社区y to call any of UserControl's inerited properies (ispostback, request etc etc)

Any ideas why this is happening?

Edit:

It seems to work if replace the 'ispostback' response.write() inside the OnLoad method of Test?


Properties like IsPostback depend on the Page where the user control is hosted, well, should be hosted, as your example does not add the user control to the Controls collection of the Page, which explains the crash.


is this a true UserControl? else i would inherit control instead, is the usercontrol placed on a asp.net page when you call the functions or is it just instated and called?


Your class is not processed as the user control. Hence there is no "IsPostBack" and another properties. They are just null. You should register your class on the page so that this class will be the part aspx page

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号