Display Logic Controls

J

John Brainard

I'm trying to write a server control that will decide whether or not the
content inside the tag body is displayed based on the logged in user's
permissions. For Example:


<cms:UserHasRights gid="editor" runat="server">
... Some html or ASP.net controls ...
</cms:UserHasRights>



The gid attribute is the group id that the user needs to be a member of in
order for the body of the <cms:UserHasRights> tag to be displayed. I've
searched high and low (and perhaps overlooked something) for a similar
example and haven't been able to find anything.

Any help would be greatly appreciated.

Thanks!

John
 
E

Ezra Epstein

There are a variety of "standard" techniques for this.

The basic setup is: conditionally display some content on the page.

The condition is app dependent. I just assume there's a Property that will
return a bool indicating whether or not the component should be displayed.

Most any html can 'runat' the server. All Web Controls can, of course. Any
server-based html block can be set Visible='false' or can be put inside a
asp:placeHolder or asp:panel that in-turn can have it's Visible attribute
set to false or true for conditional display of any contents.

Then you got 2 ways to go for setting that Visible attribute: data-binding
or programmatically setting the control.

Here are those 2:

The programmatic approach is trivial, just put the logic in any old part of
the code that participates in page rendering. If you will know whether to
display when the page is loaded (as opposed to after a post-back) then the
logic can go in Page_Load:

<asp:panel id="someConditionalDisplay" Runat="server">Show this or
don't!</asp:panel>

Then in the code-behind:

public class MyPageOrUserControlClass
{
protected System.Web.UI.WebControls.Panel someConditionalDisplay;

private void Page_Load(object sender, System.EventArgs e)
{
someConditionalDisplay.Visible = false; // or true, or ...
}

// assume standard VS "wiring": that Page_Load has been added to the page's
OnLoad event call-backs.

}

If you won't know whether to display or not at page load you can set the
Visible flag later -- either in the particular event (e.g., button click)
post/call back, or by overriding the OnPreRender() method.

Note that, like all ASP.Net server controls you gotta set the control's ID
attribute to map to the name of a variable in the code behind.

As for data-binding it's pretty similar:

<asp:panel id="someConditionalDisplay" Runat="server" Visible='<%#
ShowConditionalDisplay %>'>Show this or don't!</asp:panel>

Then in the code-behind:

public class MyPageOrUserControlClass
{
protected System.Web.UI.WebControls.Panel someConditionalDisplay;

protected bool ShowConditionalDisplay
{
get { return false; // or true, or ... check if the user is
logged in ...}
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack) {
this.DataBind();
}
}

// assume standard VS "wiring": that Page_Load has been added to the page's
OnLoad event call-backs.

}


The short of it is, the gid is part of your app's logic and is not somethin
that ASP.net will check for. You need to set the control's ID. Note,
another approach is if you're writing your own controls and are willing to
make them closely bound to the app context in which they will be used the
control can have its own logic: checks it's gid, then looks at who's logged
into the app and then set itself visible or not accordingly.

Hope that helps,

Ezra E.
 
J

John Brainard

I'm trying to avoid using the Code Behind feature of the ASP.net page. The
page will be developed by someone who isn't a programmer. They'll be given a
set of custom server controls and told how they are used and the logic
inside the control needs to determine whether the content is displayed. It's
one of those situations where the developer needs to drop something in the
html and it will just work without having to do anything special.

I will try setting the Visible property in the OnLoad event from inside the
control's code. I think that this will do what I want it to do.

Thank you for your help! I'll post the results of efforts when I get a
chance to try them out.
 
E

Ezra Epstein

Yeah, let us (the newsgroup) know.

(Back from a long weekend....)
You can write the code into the custom control. The issue that comes up is
how generic can you make the control. Normally that's a key aspect of
Custom Controls: they are drop-in reusable elements that have
control-specific or property-set state and that's all. The question of
who's logged in is a question of application state: state extrinsic to the
control. One easy way to pass this in to the control is something like:

<mytags:mycustomcontrol gid="someGroupId" user="currentUser" >
conditional content </mytags:mycustomecontrol>

The thing here is that now mycustomecontrol needs to know a fair bit about
the User object. In other words, the control is coupled with this
particular app. Another technique might be:

<mytags:mycustomcontrol gid="someGroupId"
GroupIDs="currentUser.AllGroupIDs" > conditional content
</mytags:mycustomecontrol>

Then the control is a bit more removed: it simply iterates over the GroupIDs
to see if there is one that matches gid and displays or does not display its
content accordingly.

Good luck,

Ezra E.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,568
Members
47,204
Latest member
abhinav72673

Latest Threads

Top