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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now