Inherited class giving null exceptions
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
精彩评论