views:

1170

answers:

5

Google custom search code is provided as a form tag. However, Asp.net only allows a single form tag on a page. What is the best way to implement their code so you can include it on an aspx page (say as part of a Masterpage or navigation element).

+5  A: 

You can have multiple form tags on an ASP.NET page. The limitation is on server-side (runat="server") form tags.

You can implement two form tags (or more) as long as only one has the runat="server" attribute and one is not contained in the other. Example:

<body>
<form action="http://www.google.com/cse" id="cse-search-box"> ... </form>
<form runat="server" id="aspNetform"> ... </form>
<body>
Chris Pebble
+2  A: 

You may be able to have multiple form tags, but note that they cannot be nested. You'll run into all kinds of weirdness in that scenario (e.g., I've seen cases where the opening tag for the nested form apparently gets ignored and then its closing tag winds up closing the "parent" form out).

Eric Longman
A: 

You could use Javascript:

<input name="Query" type="text" class="searchField" id="Query" value="Search" size="15" onfocus="if(this.value == 'Search') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Search'; }" onkeydown="var event = event || window.event; var key = event.which || event.keyCode; if(key==13) window.open('http://www.google.com/search?q=' + getElementById('Query').value ); " /><input name="" type="button" class="searchButton" value="go" onclick="window.open('http://www.google.com/search?q=' + getElementById('Query').value );" />
Timothy Lee Russell
+1  A: 

You'll need to remove the form tag and use javascript send the query. Have a look at http://my6solutions.com/post/2009/04/19/Fixing-Google-Custom-Search-nested-form-tags-in-asp-net-pages.aspx

I have included the before and after code as well. So you can see what I've done to integrate it with blogengine .net.

seanlinmt
Thank you, it is working.
Sharique
That doeesn't seem to be a good solution. What about users that have javascript disabled?
Shea Daniels
If you can separate the 2 form tags then all is good. Otherwise ....Actually, I wonder what's the percentage of people who disables javascript these days ...
seanlinmt
I think that's not really a worry these dayshttp://stackoverflow.com/questions/121108/how-many-people-disable-javascript
seanlinmt
A: 

try this approach: http://hubpages.com/hub/Adding-Adsense-Search-to-an-ASPX-webpage

kindly notice it has some issues working on Opera, Safari & Chrome, after doing that just make sure it works as it should for these browsers.

Regards

Website-Chef