Programatically changing gridview EmptyDataTemplate

Posted on 2008-10-15
Last Modified: 2013-12-17
I have a page that includes a gridview control showing a list of contacts associated with a selected record.  I have used the footer of the gridview to allow users to add new contacts if they have the relevant permissions.  If they do not have the relevant rights, I set the footer to invisible so they can no longer access the add contact controls.  This seems to work fine.

I'm now trying to replicate this funtionality in the empty data template so users can add records if there are no associated contacts aready.  This all works fine, except I have been unable to hide the add record futionality if the users don't have the correct privillages. I have set the empty data template including the add controls in asp page, and thought I could use the attached code snippet to change the EmptyDataTemplate.  This code gets called ok, but the gridview still includes the original EmptyDataTemplate when it is rendered.

Am i going about this the right way?
//Replace add contact funtionality in the empty data template as user doesn't have rights
                TemplateBuilder tmpEmptyDataTemplate = new TemplateBuilder();
                tmpEmptyDataTemplate.AppendLiteralString("No contacts are assigned");
                gvAssociatedUsers.EmptyDataTemplate = tmpEmptyDataTemplate;

Open in new window

Question by:phillarby
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 22719764
So I haven't walked through this in code, so I'm not exactly sure why what you've done isn't working. I have suspicion that it may be due to the call to DataBind(), but would need to try it.

That said, what about an alternative? How about wrapping all of the items you want to be able to hide in your emptydatatemplate into something like a Panel control. Then in your OnDataBind or OnPreRender (I think both would work - the first is probably cleaner), do your permissions check, and if they don't have rights, then set Panel.Visible to false. That way, the server side content of your emptydatatemplate is consistent, and just its state changes. Would this work?

Let me know. I'll do my best to help get to the bottom of this.

Author Comment

ID: 22720704
Thanks novynov.

As far as I understand, the call to DataBind() is needed to bind the new EmptyDataTemplate to the gridview.  EIther way thoughy, it doesn't work with or without it!  I have also tried removing the EmptyDataT template from the aspx page completely and defining the whole thing through the template builder using the above method, but doign that I get nothing.

I could go with using a panel, but I've not been able to reference a panel that is inside the EmptyDataTemplate to set it's visibilty.  Any hints how to do that?

Accepted Solution

novynov earned 500 total points
ID: 22720841
Referencing controls in a template can be done using FindControl() with the ID of the control. I believe this can also be done against the emptydatatemplate - though it may require a recursive call (e.g. FindControlRecursive() - I have code for this if you don't). I'll try it later to make sure (tied up right now with Windows Workflow) and let you know, but thought I'd shoot back a quick response in case you wanted to try to shortcut things.

Author Comment

ID: 22721360
A-ha, hadn't though of a recursvie FindControl! Works like a charm!  Thanks :)

private Control FindControlRecursive(Control ctrl, String id)
    //Recursive function to locate a control anywhere in a parent control. 
        //If this contorl is the search target then return it
        if (ctrl.ID == id)
            return ctrl;
        //Recursively check child controls
        foreach (Control ctrlChild in ctrl.Controls)
            Control ctrlRecursive = FindControlRecursive(ctrlChild, id);
            if (ctrlRecursive != null)
                return ctrlRecursive;
        //If the control is not found then return null
        return null;

Open in new window


Author Closing Comment

ID: 31506223
Thanks for you help - got me out of a sticky little hole!

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question