Asp.Net: Menu control remove MenuItem (MenuItemDataBound)

by Pieter Brinkman 26. February 2009 07:58

For a project I needed to remove menu items to the pages in the folder 'Subscriberpages' when a the user is in the Role of 'Marketing' and 'AccountManagement'. To do this I added the following code to the MenuItemDataBound event.

[code:c#]
protected void mainMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
 SiteMapNode node = e.Item.DataItem as SiteMapNode;

 if (node.Url.Contains("Subscriberpages"))
 {
  if (HttpContext.Current.User.IsInRole("Marketing") || HttpContext.Current.User.IsInRole("AccountManagement'"))
  {
   // Get menu.
   Menu topMenu = (Menu)sender;


   // Remove dataitem from menu.
   topMenu.Items.Remove(e.Item);
  }
 }
}
[/code]


To generate the menu I used the Web.sitemap and the asp:Menu control.

 

Categories: ASP.Net | Controls

Asp.Net: Method to get week number (getweekofyear)

by Pieter Brinkman 21. February 2009 09:41

This method return the integer weeknumber of a specific date. 

public static int GetWeekNumber(DateTime date)
{
 // Gets the Calendar instance associated with a CultureInfo.
 CultureInfo myCI = new CultureInfo("nl-NL");
 Calendar myCal = myCI.Calendar;

 // Gets the DTFI properties required by GetWeekOfYear.
 CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
 DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;

 return myCal.GetWeekOfYear(date, myCWR, myFirstDOW);
}

More info:
http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

 

Categories: ASP.Net

.Net: Example basic consoleapplication with parameters

by Pieter Brinkman 20. February 2009 08:01

This is a basic example of an console-application with parameters used for a scheduled tasks.

The application will be scheduled to run every day with the -n parameter. But the application can also run on a specific date by using the -d parameter.

class Program
{
 //Parameters
 private static DateTime theDate;
 private static RunMode mode = RunMode.NotSpecified;

 static void Main(string[] args)
 {
  if (CollectParameters(args))
  {
   Console.WriteLine("Beginning appliation in mode \"{0}\" with date {1}.", mode, theDate);
   Logger.LogEvent("Starting StatisticConsoleApp", args, (int)Logger.LogType.Message);

  }
 }

 private static bool CollectParameters(string[] args)
 {
  if (args.Length > 0)
  {
   for (int ii = 0; ii < args.Length && mode == RunMode.NotSpecified; ii++)
   {
    //Default commands for support -h and /?
    if (args[ii].ToLower().Trim() == "-h" || args[ii].Trim() == "/?")
    {
     PrintHelpText();
     return (false);
    }
    else
    {
     //Get clear input params
     string inputParam = args[ii].ToLower().Trim();
     inputParam = Regex.Replace(inputParam, "[/-]", "");

     switch (inputParam)
                    {
                        case "n":
                            mode = RunMode.Normal;
                            theDate = DateTime.Now;
                            break;

                        case "d":
                            mode = RunMode.SpecificDate;
                            if ((ii + 1) < args.Length)
                            {
                                theDate = Convert.ToDateTime(args[ii + 1]);
                            }
                            break;
       default:
        if(args[ii].Substring(0,1) == "-")
         Console.WriteLine("Unknown switch {0}. Ignoring this switch.", args[ii]);
        break;
      }
    }
   }

   if (mode != RunMode.NotSpecified)
   {
    return true;
   }
   else
   {
    Console.WriteLine("No valid parameter specified. Aborting processing.....");
    return false;
   }
  }
  else
  {
   PrintHelpText();
   return false;
  }
 }


 private static void PrintHelpText()
 {
  Console.WriteLine("\nUsage: Statisticprocess application");
  Console.WriteLine("");
  Console.WriteLine("  -n");
  Console.WriteLine("    The application will run in normal mode.");
  Console.WriteLine("    The statistics will be processed with the current date.");
  Console.WriteLine("  -d");
  Console.WriteLine("    The application runs in 'specific date' mode.");
  Console.WriteLine("    The statistics will be processed with the input date");
  Console.WriteLine("    The date may be specified in any format as long as it can be ");
  Console.WriteLine("    translated unambiguously into a proper date.");
  Console.WriteLine("");
  Console.WriteLine("");
  Console.WriteLine("    Note: Only one mode can be specified.");
  Console.WriteLine("          If two modes are specified, the second mode is ignored.");
  Console.WriteLine("");
  Console.WriteLine("    Example: In this example application runs in \"specific date\" mode ");
  Console.WriteLine("             for the date \"10-sep-2006\".");
  Console.WriteLine("");
  Console.WriteLine("    TOOLNAME [-d 1-jan-2009]");
  Console.WriteLine("");
 }
}

[/code]



If you copy this class into your program.cs you will get some errors concerning my own loghandler and the missing RunMode enum. To fix this delete the code lines for the logging and add the following enum to your console application.

public enum RunMode : byte
 {
  NotSpecified,
  Normal,
  SpecificDate
 }


This is not the clearest example but if you have any questions don't hesitate to ask.  

Categories: Microsoft

Silverlight: Debugging Silverlight (Attach to proces)

by Pieter Brinkman 17. February 2009 06:27

When attaching to a process for debug Silverligh you need to remember that Silverlight is Client-side code. So attach to IExplorer.exe, FireFox.exe or what ever browser your using.

Cheers,
Pieter

Categories: Silverlight

Asp.Net: Using the OnCommand Event with CommandArgument

by Pieter Brinkman 4. February 2009 08:53

When using a button, linkbutton or imagebutton with CommandArguments or CommandName you can use the OnCommand event instead of the OnClick event. Using the OnCommand Event you use less code to extract the CommandArgument and CommandName from the Event comparing to the OnClick event (because you don't need to cast the control).


Code example

The Aspx:

[code:html]

<asp:ImageButton ImageUrl="~/Includes/Images/delete_icon.gif" runat="server" ID="ibtDeleteClip" OnCommand="ibtDeleteClip_Command" CommandArgument='<%# Eval("ClipId") %>' />

[/code]

 

And the codebehind (C#):

[code:c#]


protected void ibtDeleteClip_Command(object sender, CommandEventArgs e)
{
   string commandArg = e.CommandArgument;
}

 

Categories: ASP.Net | Controls

Microsoft Certified Professional Developer: Webapplications (MCPD: Web)

by Pieter Brinkman 3. February 2009 10:13


After finishing my MCTS: Sql Server 2005 I started learning for my last exam for becoming MCPD: Web applications. Yesterday I passed this exam with a score of 875. This MCPD status is for Asp.Net 2.0. Soon I will do my exam for .Net 3.5. 

Now I will start focusing more on WPF and Windows developing and start building more Silverlight applications.

Cheers, Pieter

 

Categories: Portfolio

Running WCF on IIS 5 (Windows XP)

by Pieter Brinkman 2. February 2009 09:53

After running WCF on my Vista laptop (IIS7) I needed to deploy the application on some Windows XP computers. Again some strange errors occurred:

[code:html]

Error Description: "This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.
Parameter name: item"

[/code]


The problem is that WCF cannot handle more than one identity (host headers) per website. At first I configured IIS to have one HostHeader. That solution was just to dirty. So I kept on searching the internet.


You can fix this problem by adding prefix-key(s) in the baseAddressPrefixFilters section of the Web.Config:

[code:xml]
<system.serviceModel>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
        <add prefix=”http://www.local.develop/>


</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
[/code]


Hope this helps.


Sources:
http://geekswithblogs.net/robz/archive/2007/10/02/WCF-in-IIS-with-Websites-that-have-Multiple-Identities.aspx
http://blogs.msdn.com/rampo/archive/2008/02/11/how-can-wcf-support-multiple-iis-binding-specified-per-site.aspx

Categories: iis | Microsoft