Posts Tagged ‘C#’

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.

Trim a String in C#

Anytime a web form accepts user input that is destined for a database, it is (in the majority of the cases) important to trim string inputs. Without knowing, some users may cut and paste strings from one source to your form. For one reason or another, the cut and paste process may carry along unwanted extra spaces or other delimited not easily detected by the human eye. In C#, there are a few ways to handle this:

Let us assume string myString = “  Allied, agents   \r\n\r\n”;

myString.Trim()
This overload of the Trim method will remove whitespaces from both ends of the string. This is by far the most commonly used form of the method used to clean up user inputs. Often times, copy/pasted items contain a trailing space which can cause a lot of issues if not checked for.

String Output of “Allied, agents”.

myString.Trim(‘,’, ‘a’)
This overload of the Trim method will remove only the comma and lowercase letter a from either the beginning or the end of the string. Therefore, in this case, the string output will be exactly the same. Not that the letters specified are case sensitive. Additionally, you are allowed an unlimited number of characters.

String Output =  “  Allied, agents   \r\n\r\n”

TrimEnd(‘,’, ‘a’)
This overload does the exact same thing as Trim(‘,’,'a’), except that the letters are only trimmed from the end of the string.

TrimStart(‘,’, ‘a’)
This overload does the exact same thing as Trim(‘,’,'a’), except that the letters are only trimmed from the beginning of the string.

“Maximum request length exceeded” in C#

In C# ASP.NET, I kept getting the following error message:

HttpException (0×80004005): Maximum request length exceeded.

To make matters worst, this is a type of error that catch does not recognize. In the end, I found a simple solution.

By default, the request length of any request is 2048kb. To increase the length, you need to add the following script to your web.config file and specify a length.

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="32768" />
    </system.web>
</configuration>

With this solution, my web application is now able to accept files larger than 2mbs (and can actually accept a much larger 32mb). This application is an intranet web application used by trusted users.