Lions Den

The Code and Times of Hanan Schwartzberg

About Hanan | Hanan's CV | Contact Hanan

November 18, 2013

ASP.NET Relative Urls (~/) Not Rendering Correctly in a HyperLink ImageUrl

Filed under: ASP.NET,C# — Tags: , , , , , , , , , — Hanan Schwartzberg @ 2:47 pm

The Issue

When using a HyperLink object on masterpage the relative url assigned to the ImageUrl property is not resolving correctly on certain pages. The problem appears to only affect pages where a url rewrite has been used and the level of directories does not match the actual level of directories of the page. For instance, if the url “http://www.mysite.com/categories/book.aspx” actually loads “http://www.mysite.com/products/categories/default.aspx?type=books”, the the below hyperlink will resolve as “../products/images/bbb_logo.gif”. On all other pages it will correctly resolve as “../images/bbb_logo.gif”.

<asp:HyperLink runat="server" ID="hlBBB" Target="_blank" 
        NavigateUrl="http://www.bbb.org" ImageUrl="~/Images/bbb_logo.gif" />

Attempting to resolve the url manually in the code behind doesn’t work either. The following code results in the same incorrect url.

hlBBB.ImageUrl = ResolveClientUrl("~/images/bbb_logo.gif");

The Solution

There are two ways to work around this.

  1. This bug seems to be exclusive to the HyperLink object. Remove the image from the HyperLink and the ImageUrl will resolve correctly. For instance, the above HyperLink should be rewritten as:
    <asp:HyperLink runat="server" ID="hlBBB"
        NavigateUrl="http://www.bbb.org">
        <asp:Image runat="server" ID="imgBBB" ImageUrl="~/Images/bbb_logo.gif" />
    </asp:HyperLink>
  2. If you really want to leave the image in the HyperLing object. You can work around the problem in the code behind. The ResolveClientUrl method doesn’t work because takes into account the page’s location in the site, which between the rewrite and the actual location of the page seems to be inaccurate. Instead use VirtualPathUtility.ToAbsolute to resolve the url. This method can be used anywhere on the site, not just on pages and user controls and will resolve the url with an absolute path (“/images/bbb_logo.gif”).

August 10, 2009

Show a Wait Message While Generating a File for Download

Filed under: ASP.NET,C# — Tags: , , , , — Hanan Schwartzberg @ 1:31 pm

Scenario

A page has a list of images for the user to choose from. When a file is selected the request is processed via an AJAX call that directs the source of an iFrame to another page where the image is generated and passed back to the client.

Due to the size of some of the images, the generation can take a noticeable amount of time. To make sure the user does not think the site is frozen a wait message is needed. Since the generating page only returns the image, we need another way to notify the client when the file is generated and the wait message can be hidden.

(more…)

June 7, 2009

ComboBox ClientSide onChange Event

Filed under: Ajax Control Toolkit,ASP.NET,C#,Client Side — Tags: , , , , , , — Hanan Schwartzberg @ 4:57 pm

The Issue

The comboBox has a couple of server side events to deal with field changing.

  • OnSelectedIndexChanged
  • OnTextChanged

How can we catch the comboBox changing on the client side?

The Solution

Without access to these function on the client side, we need to use the client side events of the textBox that is contained within the comboBox. As with any textBox this one also has the onChange event, but the event does not run when the text was changed and the focus moved to another field. There are a few options:

  • onKeyPress
  • onKeyUp
  • onBlur

The problem with onKeyPress is when it gets to the javascript function the value of the textBox is has not changed yet, leaving onKeyUp and onBlur. While onKeyUp will give the desired effect when the user presses a key on the keyboard, part of the point of a comboBox is the ability to use the mouse, which will not be captured by this event, leaving onBlur as the remaining option. It’s still not perfect, since the event won’t run until the focus is moved off the comboBox, but it appears to be the best option. In some cases you may want to use both onKeyUp and onBlur to get the best of both events, but be careful because when the user uses the keyboard and the moves the focus your event will run twice.

In the example below I have a comboBox named cmbExample and a javascript function that accepts the textBox calling it as a parameter, cmbExample_OnBlur(textBox). I add the onBlur event to the textBox in the Page_Load method in the code behind.

protected void Page_Load(object sender, EventArgs e)
{
    TextBox textBox = cmbExample.FindControl("TextBox") as TextBox;
 
    if (textBox != null)
    {
        textBox.Attributes.Add("onBlur", "cmbExample_OnBlur(this);");
    }
}

May 14, 2009

Maintaining Dynamically Added Controls

Filed under: ASP.NET,C# — Tags: , , , , , , — Hanan Schwartzberg @ 5:45 am

Two Questions

  1. How can I keep a dynamically added control from disappearing on postBack?
  2. When in the life cycle of the page should I do this?
Click Here to download this solution

(more…)

March 16, 2009

Conditional Where in LINQ

Filed under: C#,LINQ — Tags: , , , — Hanan Schwartzberg @ 11:38 pm

Recently I needed to build a LINQ query with a conditional where clause. After a bit of searching I found a few solutions. In essence they all seem to come down to two methods. The first method is to run a second LINQ query to filter the results from the first one. The second method is to run a totally separate LINQ query for each scenario. In deciding which to use I wrote a test program that pit these two methods against each other and found that the latter was noticeably more efficient (when running the query 5000 times).

(more…)

Home | Site Design | Banner Design | Code Den | Offsite Posts | Downloads | Photography | About Hanan | Hanan's CV | Contact Hanan
Copyright © 2009 Hanan Schwartzberg. All rights reserved.