ASP.NET:在搜索表单的操作页面中读取表单变量值

发布于 2024-08-03 17:20:19 字数 550 浏览 7 评论 0原文

我有一个网站,我想在其中实现搜索功能。因此,我添加了以下代码,以便在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏夜暖风 2024-08-10 17:20:19

可能是因为您的文本框字段上没有 NAME 属性。这是用作 Request.Form 集合中的键的值。我认为没有 name 属性的输入字段将不会被提交。

例如:

<input id="txtSearchKey" type="text" name="txtSearchKey" />

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

<input id="txtSearchKey" type="text" name="txtSearchKey" />
ㄟ。诗瑗 2024-08-10 17:20:19

您可以通过以下方式获取 txtSearchKey 字段:

string strKey = PreviousPage.Request.Form["txtSearchKey"].ToString();

但是,您可以使用具有 PostBackUrl 属性的按钮,而不是使用表单操作将搜索转发到另一个页面,如下所示:

<asp:Button runat="server" ID="btnSearch" PostBackUrl="Search.aspx" />

因为在 ASP.NET 中,不允许有多个表单。

You can get your txtSearchKey field by this :

string strKey = PreviousPage.Request.Form["txtSearchKey"].ToString();

But, instead of using form action to forward your search to another page, you can use a button with PostBackUrl property like that :

<asp:Button runat="server" ID="btnSearch" PostBackUrl="Search.aspx" />

Because in ASP.NET, to have more then one form is not allowed.

勿忘初心 2024-08-10 17:20:19

您是否有任何原因不使用

表单 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

少跟Wǒ拽 2024-08-10 17:20:19

您处理事情的方式并不是真正在 ASP.NET Web 表单中工作的方式。首选方法是使用 ASP.NET 服务器控件和事件来抽象您要实现的过程。例如,您的表单实际上应该是这样的(请注意 runat="server" 属性,该属性使您能够以编程方式引用控件):

<form id="form1" runat="server">
    <div>
        <asp:Panel ID="PanelSearch" runat="server" DefaultButton="ButtonSubmit">
            <asp:TextBox ID="TxtSearchKey" runat="server" /><br />
            <asp:Button ID="ButtonSubmit" Text="Submit" runat="server" 
                onclick="ButtonSubmit_Click" /><br />
        </asp:Panel>
    </div>
</form>

然后,在后面的代码中,您将处理 ButtonSubmit_Click 事件,例如这样您就可以从 TxtSearchKey 文本框中获取值:

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
    string strKey = TxtSearchKey.Text;
}

请参阅 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):

<form id="form1" runat="server">
    <div>
        <asp:Panel ID="PanelSearch" runat="server" DefaultButton="ButtonSubmit">
            <asp:TextBox ID="TxtSearchKey" runat="server" /><br />
            <asp:Button ID="ButtonSubmit" Text="Submit" runat="server" 
                onclick="ButtonSubmit_Click" /><br />
        </asp:Panel>
    </div>
</form>

Then, in your code behind you would handle the ButtonSubmit_Click event like this to enable you to get the value from the TxtSearchKey textbox:

protected void ButtonSubmit_Click(object sender, EventArgs e)
{
    string strKey = TxtSearchKey.Text;
}

See the Quickstart example for the TextBox control for more info.

痴骨ら 2024-08-10 17:20:19

只是不要在 Request.form 之后使用 .toString() ...之后它不会给出空引用。

Just do not use .toString() after the Request.form... it will not give a null reference after that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文