Custom control with custom property not working properly

 Hi.

I decided to implement a mechanism of Role Based Access Control using Azman and enhanced controls. I'll give you an example:

[ControlBuilder(typeof(SecuredControlBuilder))]
    public class SecureLinkButton : LinkButton
    {
        [
        Category("General"),
        DefaultValue("*"),
        Description("The roles for which the control is visible.")
        ]
        public virtual string Roles
        {
            get
            {
                string s = (string)ViewState["Roles"];
                return (s == null) ? "*" : s;
            }
            set
            {
                ViewState["Roles"] = value;
            }
        }
    }

 

My control builder is the following:

 

public class SecuredControlBuilder : ControlBuilder
    {
        private bool _isInRole = false;

        public override void Init(TemplateParser parser, ControlBuilder parentBuilder, Type type, string tagName, string id, IDictionary attribs)
        {
            if (attribs != null && attribs["Roles"] != null)
            {
                if (attribs["Roles"].ToString() == "*" || HttpContext.Current.User.IsInRole(attribs["Roles"].ToString()))
                    _isInRole = true;
                else _isInRole = false;
            }
            else
                _isInRole = true;

            if (_isInRole)
                base.Init(parser, parentBuilder, type, tagName, id, attribs);
            else
            {
                attribs = new Dictionary<object, object>(1);
                attribs.Add("Visible", false);
                base.Init(parser, parentBuilder, type, tagName, id, attribs);
            }
        }
    }

So, for all those that do not have the role assigned to the control, the control is not visible and the user cannot perform operations that he is not allowed to. Problem appears when using the control in a page. Whenever I assign a role to the control, I am given the following warning: Warning    1    Generation of designer file failed: Object reference not set to an instance of an object. In designer, it tells me "Error rendering control - test. An unhandled exception has occured. Object reference not set to an instance of an object."

This puzzles me.

0
DaeMoohn
4/23/2009 7:40:35 AM
asp.net.custom-server-controls 3498 articles. 0 followers. Follow

7 Replies
830 Views

Similar Articles

[PageSpeed] 53

Hi,

Before answering your question may I ask why you need to do so instead of using the LoginView?

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/login/loginview.aspx


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
4/24/2009 8:11:54 AM

 We are using this because we want to have multiple available actions on a page. That is, imagine a big ListView (or a similar control) with records, each record having link buttons like Edit, Delete, Authorize, Cancel. A record that is unauthorized would have the actions Edit, Delete, Authorize, each visible for different kind of people. By using Azman, we would give this people various roles so that some of them would be able to insert, some to modify, some to delete, some would be able to authorize, some to cancel an authorized record. The entire application runs in our own intranetwork and we rely on active directory.

0
DaeMoohn
4/24/2009 8:29:45 AM

Hi,

I think it's better to use LoginView. Have you read the above quickstart and test it?


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
4/24/2009 9:34:26 AM

 I read the specifications of the control. It has one drawback: the fact that it stops after finding the first appropriate role. Our users may be assigned more than one role, and in fact the same user can edit a record and authorize another - for now I'm still thinking how to stop him from authorizing his own record inside the application (now we are stoping them in the database, and be raise an exception in the stored procedure). So, it would be useful for me a control which works like the one you presented me, with the modification of displaying all the templates coresponding to the roles that match. I'll use the reflector and see the mechanisms of that control to see if I can easily replicate the control and modify that behaviour.

 

I checked now, and for every custom control that has an attribute like the one that I set there, Visual Studio's Designer is unable to display it. When I assign a value to that attribute, I see the "An unhandled exception has occured. Object reference not set to an instance of an object.". Maybe it's a bug in VS.

0
DaeMoohn
4/24/2009 10:25:26 AM

 Or should that Control (extended from LinkButton) have it's own Designer?

0
DaeMoohn
4/24/2009 8:22:58 PM

Hi,

DaeMoohn:
I read the specifications of the control. It has one drawback: the fact that it stops after finding the first appropriate role. Our users may be assigned more than one role, and in fact the same user can edit a record and authorize another

You can specify rolegroup like this:

<asp:RoleGroup Roles="Role1,Role2,Role3">

Furthermore, you can use one LoginView for one control. Then this control will be shown only if the user is in the role you specified.


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
4/27/2009 8:21:05 AM

Thank you very much for this answer. It never crossed my mind to try that. Thanks a lot. I will mark your answer as a solution (although you solution may lead to duplicated code).

0
DaeMoohn
4/28/2009 7:10:54 AM
Reply:

Similar Artilces:

Custom Server Control Made of Custom Controls?
I'm giving myself a crash course in custom server controls, and I have a quick general question. Can I have a custom server control that is composed of other server controls I created. I'm pretty sure the answer is yes, but I want to be positive. Yes http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondevelopingcompositecontrols.asp Shows how to combine existing controls into a new control using class composition. -ron Thanks! ASP.NET so rocks! Making page components extensible objects is such a great idea! I love it....

Usercontrol vs Custom Server Controls, Any need for Custom Server Controls
Hello I'm new to ASP.Net so this is probably a stupid question but, is there any need to create customer server controls now that you can pre-compile usercontrols into a dll and thus easily share between applications? This blog post from David Ebb describes how to do it and the restrictions he note don't seem that bad (other than being able to embed resources such as images in the dll). http://blogs.msdn.com/davidebb/archive/2005/10/30/487160.aspx His article desribes the techniques for ASP.Net 2.0 and he suggests that there will be further improvements in .Net 3.5 in this ar...

problems listenening to events on a custom server control from another custom server control
 Hi all,I am having problems listening to events on one of custom server controls(AddBookmark) from another custom server control(BookmarkList). The listener or the BookmarkList control has a property that can be set to the AddBookmark control (string at design but the reference is obtained at run-time). I am able to get a reference to the AddBookmark control and add a listener to it on the BookmarkList control on the OnPrender of BookmarkList. But the problem is that when the events on AddBookmark gets fired, the listener added from the BookmarkList is no longer present and the listene...

Custom Server Control Vs Web Parts, Any need for Custom Server Control
Hello I've just posted a similar question on why you need server controls when you can pre-compile usercontrols and redistribute them. http://forums.asp.net/p/1397433/3010100.aspx#3010100 I've seen lots of (usercontrol vs web part) but not much on web part vs custom server control. According to the MSDN site you can put web parts onto a normal asp.net page outside of a web part zone and they behave just like custom server controls. So, if you are creating a bunch of controls for redistribution to customer asp.net sites and/or SharePoint sites why don't you just create Web Par...

custom property style for a custom server control
Can I add a proerty for style on my asp .net custom server control, seems to be one for everything else. Or do you have to go do one style at a time: backgroundColor, Font, etc....

adding custom properties to a custom server control
1.  I want to add a public property to a custom server control.  the property is called Search Type and the possible choices that will appear in the properties window from the asp.net page would be client, provider, vendor and service.  I want this property to function much like the border style property works for an asp.net button. 2.  My second question would be how do I add a section to the properties window?  What i mean is for this custom server control, i want to add a section called Search Features much like the appearance or behavior sections for an asp.net ...

Custom Control within a Custom Control
I have two individual custom controls say custom cc1 and cc2. cc1 is communicating to cc2 using event delegate mechanism.I have tested the behaviour by dropping cc1 and cc2 onto a Web Page and it works fine. I want to create a third custom control say cc3 which should contain both cc1 and cc2. So that if i drag and drop cc3 onto a Web Page i can acheive exactly what i am doing above. The problem is that i am not sure how to call/register a custom control within a custom control ? Any clue ? Thanks!! Hi, Do you mean adding custom control to custom control? I think you can use the same wa...

How to create custom properties for aspx page which should refelect in design time properties window itself.( as like as custom server control).
Hi. I need to create the custom properties for aspx page itself where those custom properties should refelet in the properties window at the design time as like as other custom server controls. I tried pageBase class which will have cusom properties for page and inherited that pageBase to my aspx page . But its refelecting the code  behind file not in design time. I need to accss those aspx page custom properties at the design time itself in properties window as like as other custom server control.I appriciate your help. [FYI: my scenerio is customer ...

Creating Custom Controls With Custom Controls Inside Of Them
Hello, I am trying to create a custom control for creating tabs. The idea is that our designer can just use the control tags to generate the tabs.  The problem that I am running into is that I want to be able to have a custom control that allows any number of child custom controls. So there would be a TabContainer Control, and within that there would be any number of tabs.  The idea is similar to how an asp:DropDown list can have any number of ListItems within them.  Here is my code, any help is appreciated. 1 <!-- TAB CONTAINER -->2 <%@ Control Language=&...

How do I make a Custom Property in my Custom Control into a Bindable Property
I've got a custom control in ASP.NET 2.0.  I want to bind one of its properties to data, but even though I specify the [ Bindable(true) ], before the property, the only property that shows up in the Data Bindings dialog is "visible". This is my code for the Custom Control:public partial class Offers_SearchTemplates_OfferSearchButton : System.Web.UI.UserControl{[Bindable(true)]public virtual string OfferUID{get{object objTitle = ViewState["OfferUID"];if (objTitle == null)return string.Empty;return (string)objTitle;}set{ViewState["OfferUID"] = value;...

Custom controls called from inside other custom controls???
I'm fairly new to writing controls in ASP.NET so, forgive the I am trying to create a base page class which will call 3 custom controls. This base page class will span across multiple projects hence the reason I am not creating user controls. These 3 custom controls are: 1. TopNav-uses render method to display an HTML table with graphics for top navigation. 2. ImagePairs-uses prerender method to add javascript code to manage what graphic is displayed in TopNav onMouseOver and onMouseOut 3. MgrHeader-uses render method to display an HTML table with s...

Custom Events in Custom Server Control
 Hi There I've created a custom server control. I have extended it by adding my own event which fires in one of the aggregated control's event handlers. My probelms is that I would like to be able to specify the event handler for the event from the aspx file as a tag attribute.Does anyone know how to link the attribute of a customer server control tag in the aspx file to a custom event in the class source for the control?   I've done this several times with Custom Controls.  All I do is declare the event in the CustomControl's class public...

Custom event for Custom server control
  Hi All, I have a custom server control (Multi selection drop downlist). This control contains AspxEditButton control(which is like textbox with button), check box list control and panel.I have to display all selected values in AspxEditButton at control is leaving that panel. How can i create LostFocus event for panel . Please anyone help meRegards, Anu All I can think of, is that there is a javascript "onblur" you could look into, maybe you could use some AJAX techniques to make that JavaScript event fire a server side ev...

Custom User Controls v. Custom Web Controls
Hi! Is it better for me to begin coding my controls instead of just designing them? What would the benefits be? I meen, i seem to be able to get all i need from just putting stuff together on an ASCX file but i'm obviously missing a huge concept here... please explain! Hi, creating user controls is indeed easier than creating custom server controls because you can simply drag & drop other controls on the form, easily add events to it, ... Custom controls however are better when you want to reuse them in several other projects. I like to create custom controls for small, reusable com...

Web resources about - Custom control with custom property not working properly - asp.net.custom-server-controls

Property - Wikipedia, the free encyclopedia
This article may require copy editing for grammar, style, cohesion, tone, or spelling . You can assist by editing it . In abstract, property ...

Property - Wikipedia, the free encyclopedia
In abstraction, property is that which belongs to or with something, whether as an attribute or as a component of said thing. For the significant ...

RBA governor Glenn Stevens welcomes slowing property market
The easing of property markets is welcome and essential says RBA governor Glenn Stevens.

Melbourne Anarchist Club in battle with property developers
The Melbourne Anarchist Club &ndash; home to a group of social outliers who reject &quot;hierarchical power structures&quot; and believe profit ...

China stocks rise, led by property shares - DailyTelegraph Search Search
CHINA stocks have climbed, led by property and resources shares, amid signs that insurers are scrambling for stakes in real estate majors and ...

China stocks rise, led by property shares
CHINA stocks have climbed, led by property and resources shares, amid signs that insurers are scrambling for stakes in real estate majors and ...

Basics key to selling Perth property, says new report - PerthNow Search Search
HOMES with water views might net a million-dollar return, but practical features such as a double lockup garage and a short commute to work make ...

Chinese demand for Aussie property drops as much as 15%, McGrath says
Chinese demand for Australian homes has dropped as much as 15 per cent from a year earlier as China's stocks tumbled and the economy slowed, ...

Men face court after $30,000 worth of suspected stolen property seized in Griffith
Two men are set to face the ACT Magistrates Court on Saturday morning in relation to stolen property and burglary charges.

Vandals spray-paint Hawthorne mosques, leave fake grenade on property
Two mosques in Hawthorne were vandalized and police say both incidents are being investigated as hate crimes.

Resources last updated: 12/16/2015 3:54:12 AM