Archive for the ‘C#’ Category

Encode HyperLink in GridView using Custom Expression Binding

In my C# Web Application, I have a GridView with a TemplatField. Within the TemplateField is a HyperLink control. My goal is to bind this HyperLink control using Custom Expression Binding. I want the NavigateUrl to point to:

Visit.aspx?name=XXX

XXX in the link above is the database column name. My first instinct is to use the Custom Expression Binding method called Eval() to bind this column. An example of the use of this semi-working method below should be included in the <asp:HyperLink> control.

<asp:HyperLink NavigateUrl=’<%# “Visit.aspx?name=” + Eval(“name”) %>’></asp:HyperLink>

I say this semi-works because the evaulted “name” must not contain any special characters like the ampersand (&). Then the next idea is trying to encode the Eval(“name”). This however was not entirely clear. After researching, I found the following solution.

<asp:HyperLink NavigateUrl=’<%# “Visit.aspx?name=” + HttpUtility.UrlEncode(Eval(“name”).ToString()) %>’></asp:HyperLink>

And this is how to properly encode a hyperlink url that is bound using Custom Expression Binding. Though this may not be visible from the browser, the web page source will reveal that the ampersand (&) has been replaced with %26, which is its equivalent.

The last step on the following page my require decoding the encoded url.

Unable to add to the Web site. Unable to add file. Access is denied.

While taking over a C# Web Application that had been migrated to work with Visual Studio 2008 (originally developed and migrated from Visual Studio .NET 2003), I occasionally ran into issues while trying to publish the web application to the production server (or any server for that matter). In the output log, there were multiple error messages, all stating:

Unable to add ‘XXX.aspx’ to the Web site. Unable to add file ‘XXX.aspx’. Access is denied.

Each XXX.aspx would be a different files. What I learned after browsing and searching Google is to make sure the files on the production (or target) server are not marked as Read Only. The solution was to:

  1. Go to the Publish directory
  2. Highlight all files and folders
  3. Right click and select Properties
  4. Make sure the Read-only attribute checkbox is unchecked (white and not gray or checked)

References:
http://forums.asp.net/p/1074678/3431238.aspx#3431238

Programmatically Submit Form on Enter Key in C#

By default, a majority of the pages and all the functions on a C# Web Application page are all contained within a single form element. Therefore, there may be multiple buttons or other postback controls within a single form. And this may be the reason why there isn’t by default a control which is automatically called when the web users hits the ENTER button.

To Assign a Default On a Standard C# Webpage

This is the default way and involves the name of the control. If you have a ASP Button called btnSearch, the code you would include in the Page_Load function.

this.Form.DefaultButton = “btnSearch”;

To Assign a Default On a Content Page (from a MasterPage) C# Webpage

If your Web application uses MasterPages and you want to assign a control to submit the form on the Enter key, you concept is the same as above, but you’ll need to pass the UniqueId of the control. Assume a control called btnSearch on a ContentPage, add the following to the Page_Load function.

this.Form.DefaultButton = btnSearch.UniqueId;

Some benefits to programmatically assigning the Enter key is your ability to change which Postback function to assign based on how the web user enters the page. Other than that, assigning the default submit control is a major convenience factor for web users.

References:
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx