Archive

Posts Tagged ‘.Net’

Custom Places in FileDialog box

June 21st, 2010 Anuraj P No comments

If you ever tried to Open a File from Visual Studio, you may notice something like Projects Folder in the Open File Dialog.

Open File Dialog in Visual Studio

Open File Dialog in Visual Studio

We can also implement the same functionality in our applications by using CustomPlaces collection property of FileDialog class. The OpenFileDialog and SaveFileDialog classes allow you to add folders to the CustomPlaces collection.

openFileDialog.CustomPlaces.Add(@"C:\Users");

You can also specify GUID of Windows Vista known folder. Known Folder GUIDs are not case sensitive and are defined in the KnownFolders.h file in the Windows SDK. If the specified Known Folder is not present on the computer that is running the application, the Known Folder is not shown.

openFileDialog.CustomPlaces.Add(@"C:\Users");
//Desktop Folder
openFileDialog.CustomPlaces.Add(new Guid("B4BFCC3A-DB2C-424C-B029-7FE99A87C641"));
//Downloads Folder
openFileDialog.CustomPlaces.Add(new Guid("374DE290-123F-4565-9164-39C4925E467B"));
Open File Dialog with Custom Places

Open File Dialog with Custom Places

Note: This feature will not have any effect in Windows XP. Also you must set the AutoUpgradeEnabled to True(default) to enable this feature in Vista or Windows 7.

The following table lists few Windows Vista Known Folders and their associated Guid.

  1. Contacts : 56784854-C6CB-462B-8169-88E350ACB882
  2. ControlPanel : 82A74AEB-AEB4-465C-A014-D097EE346D63
  3. Desktop : B4BFCC3A-DB2C-424C-B029-7FE99A87C641
  4. Documents : FDD39AD0-238F-46AF-ADB4-6C85480369C7
  5. Downloads : 374DE290-123F-4565-9164-39C4925E467B
  6. Favorites : 1777F761-68AD-4D8A-87BD-30B759FA33DD
  7. Fonts : FD228CB7-AE11-4AE3-864C-16F3910AB8FE
  8. Music : 4BD8D571-6D19-48D3-BE97-422220080E43

ASP.Net Default button and Master Pages

June 21st, 2010 Anuraj P 3 comments

Last few days I was(am) busy with one pure ASP.Net application(why its pure because we are not using any 3rd party controls, ajax nothing. Everything is postbacking ;) ). Today I got a weird bug from one of my QC team saying when ever they presses ENTER button, focus is on any textbox, it popups Help window from our application. In our application, end users can open the Help window, by clicking on some help images icons provided in the application. These images are displayed using ASP Image button controls (If you are using ASP Image controls this problem will not occur, but in our application, we need to disable it some scenarios, so can’t use ASP Image). After searching I found the issue with the default button property of ASP.Net, which helps us to submit the form using Enter Key. In my page ASP.Net considering the Help image button as default button for the Page. We can set the default button to our submit button using various methods. Set the default button property of the FORM, or create a Panel control over the controls and set the default button property of the Panel.

<form id="form1" runat="server" defaultbutton="cmdSubmit">
<asp:Panel runat="server" ID="plMain" DefaultButton="cmdSubmit">

Form tag also supports “defaultfocus” this property allows to focus to control, on Page Load.

<form id="form1" runat="server" defaultbutton="cmdSubmit" defaultfocus="txtUser">

If you are using Master Pages, there will be some slight difference because, the Form tag will not be available in the content Pages. It can fix using code behind, using FindControl method.

(Page.Master.FindControl("Form1") as HtmlForm).DefaultButton = this.cmdSubmit.UniqueID;

Also you can do something like this

Page.Master.Page.Form.DefaultButton = cmdSubmit.UniqueID;

Happy Coding :)

Pass your own arguments to the ClientValidationFunction in a CustomValidator

May 30th, 2010 Anuraj P No comments

Last few days I am working on an ASP.Net, where I can create Grids, Dropdown lists with Post back :) , and the best part is browser compatibility, client only requires compatible with IE. Yes it was a refresher course for me, I revisited GridView templates, Page.IsPostback checking etc. Today I got a requirement like I need to validate few radio buttons. I searched for an implementation, I found we can use required field validator for RadioButtonList, but in my case it was Radio buttons (Initially it was RadioButtonList, but due to some customization issues, I changed it to Radio Buttons). Then I implemented a custom validator, and in the client validation function I hardcoded the Radio Button ids, because for the client validation function we can’t pass the parameters to the function. Same problem again I faced in a GridView, where the problem I found was I won’t get the client id of the radio button in the Item template. :( After doing some research I found we can extend the custom validator control using Validation Framework. And here is the implementation. Using this we can add properties to the source / sender parameter in the client side validation function.

function ValidateRadioButtons(source, arguments) {
    var radio1 = document.getElementById(source.Radio1);
    var radio2 = document.getElementById(source.Radio2);
    var radio4 = document.getElementById(source.Radio4);
    var radio3 = document.getElementById(source.Radio3);
    var result = radio1.checked || radio2.checked || radio3.checked || radio4.checked;
    arguments.IsValid = result;
}

This is the JavaScript function which implements the validation logic. You can get the controls as part of the Source object. To get the Source.Radio1, you need to register the Radio1 as ClientValidator attribute. You can do some C# code like this.

protected void Page_Load(object sender, EventArgs e)
{
    this.Page.ClientScript.RegisterExpandoAttribute(customCheck.ClientID, "Radio1", this.radio1.ClientID, false);
    this.Page.ClientScript.RegisterExpandoAttribute(customCheck.ClientID, "Radio2", this.radio2.ClientID, false);
    this.Page.ClientScript.RegisterExpandoAttribute(customCheck.ClientID, "Radio3", this.radio3.ClientID, false);
    this.Page.ClientScript.RegisterExpandoAttribute(customCheck.ClientID, "Radio4", this.radio4.ClientID, false);
}

And the ASPX Code is like this

<asp:CustomValidator runat="server" ID="customCheck" ClientValidationFunction="ValidateRadioButtons"
        ErrorMessage="Choose any option" />

And here is the Output code generated for the custom validator.

Custom validator output rendering

Custom validator output rendering

Happy Coding.

Deploying WCF Service in IIS : no svc MIME Type

April 23rd, 2010 Anuraj P No comments

Few days back I developed a Silverlight application, which uses a WCF Service to communicate to Database. After the development when I tried to deploy the WCF Service on my IIS, it was displaying some error like

Server Error in Application “Default Web Site/SampleApp”
HTTP Error 404.3 – Not Found
Description: The page you are requesting cannot be served because of the Multipurpose Internet Mail Extensions (MIME) map policy that is configured on the Web server. The page you requested has a file name extension that is not recognized, and is not allowed.
Error Code: 0×80070032
Notification: ExecuteRequestHandler
Module: StaticFileModule
Requested URL: http://localhost:80/SampleApp/CoreService.svc
Physical Path: C:\Users\Documents\Visual Studio 2005\Projects\SampleApp\SampleApp\CoreService.svc
Logon User: Anonymous
Logon Method: Anonymous
Handler: StaticFile

I tried IIS MimeTypes and I couldn’t found an application mapping for SVC file. After doing some searching I found the fix. It is happening because of not registering the WCF in IIS. You can resolve this using WCF Installation utility which comes with .Net Framework 3.0. Run the command prompt as Administrator, go to the Windows Communication Foundation folder in c:\Windows\Microsoft.NET\Framework\v3.0\ folder. Execute the servicemodelreg.exe with -i or /i switch. After the installation try reloading the Page. It will fix this issue.

Convert string to enum

April 19th, 2010 Anuraj P No comments

Sometimes we may need to read a Config value using ConfigurationSettings.AppSettings and assign it to a Enum. This function helps to convert a string to Enum.

/// <summary>
/// Function to retrive Enum value by giving the corresponding string value.(This method is case sensitive)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The value.</param>
/// <returns>Enum value</returns>
public static T ToEnum<T>(string value)
{
    //This method will throw error if the case of the value is different from the
    //enum value.
    if (Enum.IsDefined(typeof(T), value))
    {
        return (T)Enum.Parse(typeof(T), value, true);
    }
    throw new ArgumentException("Value doesn't exists in the Enum");
}
Categories: .Net, .Net 3.0 / 3.5, ASP.Net Tags: , , ,

How To Convert XMLNodeList To DataTable

April 14th, 2010 Anuraj P No comments

After doing an XPath selection using .Net, using XmlDocument.SelectNodes(), it will return XMLNodeList class, sometimes we required to convert the XMLNodeList to Data Table for binding purposes. I couldn’t find a direct way, only way is using looping through the Nodes.

/// <summary>
    /// Converts an XMLNodelist object to data table.
    /// </summary>
    /// <param name="xmlNodeList">The XMLNodeList to convert to DataTable.</param>
    /// <param name="Columns">The columns required for the DataTable, using this parameter the
    /// data will be fetched from xmlnodelist.</param>
    /// <returns>DataTable with columns specified in the Columns parameter</returns>
    public DataTable XmlNodeListToDataTable(XmlNodeList xmlNodeList, string[] Columns)
    {
        //Creating the DataTable.
        using (DataTable dataTable = new DataTable("DataTable"))
        {
            //Adding data Table columns based on the columns parameter
            foreach (string column in Columns)
            {
                dataTable.Columns.Add(column);
            }
            //Adding rows with values.
            DataRow dataRow;
            foreach (XmlNode node in xmlNodeList)
            {
                dataRow = dataTable.NewRow();
                foreach (string column in Columns)
                {
                    dataRow[column] = node.SelectSingleNode(column).InnerText;
                }
                dataTable.Rows.Add(dataRow);
            }
            return dataTable;
        }
    }

You can also create the columns dynamically, but you need to check for duplicate Data Columns.