Programatically changing gridview EmptyDataTemplate

Posted on 2008-10-15
Medium Priority
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 2000 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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) 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 we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

762 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