Click事件对于跨网页提交(Click Event For Cross Page PostBack

2019-10-30 03:05发布

我有一个Search.aspx文本框按钮的页面。 该postbackResults.aspx其中有一个GridView。 正如我与QueryDesigner检查它的SQL查询工作。

从理论上讲,一个伪搜索应该通过键入值的文本框,然后点击按钮来完成。 当我输入一个值,并且点击时, 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>

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


    }
}
}

Answer 1:

我看到一些奇怪...

在search.aspx ......你有这个。

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

然而,在第2页房源......你张贴了这个...

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

一项PostBackUrl是Results.aspx和你ResultsSearch.aspx张贴的其他页面。

您可以检查这是什么问题? (我还不能发表评论。)



Answer 2:

你绝对不向我们展示所有的代码隐藏,但是我们假设你正在做的这个地方:

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>


文章来源: Click Event For Cross Page PostBack