ASP.NET:在搜索表单的操作页面中读取表单变量值
我有一个网站,我想在其中实现搜索功能。因此,我添加了以下代码,以便在我的 html 页面中添加一个搜索框。
<form id="search" method="post" action="Results.aspx">
<input id="txtSearchKey" type="text" name="txtSearchKey" />
<input id="Submit1" type="submit" value="submit" /><br />
<br />
</form>
在 Results.aspx 中,我想读取用户在 txtSearchKey 文本框中输入的值。做到这一点的理想方法是什么?我用过
string strKey = Request.Form["txtSearchKey"].ToString();
但它抛出空引用异常。
我不想让所有页面都在 ASP.NET 中。我只想将结果页面作为 ASP.NET
I have a website where i want to implement search functionality.So i added the below code to have a search box in my html page
<form id="search" method="post" action="Results.aspx">
<input id="txtSearchKey" type="text" name="txtSearchKey" />
<input id="Submit1" type="submit" value="submit" /><br />
<br />
</form>
In Results.aspx, I want to read the value user has entered in the txtSearchKey text box. What is the ideal way to do this ? I used
string strKey = Request.Form["txtSearchKey"].ToString();
But it throw a null reference exception.
I don't want to have all pages in ASP.NET.I want to have only the result page as ASP.NET
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可能是因为您的文本框字段上没有 NAME 属性。这是用作 Request.Form 集合中的键的值。我认为没有 name 属性的输入字段将不会被提交。
例如:
Could be because you do not have a NAME attribute on the textbox field. That's the value that is used as the key in the Request.Form collection. An input field without a name attribute will not be submitted, I think.
e.g.:
您可以通过以下方式获取 txtSearchKey 字段:
但是,您可以使用具有 PostBackUrl 属性的按钮,而不是使用表单操作将搜索转发到另一个页面,如下所示:
因为在 ASP.NET 中,不允许有多个表单。
You can get your txtSearchKey field by this :
But, instead of using form action to forward your search to another page, you can use a button with PostBackUrl property like that :
Because in ASP.NET, to have more then one form is not allowed.
您是否有任何原因不使用
表单 runat="server"
,然后在此表单中拖动 TextField 和 Button 。然后双击该按钮并编写您想要的代码。
如果您想按照自己的方式进行操作,则需要提供您的 sa name="txtMySearchKey" 才能正常工作
Is there any reason you don't use
form runat="server"
and then drag a TextField and a Button in this form. Then doubleclick the button and write code you want.
If you want to do it your way you need to give your s a name="txtMySearchKey" for it to work
您处理事情的方式并不是真正在 ASP.NET Web 表单中工作的方式。首选方法是使用 ASP.NET 服务器控件和事件来抽象您要实现的过程。例如,您的表单实际上应该是这样的(请注意 runat="server" 属性,该属性使您能够以编程方式引用控件):
然后,在后面的代码中,您将处理
ButtonSubmit_Click
事件,例如这样您就可以从TxtSearchKey
文本框中获取值:请参阅 TextBox 控件的快速入门示例 了解更多信息。
The way you are going about things is not really the way you work in ASP.NET web forms. The preferred way is to use asp.net server controls and events to abstract the process you are trying to achieve. For instance, your form should really be something like this (note the runat="server" attribute that enables you to reference the controls programmatically):
Then, in your code behind you would handle the
ButtonSubmit_Click
event like this to enable you to get the value from theTxtSearchKey
textbox:See the Quickstart example for the TextBox control for more info.
只是不要在 Request.form 之后使用 .toString() ...之后它不会给出空引用。
Just do not use .toString() after the Request.form... it will not give a null reference after that.