views:

586

answers:

4

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 . Please advice

I dont want to have all pages in ASP.NET.I want to have only the result page as ASP.NET

Thanks in advance

A: 

You can get your txtSearchKey field by this :

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

But, instead of using form action to forward your search to another page, you can use a button with PostBackUrl property like that :

<asp:Button runat="server" ID="btnSearch" PostBackUrl="Search.aspx" />

Because in ASP.NET, to have more then one form is not allowed.

Canavar
I would like to have only one ASP.NET page which is going to display the results.I Want to have the rest of the pages as HTML pages
Shyju
then my first recommendation is suitable for you.
Canavar
Actually it's not. Since the previous page is plain HTML, the PreviousPage property will be empty, and this would cause an error.
patmortech
Yea. Previous page is throwing an error.Whats the other options ?
Shyju
See my answer below. Use the code you have in your original post, but make sure to put the name attribute on your form field.
patmortech
+3  A: 

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" />
patmortech
+1 Yes, I didn't notice that attribute.
Canavar
I added name attribute too.But not working then also
Shyju
Still getting a null reference exception? Also, where exactly are you putting the code to retrieve the form value (i.e., what page event handler, or is it inline)?
patmortech
A: 

Is there any reason you don't use

form runat="server"

and then drag a TextField and a Button in this form. Then doubleclick the button and write code you want.

If you want to do it your way you need to give your s a name="txtMySearchKey" for it to work

sBENdk
A: 

The way you are going about things is not really the way you work in ASP.NET web forms. The preferred way is to use asp.net server controls and events to abstract the process you are trying to achieve. For instance, your form should really be something like this (note the runat="server" attribute that enables you to reference the controls programmatically):

<form id="form1" runat="server">
    <div>
     <asp:Panel ID="PanelSearch" runat="server" DefaultButton="ButtonSubmit">
      <asp:TextBox ID="TxtSearchKey" runat="server" /><br />
      <asp:Button ID="ButtonSubmit" Text="Submit" runat="server" 
       onclick="ButtonSubmit_Click" /><br />
     </asp:Panel>
    </div>
</form>

Then, in your code behind you would handle the ButtonSubmit_Click event like this to enable you to get the value from the TxtSearchKey textbox:

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
    string strKey = TxtSearchKey.Text;
}

See the Quickstart example for the TextBox control for more info.

Dan Diplo
I was not asking for a button click event of a web page with a text box. I have only one page (to show search results) and lot of HTML pages(which should have this search box) :)
Shyju
Why are using asp.net then, if you are not going to use it's features? Your call, I suppose...
Dan Diplo