January 2006 - Posts - Raj Kaimal

January 2006 - Posts

Determining a SiteMapNode's visibility at runtime

Just a random thought. Wouldn’t it be nice if the XmlSiteMapProvider or SiteMapDataSource had a CheckAccessibility event? All we would have to do is handle this event and set a property that determines if the node should be displayed or not.

This way, we can control a nodes visibility using some custom rules rather than role based visibility.

The provider would look like this

public class SecurityXmlSiteMapProvider : XmlSiteMapProvider {

 

    public delegate void AccessibilityHandler(object sender, NodeAccessibleEventArgs e);

    public event AccessibilityHandler CheckAccessiblity;

 

    public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node) {

        NodeAccessibleEventArgs nodeAccEvtArgs = new NodeAccessibleEventArgs(node);

 

        if (CheckAccessiblity != null) {

            CheckAccessiblity(this, nodeAccEvtArgs);

            return nodeAccEvtArgs.IsAccessible;

        }

        else {

            return true;

        }

    }

}

 

The web.config file would contain this


<siteMap defaultProvider="SecurityXmlSiteMapProvider" enabled="true">

  <providers>

    <add

      name="SecurityXmlSiteMapProvider"

      description="Default SiteMap provider."

      type="TestControls.SecurityXmlSiteMapProvider"

      siteMapFile="Web.sitemap"

      securityTrimmingEnabled="true"

          />

  </providers>

</siteMap>

 

And at runtime, you would handle the event like so and determine if the node should be displayed or not (Could be simpler if we were handling the event in SiteMapDataSource).

 

protected void Page_Load(object sender, EventArgs e) {

 

    SecurityXmlSiteMapProvider siteMapProvider = SiteMapDataSource1.Provider as SecurityXmlSiteMapProvider;

    if (siteMapProvider != null) {

        siteMapProvider.CheckAccessiblity += new SecurityXmlSiteMapProvider.AccessibilityHandler(siteMapProvider_CheckAccessiblity);

    }

}


void siteMapProvider_CheckAccessiblity(object sender, NodeAccessibleEventArgs e) {

    if (..Test Condition..) {

        e.IsAccessible = false;

    }

    else {

        e.IsAccessible = true;

    }

}

Posted by rajbk | 7 comment(s)
Filed under:

Page Event handlers in ASP.net 2.0

In ASP.net 2.0, you can declaratively wire an event to a control by adding an attribute/value to your control. The attribute name is the event name and the attribute value is the method to call. VS 2005 will also create the handler for you as shown below. 

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

protected void Button1_Click(object sender, EventArgs e) {

Similarly, since we have a Page_Load event handler in the code behind, I expected to see some kind of declaration in the aspx code but found none.

Thankfully, MSDN explains this.

To create a handler for page events

In the code editor, create a method with the name Page_event.

For example, to create a handler for the page's Load event, create a method named Page_Load.

Page event handlers are not required to take parameters the way that other controls event handlers are.
 
ASP.NET pages automatically bind page events to methods that have the name Page_event. This automatic binding is configured by the AutoEventWireup attribute in the @ Page directive, which is set to true by default. If you set AutoEventWireup to false, the page does not automatically search for methods that use the Page_event naming convention.
http://msdn2.microsoft.com/en-us/library/6w2tb12s.aspx


So, if I wanted to handle the Page Init event, I just need to add this and the event gets bound automatically.

protected void Page_Init() {
   
Trace.Write("I am in init!");
}

void Page_Init() {
   
Trace.Write("I am in init!");
}

Posted by rajbk | 3 comment(s)
Filed under:

Tip to optimize C# Refactoring in Web Projects

Scott has posted a great tip for speeding up refactoring performance with Web Projects in VS 2005.

The refactoring time has reduced from 1 minute 10 seconds to less than a second for my Web Project.

No more "Preparing file ...."

This is a great way to start the new year!!
Very cool!

PS: Since the refactoring tool was taking too long, I decided to read some blog posts to kill time and ended up reading his post.

Posted by rajbk | with no comments
Filed under:
More Posts