我怎样才能做 <form method="get">在 ASP.Net 中搜索表单? [英] How can I do <form method="get"> in ASP.Net for a search form?

查看:14
本文介绍了我怎样才能做 <form method="get">在 ASP.Net 中搜索表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的应用中有一个搜索表单,我希望它与 method="GET" 等效.

I have a search form in an app I'm currently developing, and I would like for it to be the equivalent of method="GET".

因此,当点击搜索按钮时,用户会转到search.aspx?q=the+query+he+entered

Thus, when clicking the search button, the user goes to search.aspx?q=the+query+he+entered

我想要这个的原因只是可添加书签的 URL,而且这样做感觉更干净.

The reason I want this is simply bookmarkable URLs, plus it feels cleaner to do it this way.

我也不希望将 viewstate 隐藏字段值附加到 URL.

I also don't want the viewstate hidden field value appended to the URL either.

我能想到的最好的方法是:

The best I could come up with for this is:

  1. 捕获按钮的服务器端点击事件和Response.Redirect.
  2. 将 Javascript onclick 处理程序附加到触发 window.location.replace 的按钮上.
  1. Capture the server-side click event of the button and Response.Redirect.
  2. Attach a Javascript onclick handler to the button that fires a window.location.replace.

两者都感觉古怪和次优...你能想到更好的方法吗?

Both feel quirky and sub-optimal... Can you think of a better approach?

推荐答案

使用普通的旧 html 表单,而不是服务器端表单 (runat=server),您确实应该能够使其工作.

Use a plain old html form, not a server side form (runat=server), and you should indeed be able to make it work.

但是,如果您有一个开箱即用的 Visual Studio 母版页,它将整个页面包装在服务器端表单中,这可能是一个问题,因为您无法嵌套表单.

This could however be a problem if you have an out of the box visual studio master page which wraps the entire page in a server side form, because you can't nest forms.

Web 表单不必很糟糕,但默认实现通常很糟糕.您不必对所有内容都使用 Web 表单.有时,普通的旧 post/get 和 process 请求代码就可以了.

Web forms don't have to suck, but the default implementations often do. You don't have to use web forms for everything. Sometimes plain old post/get and process request code will do just fine.

这篇关于我怎样才能做 <form method="get">在 ASP.Net 中搜索表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆