在ASPX页面上实施Google自定义搜索的最佳方法
时间:2020-03-05 18:56:41 来源:igfitidea点击:
Google自定义搜索代码作为表单标签提供。但是,Asp.net只允许页面上有一个表单标签。实施其代码的最佳方法是什么,以便可以将其包括在aspx页面上(例如,作为母版页或者导航元素的一部分)。
解决方案
回答
我们可以在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>
回答
我们可能可以有多个表单标签,但是请注意,它们不能嵌套。在这种情况下,我们会遇到各种各样的怪异现象(例如,我见过这样的情况:嵌套表单的开始标签显然被忽略,然后其结束标签逐渐关闭"父"表单)。
回答
我们可以使用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 );" />
回答
我们需要删除表单标记,然后使用javascript发送查询。看一下
http://my6solutions.com/post/2009/04/19/Fixing-Google-Custom-Search-nested-form-tags-in-asp-net-pages.aspx
我也包括了之前和之后的代码。因此,我们可以看到我为将其与Blogengine .net集成所做的工作。