#me { float: left; }

February 21, 2008

All HTML elements can live server side in ASP.NET

Filed under: ASP.NET, Web Design — Simon Rigby @ 8:04 am

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.

February 15, 2008

ASP.NET Menu control not rendering correctly in Safari

Filed under: ASP.NET, Safari, Web Design — Simon Rigby @ 12:22 pm

I found this neat solution to a long standing problem on ASP.NET.

Add a file called safari.browser to the app_browsers folder, containing the following:

<browsers>
    <browser refID="safari1plus">
        <controlAdapters>
            <adapter controlType="System.Web.UI.WebControls.Menu" adapterType="" />
        </controlAdapters>
    </browser>
</browsers>

Thanks to micheilvoo for the solution. The original thread can be found here.

August 15, 2007

Dynamically adding stylesheets and meta data to ASP.NET pages

Filed under: ASP.NET, Web Design — Simon Rigby @ 11:04 am

Adding stylesheets to a page at runtime couldn’t be easier.

Here we load a different stylesheet based on the browser name (needless to say this could be extended to cover different versions/OSs etc.)

HtmlLink link = new HtmlLink();
link.Attributes.Add(“type”, “text/css”);
link.Attributes.Add(“rel”, “stylesheet”);

switch (Request.Browser.Browser)
{
case “Firefox”:
link.Attributes.Add(“href”, “cssFF.css”);
break;
case “IE”:
link.Attributes.Add(“href”, “cssIE.css”);
break;
}
this.Header.Controls.Add(link);

May 3, 2007

A List Apart – The Web Design Survey 2007

Filed under: Web Design — Simon Rigby @ 2:32 pm

A List Apart are currently running their 2007 survey at http://alistapart.com/articles/webdesignsurvey

Theme: WordPress Classic. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.