在您的站点内搜索并使用datapager ASP.NET

时间:2015-03-06 10:37:56

标签: c# asp.net .net

我正在尝试“在您的数据库中搜索”。第一种形式是WebForm1.aspx工作正常,当我在第二个表单(entry.aspx)上搜索时仍然可以正常工作,但主要问题是每当我点击下一个或上一个按钮时,它就无法正常工作(DataPager) )。

它要么显示任何内容,要么显示出错误。

<form id="form1" runat="server">
<div>
  <h1>search for something, man!</h1>
    <p><asp:TextBox runat="server" ID="txtSearch" Text="search for something"></asp:TextBox></p>
    <p><asp:Button runat="server" ID="btnSubmit" Text="Search" OnClick="btnSubmit_Click" /></p>
<asp:ListView runat="server" ID="lv">


   <LayoutTemplate>
       <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
   </LayoutTemplate>



    <ItemTemplate>
          <a href="<%# ResolveClientUrl("~/entry.aspx?id=" + Eval("BlogTitle").ToString()) %>"><%# Server.HtmlEncode(Eval("BlogTitle").ToString())%></a><%#Eval("BlogContents")%></ItemTemplate>


</asp:ListView>


</div>
</form>

Webform.aspx.cs

protected void btnSubmit_Click(object sender, EventArgs e){                                                                           
       BlogDBDataContext db = new BlogDBDataContext();
       dynamic q = from b in db.Blogs
                   where b.BlogContents.Contains(txtSearch.Text.Trim()) | b.BlogTitle.Contains(txtSearch.Text.Trim())
                   select b;

       lv.DataSource = q;
       lv.DataBind();

       Session["Data"] = txtSearch.Text;
       Response.Redirect("~/entry.aspx");

   }

entry.aspx

<form id="form1" runat="server">
<div>
  <h1>search for something, man!</h1>
    <p><asp:TextBox runat="server" ID="txtSearch" Text="search for something"></asp:TextBox></p>
    <p><asp:Button runat="server" ID="sub" Text="Search" OnClick="sub_Click" /></p>

    <br />
    <br />

<asp:ListView runat="server" ID="lv">
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
    </LayoutTemplate>
    <ItemTemplate>
         <h1><%#Eval ("BlogTitle") %></h1>
        <div>
            <%#Eval("BlogContents")%>


        </div>
    </ItemTemplate>
</asp:ListView>
</div>
    <asp:DataPager ID="lvDataPager1" runat="server" PagedControlID="lv" PageSize="1" OnPreRender="lvDataPager1_PreRender1">
    <Fields>
         <asp:NextPreviousPagerField ShowLastPageButton="false" ButtonCssClass="previousNextLink" />
        <asp:NumericPagerField ButtonType="Link" />
        <asp:NextPreviousPagerField ShowPreviousPageButton="false" ButtonCssClass="previousNextLink" />
    </Fields>
</asp:DataPager>

</form>

entry.aspx.cs

   protected void Page_Load(object sender, EventArgs e)
    {
        string strTitle = "";

        if (Session["Data"] != null)
        {
            strTitle = Session["Data"].ToString();
            BlogDBDataContext db = new BlogDBDataContext();

            dynamic q = from b in db.Blogs
                        where b.BlogContents.Contains(strTitle) | b.BlogTitle.Contains(strTitle)
                        select b;


            lv.DataSource = q;
            lv.DataBind();
        }
    }protected void lvDataPager1_PreRender1(object sender, EventArgs e)
    {
        this.lv.DataBind();
    }protected void sub_Click(object sender, EventArgs e)
    {
        BlogDBDataContext db = new BlogDBDataContext();
        dynamic q = from b in db.Blogs
                    where b.BlogContents.Contains(txtSearch.Text.Trim()) |b.BlogTitle.Contains(txtSearch.Text.Trim())
                    select b;

        lv.DataSource = q;
        lv.DataBind();
    }

1 个答案:

答案 0 :(得分:0)

看起来您遇到了页面生命周期问题。

您的列表视图在多个位置绑定并反弹,因此当您更改内容时,您正在查看的页面不再存在。

移动绑定逻辑,使其只在一个地方

我建议只在页面加载中使用它,丢失提交按钮单击方法,然后从查询字符串中读取搜索数据。