Click Event For Cross Page PostBack

2019-08-28 16:43发布

I have a Search.aspx page with TextBox and Button. The postback is to Results.aspx which has a GridView. The Sql query works as I have checked it with QueryDesigner.

In theory, a pseudo-search should be done by typing a value into the text box and clicking the button. When I enter a value and click, the Results.aspx page opens but is blank.

The click event in the Search.aspx is nothing more than:

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

This setup does work in a Web Project, however, will not in a Web Site, which this is. It would appear that the click event is not working by not submitting the value to the Results.aspx page. I have had no luck finding a click event which will work. I would appreciate any help.

EDIT: The sql query is complicated but as I said, it works fine when a value is entered in the Query Designer. I did add a "namespace" to the .cs as Web Sites, as opposed to Web Projects, normally do not have a namespace.

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条回答
We Are One
2楼-- · 2019-08-28 17:29

i am seeing something weird...

in your search.aspx... you have this.

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

However, in your 2nd page listing... you posted this...

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

the PostbackUrl is Results.aspx and the other page you posted in ResultsSearch.aspx.

Can you check if this is the problem? (i cant post comments yet.)

查看更多
Luminary・发光体
3楼-- · 2019-08-28 17:31

You definitely aren't showing us all of your code-behind, but we'll assume you are doing this somewhere:

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

After you've done that, THEN you can find the Label and set its text (do not do this in the Page_Load prior to binding).

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()
  }
}

To use a querystring value for your SqlDataSource, use a QueryStringParameter:

<SelectParameters>
    <asp:QueryStringParameter Name="RecipeName" QueryStringField="RecipeName" Type="String" />
</SelectParameters>
查看更多
登录 后发表回答