You probably have this form nested inside another form. So earlier in your page you probably have a <form runat="server"> in there. That doesn't work, ad you can't have nested forms.
I know a way how to solve this, but I'm not sure if it's the best solution. I place my fields inside the main form (with runat=server) and when the search button is pressed I submit the main form to another page. Here is a macro sample I'm using:
namespace Reeleezee.WebApplication.UserControls
{
public partial class WebsiteTop : BaseUserControl
{
#region Properties
public int SearchLink
{
get;
set;
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
BtnSearch.OnClientClick = string.Format("document.getElementById('aspnetForm').action='{0}';", library.NiceUrl(SearchLink));
}
}
}
As you can see I use javascript to post the form to the new link. You have to make sure you give your main form the correct id:
<form runat="server" id="aspnetForm">
On the other page you can get your search results from the Request.Form:
//Dont use !IsPostBack because when we redirect to this page we don't know if it's a postback or not.
//Get the textbox from the WebsiteTop usercontrol.
TextBox txtSearch = this.Page.FindControlRecursive<TextBox>("TxtSearch");
//Get the search value from the form.
Search = Request.Form[txtSearch.UniqueID];
//Set the posted value inside the textbox again.
txtSearch.Text = Search;
Btw sun do you work for Tribal China? If so I work for Digibiz and you can just get this code from svn :).
Jeroen came across some stuff I did recently and asked that I post as he thought it might be helpful.
As Sebastiaan pointed out, you can't have nested forms, and thanks to the ASP.NET, you can only have one form for the page. This means that you can't have a separate search form that posts to a different page. But for a search, you don't really want to do a post; more likely a GET is better (bookmarking and analytics).
This makes the above code from Jeroen change (in the code behind):
protected void Page_Load(object sender, EventArgs e)
{
BtnSearch.OnClientClick = string.Format(
"document.location.href ='{0}?q=' + encodeURIComponent($('#{1}').val()); return false;",
library.NiceUrl(SearchLink), // SearchLink is the nodeId of the page that will do the search
TxtSearch.ClientID); // This is the search terms text box control
}
(Note JQuery is required for this solution to work; use document.getElementById if you're not using it.)
Then in the search page, you only need to get the search terms from the query string: Request.QueryString["q"]. Be sure to check for null!
Now I'll freely admit the the above isn't a very clean solution. If you look at the generated code, you'll see that the "return false" in the ClientClick code causes the ASP.NET Post code to be skipped (intentionally).
I could have changed the asp:Button to a link, and added a JQuery click handler to that link instead, and I could also have used a straight input instead of an asp:TextBox control.
I want to make a submit form in HomePage, but it can't goto search.aspx page.
code like this:
<form method="post" action="search.aspx" id="RunwayMasterForm">
<input name="search" type="text" class="input" value="" style="width: 65%;" />
<input class="art-button" type="submit" name="search" value="" onclick="window.location.href='search.aspx'" />
</form>
hope some one can give me some help.
You probably have this form nested inside another form. So earlier in your page you probably have a <form runat="server"> in there. That doesn't work, ad you can't have nested forms.
yes, I put this form in a template that support canvas mode. Is there another way to do this?
I know a way how to solve this, but I'm not sure if it's the best solution. I place my fields inside the main form (with runat=server) and when the search button is pressed I submit the main form to another page. Here is a macro sample I'm using:
.ascx:
.cs:
As you can see I use javascript to post the form to the new link. You have to make sure you give your main form the correct id:
On the other page you can get your search results from the Request.Form:
Btw sun do you work for Tribal China? If so I work for Digibiz and you can just get this code from svn :).
Jeroen
Hi,
Jeroen came across some stuff I did recently and asked that I post as he thought it might be helpful.
As Sebastiaan pointed out, you can't have nested forms, and thanks to the ASP.NET, you can only have one form for the page. This means that you can't have a separate search form that posts to a different page. But for a search, you don't really want to do a post; more likely a GET is better (bookmarking and analytics).
This makes the above code from Jeroen change (in the code behind):
(Note JQuery is required for this solution to work; use document.getElementById if you're not using it.)
Then in the search page, you only need to get the search terms from the query string: Request.QueryString["q"]. Be sure to check for null!
Now I'll freely admit the the above isn't a very clean solution. If you look at the generated code, you'll see that the "return false" in the ClientClick code causes the ASP.NET Post code to be skipped (intentionally).
I could have changed the asp:Button to a link, and added a JQuery click handler to that link instead, and I could also have used a straight input instead of an asp:TextBox control.
The link solution would look something like:
Sometimes the single form limitation is quite frustrating, but if you don't mind using javascript you can always work around it.
Hope this helps,
JT
Thanks for you all. I do it only with javascript, no jquery.
<input type="text" value="" id="searchTerms" />
<a href="#" id="searchLink" onclick="document.location.href='/search.aspx?search='+encodeURIComponent(getElementById('searchTerms').value)" >search</a>
Nice ! This is exactly what I´ve been looking for ! Thank you guys !
is working on a reply...