Sep 01 2006

NameValueCollection databinding

Posted by admin under ASP.NET articles

If it's something I never seems to remember it has to be the different Request.ServerVariables. Recently I was doing some work for a client and needed to get the referer url and compare it to the clients domain name.

While it sounds easy, and indeed it was so easy it doesn't qualify for an article here, I needed some information from Request.ServerVariables and while googling I hit me that know I should once and for all create my own article on it. Just a simple list with example and description. So, coding first :) - I started up VS2005 and throw in an GridView and had it databound to the collection:



Gridview1.DataSource = Request.ServerVariables;
Gridview1.DataBind();

I almost blushed - this was too easy - the only problem was that when running it it didn't work out as expected:

Just a single column is shown. Turns out that the NameValueCollection enumerator just returns the keys - and you are then supposed to retrieve the  value yourself.

There are more than one solution for this, you could for example create a convert function which converts the Request.ServerVariables to something like a hashtable, but but here's how I did it (not saying one is better than another):


<%@ Page Language="C#" %>

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

<script runat="server">


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = Request.ServerVariables ;
            GridView1.DataBind();
        }
    }



    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            NameValueCollection oColl = GridView1.DataSource as NameValueCollection;
            Label lblName = e.Row.FindControl("lblName") as Label;
            lblName.Text = e.Row.DataItem.ToString();

            Label lblValue = e.Row.FindControl("lblValue") as Label;
            lblValue.Text = oColl[lblName.Text];
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:Label ID="lblName" runat="server"></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:Label ID="lblValue" runat="server"></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>
    
    </div>
    </form>
</body>
</html>

By accepting the fact that we only get the key as DataItem from the NameValueCollection, then we use the datasource (which is a NameValueCollection) to retrieve the value in RowDataBound:

if (e.Row.RowType == DataControlRowType.DataRow) { NameValueCollection oColl = GridView1.DataSource as NameValueCollection; Label lblName = e.Row.FindControl("lblName") as Label; lblName.Text = e.Row.DataItem.ToString(); Label lblValue = e.Row.FindControl("lblValue") as Label; lblValue.Text = oColl[lblName.Text]; }