问题描述
我有一个 ASP.NET 页面,其中包含一个搜索页面的表单.
I have an ASP.NET page that contain a form that search the page.
是否有任何解决方案可以让我在 URL 中包含搜索文本?
Is there any solution so that I can have the search text in the URL?
我想让我的客户可以复制/粘贴搜索结果 URL.
I want to give the posibility to my clients to copy/paste the search results URL.
推荐答案
经过对这个主题的更多研究后,我认为 javascript 解决方案是最好的:
After more research abut this topic I think that the javascript solution is the best:
您可以使用 JavaScript 访问表单的 ACTION 属性.
You can access the ACTION attribute of the form using JavaScript.
<form id="myForm" action="Search.aspx" onsubmit="return setAction();">
<input id="textbox" />
<input type="submit" value="Submit" />
</form>
<script type="text/javascript">
function setAction()
{
var myForm = document.getElementById( "myForm" );
var myText = document.getElementById( "textbox" );
if (myForm && myForm.action && myText && myText.value != null )
{
myForm.action = "Search.aspx?q=" + myText.value;
}
return true;
}
</script>
就我个人而言,我不是 JavaScript 的忠实粉丝……但这不会向服务器添加额外的请求.如果您认为这有任何缺点,请告诉我.
Personally I am not a big fan of JavaScript ... but this does not add an extra request to the server. If you think that this has any drawbacks please let me know.
这篇关于URL 中的 ASP.NET Web 窗体参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!