2

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).

4

4 回答 4

7

一个 ASP.NET 页面上可以有多个表单标记。限制在于服务器端 (runat="server") 表单标签。

只要只有一个具有 runat="server" 属性并且一个不包含在另一个中,您就可以实现两个(或多个)表单标签。例子:

<body>
<form action="http://www.google.com/cse" id="cse-search-box"> ... </form>
<form runat="server" id="aspNetform"> ... </form>
<body>
于 2008-09-16T18:08:36.270 回答
2

您可以有多个表单标签,但请注意它们不能嵌套。在那种情况下,您会遇到各种奇怪的情况(例如,我见过嵌套表单的开始标记显然被忽略,然后它的结束标记最终关闭“父”表单的情况)。

于 2008-09-16T18:13:08.943 回答
1

您需要删除表单标签并使用 javascript 发送查询。看看 http://my6solutions.com/post/2009/04/19/Fixing-Google-Custom-Search-nested-form-tags-in-asp-net-pages.aspx

我也包含了之前和之后的代码。因此,您可以看到我为将它与 blogengine .net 集成所做的工作。

于 2009-04-20T08:05:04.233 回答
0

您可以使用 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 );" />
于 2008-09-16T18:21:21.760 回答