cdub
cdub

Reputation: 25701

Form and QueryStrings in ASP.Net

I have a form on home.aspx that is this:

                    <form name="search" method="post" action="searchresults.aspx" id="searchform" runat="server">
                        <div class="searchField">
                            <input name="keywords" type="text" id="keywordSearch" value="Enter keywords" class="watermark" />
                        </div><!--end searchField-->
                        <div class="advanceSearchBox">
                            <p><b>Narrow results by:</b></p>

                            <asp:Literal ID="ltrlPopulation" runat="server" />

                            <asp:Literal ID="ltrlDatasource" runat="server" />  

                        </div><!--end advanceSearchBox-->
                        <div style="float: right; margin-right: 2px;">
                            <asp:ImageButton ImageUrl="images/go_up.png" AlternateText="GO" Width="34" Height="24" id="keywordSearchGO" runat="server" />
                        </div>
                    </form>

And on my searchresults.aspx.cs page I have this QueryString but its always empty:

 Response.Write(Request.QueryString["keywords"]);

Did I forget something?

Upvotes: 1

Views: 3884

Answers (4)

Yuck
Yuck

Reputation: 50825

You could change the opening form tag to:

<form name="search" method="get" action="searchresults.aspx" id="searchform" runat="server">

If you really want to be using query string for some reason.

Upvotes: 3

James Johnson
James Johnson

Reputation: 46047

You're posting to searchresults.aspx, so you will need to access your posted variables through Request.Form or Request.Params.

string keyWords = Request.Form["keywords"];

OR

string keyWords = Request.Params["keywords"];

Upvotes: 1

csano
csano

Reputation: 13676

A post doesn't generate a query string. To access post data, you'll need to do so through the Form or Params property.

Request.Form["keywords"];
Request.Params["keywords"];

Upvotes: 2

Robert
Robert

Reputation: 8767

That's because QueryString is for GET requests, not POST. You want to use Request.Form for posted data.

Response.Write(Request.Form["keywords"]); 

Read more documentation for the Request.Form Collection here.

Upvotes: 3

Related Questions