Richard Hoffman
Richard Hoffman

Reputation: 139

Accessing html form elements with runat=server

I have a select box in an asp.net form like so:

<form action="Process.aspx" method="post">
  <select  name="referer" id="refererSelect" runat=server>
    <option value="">Please select one ...</option>
    <option value="customer">Return Customer</option>
    <option value="referral">Client Referral</option>
    <option value="trade">Trade Show</option>
    <option value="search">Search Engine</option>
    <option value="ad">Advertisement</option>
</select>

This way I can set a session variable with the same name in Process.aspx:

private static string[] keys = {"name", "email", "company", "budget", "referer", "services", "comment"};

protected void saveToSesh()
{
    System.Collections.Specialized.NameValueCollection form = Request.Form;
    foreach (string key in keys)
    {
        Session.Add(key, form[key]);
    }
}

, and then set the value of the select box when the user returns later like so:

string referer = (string) Session["referer"];
foreach (ListItem i in refererSelect.Items)
    if (referer == i.Value)
        i.Selected = true;

The problem is when the form is sent to the server, the form key gets changed to "ctl00$content$refererSelect". Is there any way to fix this?

Upvotes: 1

Views: 576

Answers (1)

Brian MacKay
Brian MacKay

Reputation: 32019

It seems like your issue probably lives wherever the session variable 'referer' is assigned.

I think you're assigning MyControl.UniqueID when you actually need MyControl.ID (which does not have all the naming container-related goo).

Read this to learn more about how that all works.

Upvotes: 1

Related Questions