Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5858
  • Last Modified:

Programatically changing gridview EmptyDataTemplate

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;
                gvAssociatedUsers.DataBind();

Open in new window

0
phillarby
Asked:
phillarby
  • 3
  • 2
1 Solution
 
novynovCommented:
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.
0
 
phillarbyAuthor Commented:
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?
0
 
novynovCommented:
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.
0
 
phillarbyAuthor Commented:
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

0
 
phillarbyAuthor Commented:
Thanks for you help - got me out of a sticky little hole!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now