All HTML elements can live server side in ASP.NET

In doing a search for something the other day I discovered that its a popular misconception that if an HTML tag doesn’t have an ASP.NET equivalent then you are forced to work with that tag purely in JavaScript. This is not the case as any HTML tag can have a runat="server" attribute. It’s just that the tag is then cast as an HtmlGenericControl.

I’m not advocating frames as a design construct but let’s say you have a search box on your website and you want to pass off the entered criteria to a popup which is essentially two frames, a header that brands it as ‘our’ site and a bottom frame that contains a search result document from another domain. (Yes I know there are better ways of doing this. Its an example, not doctrine).

The results page from the other domain expects http://www.somedomain.com/result.php?query=foo.

Firstly, we would have a bit of JavaScript on our main site that opens a popup window and loads in our frameset.

The frame set is an aspx page and expects a URL in the format http://www.mydomain.com/StockSearch.aspx?query=foo.

When that frameset loads I need to process the query string parameter so that I can use it formulate the src property of the search results frame. So the first thing to do is to create the frameset like so:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StockSearch.aspx.cs" Inherits="StockSearch" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Stock Search</title>

    <frameset rows="64,*" frameborder="0" framespacing="0" framepadding="0">
        <frame src="pop_header.html">
        <frame runat="server" id="searchFrame" />
    </frameset>
</head>

<body>
    <form id="form1" runat="server">
        <div>

        </div>
    </form>
</body>
</html>

Two things to note. Note that the second frame (the one that I want to get at) has the runat="server" attribute. Also note that it is ‘closed’ (i.e. <frame />). The closing slash is important otherwise .NET kicks up about an end of file error. All server side tags must have either a matching close tag version (i.e. <frame></frame) or a closing slash for empty tags (i.e. <frame />).

Now in the code behind I can quiz the passed in query string and set the src property of the second frame accordingly.

public partial class StockSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlGenericControl frame = (HtmlGenericControl)searchFrame;
        frame.Attributes.Add(
            "src",
            "http://www.somedomain.com/result.php?query=" + Request.QueryString["query"].ToString()
        );
    }
}

You can use a similar technique for any HTML tag that lacks an ASP.NET equivalent.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s