单击“跨页面PostBack事件”

时间:2013-08-31 17:56:30

标签: c# asp.net visual-studio

我有一个Search.aspx页面,其中包含 TextBox 按钮postbackResults.aspx,它有一个GridView。 Sql查询工作,因为我已使用QueryDesigner检查它。

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

Search.aspx中的点击事件仅仅是:

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

此设置在Web项目中有效,但不会出现在Web站点中。通过不将值提交到Results.aspx页面,似乎点击事件无效。我没有找到可行的点击事件。我将不胜感激任何帮助。

编辑:sql查询很复杂,但正如我所说,在查询设计器中输入值时,它可以正常工作。我确实在.cs中添加了一个“命名空间”作为Web站点,而不是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>

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

ResultsSearch.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)
    {


    }
}
}

2 个答案:

答案 0 :(得分:0)

我看到一些奇怪的东西......

在你的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 中发布的另一页。

你能检查一下这是不是问题? (我无法发表评论。)

答案 1 :(得分:0)

你绝对没有向我们展示你的所有代码隐藏,但我们会假设你在某处做这件事:

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

完成后,然后,您可以找到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>