reusing user controls

G

Guest

Hello All,

I have a base usercontrol which has a datagrid and a virtual method to
populate the datagrid. I developed a child usercontrol which inherits the
base usercontrol and also overrides the virtual method.

I have a page on which I placed these usercontrols together. The base
usercontrol's datagrid gets populated fine however in the override method of
the child usercontrol it is throwing an exception when I am trying to bind
the datagrid which is present in the base usercontrol.

My question is if I inherit a usercontrol from a parent usercontrol, are not
all the UI elements (in this case datagrid) inherited? Why is the child
usercontrol unable to find the datagrid in the parent usercontrol? The
accessor for datagrid is protected so it should be visible right?

The reason why I want to inherit the base usercontrol is to reuse the
functionality so that all I need to do is override the virtual method...


Did my question make sense?

Thanks for your suggestions!!


The exception that I am getting is object reference not set to an
instance......

Below are the code snippets:

Base usercontrol

public class upcoming : System.Web.UI.UserControl
{
protected System.Web.UI.HtmlControls.HtmlTable MatchTable;
protected System.Web.UI.WebControls.DataGrid dgSchedules;


public virtual void GetUpcomingLeagueMatches()

{
try
{
// get news article

DataSet ds = new DataSet();
ds =dataLayer.GetUpcomingLeagueMatches();
dgSchedules.DataSource = ds;
dgSchedules.DataBind();

}
catch(Exception e)
{
// no articles to display

}
}

}


Child Usercontrol

public class Upcoming2020Matches : upcoming
{

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

public override void GetUpcomingLeagueMatches()
{
try
{
// get news article

DataSet ds = new DataSet();
ds = dataLayer.GetUpcoming2020Matches();
base.dgSchedules.DataSource = ds;
base.dgSchedules.DataBind();

}
catch
{
}
}
}
 
G

Guest

Hi,
try this modified code:
Base usercontrol
public class upcoming : System.Web.UI.UserControl
{
protected System.Web.UI.HtmlControls.HtmlTable MatchTable;
protected System.Web.UI.WebControls.DataGrid dgSchedules;
dgSchedules = new System.Web.UI.WebControls.DataGrid();

public virtual void GetUpcomingLeagueMatches()

{
try
{
// get news article

DataSet ds = new DataSet();
ds =dataLayer.GetUpcomingLeagueMatches();
dgSchedules.DataSource = ds;
dgSchedules.DataBind();

}
catch(Exception e)
{
// no articles to display

}
}

}


Child Usercontrol

public class Upcoming2020Matches : upcoming
{

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

public override void GetUpcomingLeagueMatches()
{
try
{
// get news article

DataSet ds = new DataSet();
ds = dataLayer.GetUpcoming2020Matches();
Upcoming2020Matches childinstance = new Upcoming2020Matches();
childinstance .dgSchedules.DataSource = ds;
childinstance .dgSchedules.DataBind();
}
catch
{
}
}
}
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top