Bilal Haidar Blog
Jul 22 2008
Here is a snippet code that helps you bind the items contained in an enumeration into a DropDownList in ASP.NET.
To start with, let us define an enumeration as follows:
public enum Directions { North = 1, East, South, West }
The above is a C# definition for an enumeration called Directions that has 4 main values.
Now, to bind the above enumeration values to a DropDownList we need the following:
if (!Page.IsPostBack) { // Loop through the Directions' items // and add them item by item into the DropDownList ListItem item = null; string[] directionNames = Enum.GetNames(typeof(Directions)); for (int x = 0; x < directionNames.Length - 1; x++) { // Create the item item = new ListItem( directionNames[x], ((Int32)Enum.Parse(typeof(Directions), directionNames[x])).ToString()); // Add the item to the list of items // inside the DropDownList this.ddlDirections.Items.Add(item); } }
You notice in the code above, to retrieve the names of all the items in the enumeration, you make use of the Enum.GetNames() method. Once you have all the items in the enumeration as String values, you loop through the list of values of the enumeration.
For each value, you create a new ListItem class passing to it the:
Once the ListItem is created, simply add it to the collection of Items of the DropDownList placed in the HTML markup.
The DropDownList is defined as follows:
<asp:DropDownList ID="ddlDirections" runat="server" AppendDataBoundItems="true" > <asp:ListItem Text=" .. Choose a Direction .." Value="-1" Selected="True"/> </asp:DropDownList>
This is all what you need to do to bind a DropDownList to an enumeration!
Hope it helps, Regards
Tags: ASP.NET 2.0, ASP.NET 3.5
Comments Off
Jul 21 2008
The ASP.NET AJAX Roadmap is published and can be reached here (http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14924)
Enjoy it!
Regards
Tags: AJAX-ATLAS, ASP.NET 2.0 - Master Pages, ASP.NET 3.5, ASP.NET Extensions 3.5
Jul 17 2008
In ASP.NET 2.0 and ASP.NET 3.5 applications hosted under IIS 7.0 and are running with the Integrated Mode, ASP.NET Session State does not get initialized when a native or non-managed request enters the Integrated HTTP Request pipeline.
If you look at the IIS 7.0's applicationHost.config configuration file located at: %windir%/System32/inetsrv/config you will notice that the SessionStateModule is defined to handle only managed requests as follows:
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="managedHandler" />
You notice that the preCondition attribute has the value of managedHandler which means that this module would be initialized only for managed requests, i.e. .aspx, .asmx, etc ...
First of all to be able to make this module get initialized for non-managed resources, you need to remove this entry and then add the same entry but this time having the preCondition attribute set to empty string.
<remove name="Session" /> <add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" />
With the above configuration added to the application's web.config configuration file under the <modules /> configuration section located in <system.webServer /> configuration section group.
Now, the Session State is still not initialized for non-managed resources, why?
The SessionStateModule is usually initialized in the AcquireRequestState HttpApplication's event. This event usually gets called after the MapRequestHandler and PostMapRequestHandler events. This means, the AcquireRequestState event fires after the ASP.NET Runtime has decided on the HttpHandler for the current HTTP Request.
The SessionStateModule does a check if the current HTTP Request's Handler implements the IRequiresSessionState marker interface. If not, the SessionStateModule stops execution and no ASP.NET Session State gets initialized. Why? There is no native or non-managed Handler that can implement the managed IRequiresSessionState marker interface and hence if the SessionStateModule finds out that the Handler created for the current request does not implement the IRequiresSessionState, no Session State gets initialized.
Now, when a non-managed or native request enters the Integrated or Unified HTTP Request Pipeline, ASP.NET Runtime does not create an instance of a managed Handler, the HttpHandler for the current HttpContext is null and not initialized. This means that the SessionStateModule, that has already been mapped for native and managed requests, checks that the current HttpContext's Handler is null and hence there is already no Handler instance created to check if it implements the IRequiresSessionState marker interface.
The trick to solve this problem is provided by Mike Volodarsky (Program Manager in the IIS team) that can be read in this www.iis.net forum post (https://forums.iis.net/p/1094546/1648944.aspx#1648944)
What should be done is fake the SessionStateModule with an instance of a managed HttpHandler that implements IRequiresSessionState marker interface that gets created for native requests.
What are you talking about Bilal?
Well yes. The idea is to develop a custom HttpHandler that implements the IRequiresSessionState marker interface with empty implementation, this handler will be used as a temporary handler only as you will see later in the code.
Next, what we need to do is the following:
In the MapRequestHandler event, a managed handler is created when the HTTP request is for a managed resource. This means, in the PostMapRequestHandler event you can check to see if ASP.NET runtime has created an HttpHandler for the current request, if not, then this means the HTTP request is for a native resource and not a managed one. At this moment, you can initialize a new instance of the custom dummy handler that was introduced above, set it to the Handler of the current HTTP request.
After the PostMapRequestHandler executes, the AcquireRequestState event fires. At this moment, the SessionStateModule starts executing. Now, the SessionStateModule will find out that the current HTTP request has a valid HttpHandler that implements the IRequiresSessionState marker interface, this means now the ASP.NET Session State will get initialized successfully!!
In the PostAcquireRequestState event the current HTTP request's Handler should be reverted back to its original Handler (i.e. Handler created by ASP.NET runtime which is in this case NULL because the request originally is for a native and non-managed request). This is very important because if the custom dummy HttpHandler was kept as the Handler for the current request, the ASP.NET Runtime would execute that Handler when it is time during the Integrated HTTP Request Pipeline to execute the Handler for the current request.
Therefore, you should check if the current request's Handler is of type the custom dummy HttpHandler then you should set it back to NULL, where NULL represents the Handler's instance that was originally created by the ASP.NET runtime.
The code below shows both the custom dummy HttpHandler and the custom module used to configure the PostAcquireRequestState and PostMapRequestHandler events.
CustomNativeHandler
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.SessionState; using System.Web.Security; public class CustomNativeHandler : IHttpHandler, IRequiresSessionState { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { throw new NotImplementedException(); }
public CustomNativeHandler() { } }
SessionStateForNativeRequestsModule
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; public class SessionStateForNativeRequestsModule : IHttpModule { public void Dispose() { throw new NotImplementedException(); } public void Init(HttpApplication context) { // Subscribe to the PostMapRequestHandler context.PostMapRequestHandler += new EventHandler(context_PostMapRequestHandler); // Subscribe to the PostAcquireRequestState context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState); } void context_PostAcquireRequestState(object sender, EventArgs e) { // Get an instance of the current Context HttpContext context = ((HttpApplication)sender).Context; // Use "as" so that if the current handler // is not a CustomNativeHandler, then no exception // will be thrown, like in the case of casting. CustomNativeHandler customHandler = context.Handler as CustomNativeHandler; // If the current handler is our custom dummy handler // then revert back to the managed handler that was // originally created by the .NET Framework for the // current request, which is null in this case // since .NET Framework does not create a managed Handler // for a native request if (customHandler != null) context.Handler = null; } void context_PostMapRequestHandler(object sender, EventArgs e) { // Get an instance of the current Context HttpContext context = ((HttpApplication)sender).Context; // If the current HttpHandler is null, then this must be // a native request being processed. // Create a new instance of the CustomNativeHandler // and set it to the current request Handler. if (context.Handler == null) context.Handler = new CustomNativeHandler(); } }
To configure the above custom module in an application's web.config configuration file, you add the following section:
<system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules> <add name="SessionStateForNativeRequestsModule" type="SessionStateForNativeRequestsModule"/> <remove name="Session" /> <add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" /> </modules> </system.webServer>
Hope this helps, Regards
Tags: ASP.NET 3.5, ASP.NET Session State, IIS 7.0, SessionStateModule
Jul 10 2008
I was searching for the WinSDK for .NET 3.5 and found it on the MSDN website. This is very essential especially when you are about to learn a new technology, it is worth hundreds of books out there!!
Check it out here: Windows SDK for Windows Server 2008 and .NET Framework 3.5 (http://www.microsoft.com/downloads/details.aspx?FamilyId=E6E1C3DF-A74F-4207-8586-711EBE331CDC&displaylang=en).
Tags: Windows SDK, Windows SDK .NET 3.5, Windows SDK Server 2008
Jul 8 2008
Community Nights Session
Interfaces and Abstraction Ralph Varjabedian (Telephone.com Team Leader) Monday, July 14, 2008 - 6:00 - 8:00 PM Microsoft office
Please use the REGISTRATION PAGE to register for the session. Seats are limited, make sure you confirm your attendance once you receive the email upon regisration.
Note: If you have any problem with registration, please contact Bilal Haidar:
Tags:
Jul 6 2008
This post should have been written long time ago in fact!!
Haissam is running a very interesting blog at: http://dotnetslackers.com/Community/blogs/haissam/
What is worth mentioning about Haissam's blog is that he is posting nowadays on integrating ASP.NET and .NET with other technologies and this is really cool. He is working on Adobe Flex this period as a UI layer for Web applications and having .NET as the back-end and hence the beauty of this integration.
Check out his blog!
Keep up the great job Haissam! All of luck.
Jul 2 2008
Few months ago Telerik started a new strategy of delivering guides and tutorials to its products by adding the Telerik Trainer. The Telerik Trainer is a special Media-Player software that you can download for free from the Telerik website (http://www.telerik.com/training.videos/TelerikTrainer.zip). This Media Player has all the features required to watch the T.T. videos with special and customized effects and hence no need for any other media player to watch the T.T. videos. Once you download the T.T. you can download several T.T. videos that are evolving day after day with lots of videos being added. You can reach the download page here (http://www.telerik.com/support/teleriktrainer.aspx).
In addition, Telerik already has a Videos page that you can download and watch to get to know more about their controls and how to use them and do special tasks with them. You can reach the Videos page here (http://www.telerik.com/support/videos/default/b220i-a.aspx).
Moreover, Telerik provides a self-paced tutorial that is around 600 pages compiled in a PDF document that covers all of their controls. It is a Step-By-Step tutorial that you can go through to learn how to use and configure the controls properly. You can reach this long-tutorial here (http://www.telerik.com/support/self-paced-tutorial.aspx).
Also, when you download and install the Telerik controls there is the main CHM documentation file that contains heavy tutorials, online links on how to build some features and a complete documentation for all the classes that build up the Telerik controls.
Finally, Telerik provides an online Forums that you can make use of to ask questions where other developers using the Telerik controls help you there and reply to your questions including Telerik MVPs and Telerik core developers.
Last but not least, Telerik provides an online support tickets where you can send a request to the support team to help you with your problems, issues and bugs (if any).
Hope all this helps! Regards
Tags: Telerik Controls
Jun 29 2008
You can download the code for the post here.
I have always enjoyed the ModalPopup dialogs provided by ASP.NET AJAX Extensions 1.0. In one way or another they add a very special touch to the Web application in a user-friendly way. You can read more on ModalPopups in AJAX here (http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ModalPopup/ModalPopup.aspx).
Now that I am working on WPF, I thought of having the same feature in WPF Browser Application (XBAP). The WPF Browser Application as you know runs in the Browser (Firefox and IE) with several Security limitations on WPF. WPF provides the Popup class that allowed me to provide such a Modal Popup Dialog Box. Read more on Popup class here (http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.aspx).
To start with, let us create a new XBAP application by opening up VS 2008, File --> New Project --> WPF Browser Application.
Let us prepare Page1.xaml of this new project piece by piece:
<Page x:Class="ModalPopupDialog.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Modal Popup Dialog">
First of all set the Title of main page.
<StackPanel Name="mainPage" > <StackPanel Margin="10"> <TextBlock Margin="5" TextWrapping="Wrap">Click the button to show a Modal Popup Dialog Box</TextBlock> <Button Click="cmdShowModal_Click" HorizontalAlignment="Left" Padding="3" Margin="5">Show Modal</Button> <Label Name="lblName" Margin="0, 10, 10, 10" FontSize="18" FontFamily="Arial"></Label> </StackPanel>
Add a new StackPanel that shall be the main container of the entire Page. Then add another StackPanel to hold a TextBlock and a Button. The TextBlock asks the user to press the Button to show the Modal Popup Dialog Box.
<Popup Name="dialogPopUp" StaysOpen="True" Placement="Center" Width="250" Height="200"> <ScrollViewer VerticalScrollBarVisibility="Auto"> <Border BorderThickness="1" Background="#D4D0C8"> <TabControl> <TabItem Header="Page 1"> <StackPanel Margin="5" Background="White"> <TextBlock Margin="10" TextWrapping="Wrap"> Enter your Full Name: </TextBlock> <TextBox Focusable="True" Name="txtName" Margin="10"></TextBox> <StackPanel Orientation="Horizontal" Margin="10"> <Button Click="cmdOK_Click" Padding="3" Margin="0,0,5,0">OK</Button> <Button Click="cmdCancel_Click" Padding="3">Cancel</Button> </StackPanel> </StackPanel> </TabItem> <TabItem Header="Page 2"> <StackPanel Margin="5" Background="White"> <TextBlock Margin="10" TextWrapping="Wrap"> Another TabItem </TextBlock> </StackPanel> </TabItem> </TabControl> </Border> </ScrollViewer> </Popup> </StackPanel> </Page>
The Popup Window contains a ScrollViewer that makes sure the Popup Window will scroll when its content Width or Height goes beyond the preset Width and Height on the Popup Window.
The ScrollViewer contains a TabControl with two defined TabItems. The first contains a TextBlock and TextBox to allow the user to enter her/his Full Name and then either hit the OK or Cancel button. The other TabItem is empty just a way to show you how flexible the Popup Window is.
Before running this application we need to add some code in the code-behind of the Page1.zaml. Mainly, when the *Show Modal* button is clicked we want to disable the main StackPanel so that the user cannot interact with the page, set a light gray color for the background of the page and finally open the Popup control using the *IsOpen* boolean Dependency Property.
When the OK button on the Popup control is clicked, we need to reverse what has been done above, i.e. enable the main StackPanel, remove the light gray color background of the main page and finally close the Popup control. In addition, we will grab the text the user has entered and show it on a label on the main page.
When the Cancel button on the Popup control is clicked the same thing should happen as in the case of the OK button with the exception that there is no need to update the Label in the main page with any entered text.
The code below shows how the above is implemented:
private void cmdShowModal_Click(object sender, RoutedEventArgs e) { // Configure the main page to disable the main StackPanel, // Set the background color to LightGray as if it is dimed // and finally open the Dialogbox. this.ConfigureMainPage( false, new BrushConverter().ConvertFromString("#8F8F8F") as SolidColorBrush, true); } private void cmdOK_Click(object sender, RoutedEventArgs e) { // Configure the main page to enable it, remove the LightGray // color as the background and finally close the dialogbox. this.ConfigureMainPage(true, null, false); // Grab the Full Name entered in the dialogbox. this.lblName.Content = string.Format("Welcome {0} to my first XBAP application!", this.txtName.Text); // Clear the Textbox this.txtName.Text = ""; } private void cmdCancel_Click(object sender, RoutedEventArgs e) { // Configure the main page to enable it, remove the LightGray // color as the background and finally close the dialogbox. this.ConfigureMainPage(true, null, false); // Clear the Textbox this.txtName.Text = ""; } /// <summary> /// Configures the main page by setting the /// IsEnabled property of the main StackPanel, setting /// the background of the main StackPanel and finally open/close /// the dialog box /// </summary> private void ConfigureMainPage( bool mainPageEnabled, Brush mainPageBackground, bool IsDialogOpened) { this.mainPage.IsEnabled = mainPageEnabled; this.Background = mainPageBackground; dialogPopUp.IsOpen = IsDialogOpened; }
The code is self-explanatory and simply does what I have just explained above.
When we run the application, the main page looks as in Figure 1 below:
Once you click the *Show Modal* button the Popup window shows up as shown in Figure 2 below:
The user can type her/his name then either press OK or Cancel button. Notice how the Popup is popped Window-Centered and you can see the TabControl clearly containing two main tabs. Assume I typed my name and pressed OK, the page looks as in Figure 3 below:
You can see how the page is updated with the text the user has typed in the Textbox that was shown inside the Popup control.
Hope you enjoyed reading this post!
Thanks
Tags: WPF Browser Applications
Jun 24 2008
Few months ago I was contacted by Wiley Publishing to write an ASP.NET book. The book that I was asked to work on was the Professional ASP.NET 2.0 Security, Membership and Role Management written by Stefan Schackow.
The idea was to upgrade the book into ASP.NET 3.5, IIS 7.0 and provide both C# and VB.NET code for all the chapters.
To make the story short, I started working on this book that now has the following title:
Professional ASP.NET 3.5 Security, Membership and Role Management with C# and VB
This week I finally finished the first draft of all the chapters. Expect the book to be in the market sometime end of this summer!
It was a great experience working on authoring a book after I have worked for several years on writing articles online or for printed magazines.
More to come on the book :)
Jun 11 2008
I have started lately working on WPF and developing an application by going through the complete life-cycle. Starting with the UI design in Microsoft Blend going to VS 2008 to implement the functionality of the application.
I noticed that Microsoft Blend misses the integration with Visual Source Safe. Does any one know if VSS will be integrated into Microsoft Blend?
Microsoft always talks about integration, I believe integrating VSS into Microsoft Blend is something required very much!