Crna Krv Crna Krv - 13 days ago 7
ASP.NET (C#) Question

Preventing a control from loading

How do I prevent a control that is displayed in the ASPX page from loading ?

Basically, I have an ASP.NET web form with a single

ObjectDataSource
that is feeding 2 GridViews (let's call them
gvSystemA
and
gvSystemB
). That
ObjectDataSource
is getting its data from a SQL Server stored procedure.

That stored procedure extracts data from either System A or System B, depending on the value of the "System" parameter. These 2 systems are completely different so the output from that stored procedure is different as well.

So I basically want to display
gvSystemA
, or
gvSystemB
, but not both. The
ObjectDataSource
and both Gridviews are on the aspx page. How do I prevent gvSystemA or gvSystemB from loading from code-behind ? I tried several things but it still tries to load both Gridviews:

protected void Page_Load(object sender, EventArgs e)
{
if ((string)HttpContext.Current.Session["System"] == "sysA")
{
gvSystemB.Enabled = false;
}
else
{
gvSystemA.Enabled = false;
}
}


And I get the following error (as the DataFields from gvSystemA are different from gvSystemB):


A field or property with the name 'TELEPHONENUM' was not found on the selected data source

Answer

You need to set Visible property not Enabled. When you set Visible=false, then it should be hidden from UI and shouldn't do Data binding.

protected void Page_Load(object sender, EventArgs e)
{

    if ((string)HttpContext.Current.Session["System"] == "sysA")
    {
        gvSystemB.Visible = false;
    }
    else
    {
        gvSystemA.Visible = false;
    }

}