单击跨页PostBack事件

本文关键字:事件 PostBack 单击 | 更新日期: 2023-09-27 18:29:42

我有一个包含TextBox按钮Search.aspx页面。postback是指具有GridView的Results.aspx。Sql查询的工作原理与我使用QueryDesigner检查过的一样。

理论上,伪搜索应该通过在文本框中键入值并单击按钮来完成。当我输入一个值并单击时,Results.aspx页面将打开,但为空白。

Search.aspx中的点击事件无非是:

protected void Button2_Click(object sender, EventArgs e){} 

此设置在Web项目中有效,但在网站中无效。如果不将值提交到Results.aspx页面,则单击事件似乎不起作用。我一直没有找到一个点击事件,这将工作。如果有任何帮助,我将不胜感激。

编辑:sql查询很复杂,但正如我所说,当在查询设计器中输入值时,它可以正常工作。我确实在.cs中添加了一个"名称空间",因为网站通常没有名称空间,而不是Web项目。

Search.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Search.aspx.cs"  Inherits="LinqTest.Search" %>
 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
<title></title>
 </head>
 <body>
<form id="form1" runat="server">
<div>
    <asp:TextBox runat="server" ID="Name"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Search" PostBackUrl="~/Results.aspx"  />
</div>
</form>
 </body>
 </html>

搜索.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 namespace LinqTest
 {
 public partial class Search : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
}
}

ResultsSearch.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultsSearch.aspx.cs"   Inherits="RecipeFaire.ResultsSearch" %>
 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
 <%@ PreviousPageType VirtualPath="~/Search.aspx" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<!-- this is the results of the search done on the search.aspx -->
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="RecipeID" DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <tr>
                <td rowspan="2">
                    <asp:Image ID="Image1" runat="server" Width="100px" Height="80px"  ImageUrl='<%# "~/Handler.ashx?RecipeID=" + Eval("RecipeID")%>' />
                </td>
                <td rowspan="2" width="100px">
                    <asp:Rating ID="Rating1" runat="server" align="right" valign="top" CurrentRating='<%# Eval("RatingAVG")%>'
                        MaxRating="5" ReadOnly="true" StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar"
                        FilledStarCssClass="filledRatingStar" EmptyStarCssClass="emptyRatingStar">
                    </asp:Rating>
                    &nbsp&nbsp&nbsp<br></br>
                    <asp:Label ID="Label7" runat="server" align="right" Text='<%#  Eval("Count") %>' />&nbsp&nbsp
                    <asp:HyperLink ID="Home" Font-Size="Small" runat="server" NavigateUrl='<%# Bind("RecipeID", "../../Comments.aspx?RecipeId={0}") %>'
                        Text="Reviews" />
                </td>
                <td>
                    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Bind("RecipeID", "PageLinkDetails.aspx?RecipeId={0}") %>'
                        Text='<%# Eval("RecipeName") %>'></asp:HyperLink>
                </td>
            </tr>
            <tr style="width: 500px">
                <td>
                    <asp:Label ID="DescriptionLabel" valign="top" runat="server"  Text='<%# Eval("Description") %>' />
                </td>
            </tr>
            <tr>
                <td colspan="5" style="color: LightGrey">
                    <asp:Label ID="Label2" runat="server" Font-Size="1px" Height="1px"   Text='<%# Eval("RecipeID") %>'></asp:Label>
                    <hr style="border-style: dotted" />
                </td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<asp:LinkButton
                runat="server" ID="SortByName" CommandName="Sort" CommandArgument="RecipeName">Sort By Recipe Name</asp:LinkButton>
            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
            <asp:LinkButton runat="server" ID="SortByPrice" CommandName="Sort" CommandArgument="RatingAVG">Sort By Rating</asp:LinkButton>
            <table id="Table1" runat="server">
                <tr id="Tr1" runat="server">
                    <td id="Td1" runat="server">
                        <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr id="Tr2" runat="server" style="">
                                <th id="Th1" runat="server">
                                </th>
                            </tr>
                            <tr id="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr id="Tr3" runat="server">
                    <td id="Td2" runat="server" style="" align="center">
                        <asp:DataPager ID="DataPager2" PagedControlID="ListView1" PageSize="8" runat="server">
                            <Fields>
                                <asp:NumericPagerField ButtonCount="8" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
    </asp:ListView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RecipeUploadConnectionString %>"
        SelectCommand="SELECT pr.RecipeID, pr.CategoryName, pr.CategoryType, pr.RecipeName, pr.Description, COUNT(rr.RecipeID) AS Count, AVG(rr.Rating) AS RatingAVG 
        FROM PostedRecipes AS pr LEFT OUTER JOIN RecipeRatings AS rr ON pr.RecipeID = rr.RecipeID 
        WHERE pr.RecipeName LIKE '%' + @RecipeName + '%' OR pr.CategoryName LIKE '%' + @CategoryName + '%' 
        OR pr.CategoryType LIKE '%' + @CategoryType + '%' OR pr.CuisineOrigin LIKE '%' + @CuisineOrigin + '%' 
        OR pr.CuisineType LIKE '%' + @CuisineType + '%'  GROUP BY pr.RecipeID, pr.RecipeName, pr.CategoryName, pr.CategoryType, pr.CuisineOrigin, pr.CuisineType, pr.Description">
        <SelectParameters>
            <asp:FormParameter FormField="RecipeName" Name="RecipeName" Type="String"/>
             <asp:FormParameter FormField="CategoryName" Name="CategoryName" Type="String" />
            <asp:FormParameter FormField="CategoryType" Name="CategoryType" Type="String"/>
            <asp:FormParameter FormField="CuisineOrigin" Name="CuisineOrigin" Type="String" />
            <asp:FormParameter FormField="CuisineType" Name="CuisineType" Type="String"/>
        </SelectParameters>
    </asp:SqlDataSource>
</div>
</form>
</body>
 </html>

结果搜索.cs

 using System;
 using System.Data;
 using System.Data.SqlClient;
 using System.Configuration;
 using System.Collections;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
 using System.IO;
 using System.Text;
 using System.Web.SessionState;

 namespace RecipeFaire
 {
public partial class ResultsSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
}

单击跨页PostBack事件

我看到了一些奇怪的东西。。。

在你的search.aspx中…你有这个。

<asp:Button ID="Button1" runat="server" Text="Search" PostBackUrl="~/Results.aspx"  />

然而,在您的第二页列表中。。。你发布了这个。。。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultsSearch.aspx.cs"   Inherits="RecipeFaire.ResultsSearch" %>

PostbackUrl是Results.aspx,您在ResultsSearch.aspx中发布的其他页面。

你能检查一下这是否有问题吗?(我还不能发表评论。)

您肯定没有向我们展示您所有的代码,但我们会假设您在某个地方这样做:

ListView1.DataSource = SqlDataSource1;
ListView1.DataBind();

完成后,THEN您可以找到Label并设置其文本(在绑定之前不要在Page_Load中执行此操作)。

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
  if (e.Item.ItemType == ListViewItemType.DataItem)
  {
    Label recipeName = (Label)e.Item.FindControl("labRecipeName");
    recipeName.Text = Request.QueryString["RecipeName"].ToString()
  }
}

要为SqlDataSource使用查询字符串值,请使用QueryStringParameter:

<SelectParameters>
    <asp:QueryStringParameter Name="RecipeName" QueryStringField="RecipeName" Type="String" />
</SelectParameters>