Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

618 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