ASP.NET : Reading form variable values in the action page of search form

Shyju picture Shyju · Sep 8, 2009 · Viewed 8.5k times · Source

I have a website where i want to implement search functionality.So i added the below code to have a search box in my html page

   <form id="search" method="post" action="Results.aspx">
    <input id="txtSearchKey" type="text" name="txtSearchKey" />
    <input id="Submit1" type="submit" value="submit" /><br />
    <br />
</form>

In Results.aspx, I want to read the value user has entered in the txtSearchKey text box. What is the ideal way to do this ? I used

 string strKey = Request.Form["txtSearchKey"].ToString(); 

But it throw a null reference exception.

I don't want to have all pages in ASP.NET.I want to have only the result page as ASP.NET

Answer

patmortech picture patmortech · Sep 8, 2009

Could be because you do not have a NAME attribute on the textbox field. That's the value that is used as the key in the Request.Form collection. An input field without a name attribute will not be submitted, I think.

e.g.:

<input id="txtSearchKey" type="text" name="txtSearchKey" />