Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 实现搜索页面_Asp.net_Search - Fatal编程技术网

Asp.net 实现搜索页面

Asp.net 实现搜索页面,asp.net,search,Asp.net,Search,我是ASP.Net新手,正在尝试在我的项目中实现搜索页面 我创建了一个简单的搜索.aspx <asp:TextBox runat="server" ID="txtSearch" MaxLength="250"/> <asp:LinkButton ID="lnkSearch" runat="server" Text="Search" OnClick="Search_Click" ClientIDMode="Static" /> <asp:Repeater ID="re

我是ASP.Net新手,正在尝试在我的项目中实现搜索页面

我创建了一个简单的搜索.aspx

<asp:TextBox runat="server" ID="txtSearch" MaxLength="250"/>
<asp:LinkButton ID="lnkSearch" runat="server" Text="Search" OnClick="Search_Click" ClientIDMode="Static" />
<asp:Repeater ID="rep" runat="server" >
....
</asp:Repeater>
问题是,当我在txtSearch中键入内容并点击enter或单击search时,页面会重新加载旧的查询字符串和旧的搜索结果,似乎txtSearch.Text在点击search\u click之前会用旧的查询值更新

例如如果我在地址栏中输入
search.aspx?q=apple
,页面将返回正确的结果和txtSearch的Text=“apple”。。如果我键入
green apple
并点击回车键,页面将返回
apple
结果和txtSearch的Text=“apple”,链接也是
search.aspx?q=apple

我试过了

  • 文本框的
    AutoPostBack=“True | False”

  • if(!IsPostBack)
    txtSearch.Text=Request.Params[“q”].ToString()

但我想我不能用它,因为我要发回同一个页面,不是吗

我也试过了

if (IsPostBack && txtSearch.Text != Request.Params["q"].ToString()) 
       txtSearch.Text = Request.Params["q"].ToString();

这对我来说似乎是一种奇怪的方式

我的首选是在事件处理程序本身中包含搜索逻辑,而不是实现这种奇怪的回发循环

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == false)
    {
        // If passed in on first entry to the page
        var searchQuery = Reqest.Params["q"];
        if (String.IsNullOrWhitespace(searchQuery) == false)
        {
            txtSearch.Text = searchQuery;
            Search_Click(null, null);
        }
    }

    txtSearch.Focus();

    this.Page.Form.DefaultButton = this.lnkSearch.UniqueID;
}

protected void Search_Click(object sender, EventArgs e)
{
    // Pass the value of the search to the repeater
    BindRepeater(txtSearch.Text);
}

请注意,我正在将搜索文本传递给
bindreater
,因此您必须更新它以使用参数值而不是查询字符串

不确定为什么要在
页面加载中执行此工作
。。。为什么不在
搜索中完成这项工作\u单击
并消除重新加载的复杂性?@freefiller很抱歉,我没有理解你的意思?你能举个简单的例子吗?非常感谢你的时间,我会测试它,然后再给你回复。我如何用新的搜索条件更新查询字符串?地址保持不变,先生,我有点“合并”了我的代码和你的代码,现在它工作得很好
protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack == false)
    {
        // If passed in on first entry to the page
        var searchQuery = Reqest.Params["q"];
        if (String.IsNullOrWhitespace(searchQuery) == false)
        {
            txtSearch.Text = searchQuery;
            Search_Click(null, null);
        }
    }

    txtSearch.Focus();

    this.Page.Form.DefaultButton = this.lnkSearch.UniqueID;
}

protected void Search_Click(object sender, EventArgs e)
{
    // Pass the value of the search to the repeater
    BindRepeater(txtSearch.Text);
}