2013-08-31 116 views
0

我有一個Search.aspx頁面文本框按鈕postbackResults.aspx它有一個GridView。 Sql查詢的工作原理與QueryDesigner一樣。Click Event Event For Cross Page PostBack

理論上,僞搜索應該通過在文本框中輸入一個值並點擊按鈕來完成。當我輸入一個值並單擊時,Results.aspx頁面打開,但爲空。

Search.aspx click事件無非是:

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

此設置的確在Web項目的工作,但是,將無法在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) 
    { 


    } 
} 
} 
+0

可以顯示一些代碼?任何錯誤等? – christiandev

+0

感謝您的努力。代碼在上面。我沒有收到任何錯誤消息,只是目標頁面上的空白頁面。具有或不具有「命名空間」的結果相同。 – user1830924

+0

請顯示您的Search.aspx的完整標記。必須有一些更多的標記,否則它將不能像驗證一樣工作,或者你確定在Search.aspx中只使用了這麼多的標記? –

回答

0

我看到奇怪的東西...

在您的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其他頁面。

你能檢查這是否是問題嗎? (我還不能發表評論。)

+0

請參閱我上面對FlopScientist的評論。我的錯誤將該代碼發送給您。相信我,我知道你在指出什麼,我已經糾正了這一點,PostbackURL不是問題 - 我在這方面是正確的。 – user1830924

0

你絕對不會向我們展示所有的代碼隱藏,但是我們假設你正在做的這個地方:

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> 
+0

ethorn10,我很感激你的時間。我在編輯中給出了上述所有代碼。我在嘗試這樣做時顯然有些迷失,並且我可以看到上面的代碼綁定了我缺少的ListView。如果你可以根據我的代碼澄清,我會很感激。 – user1830924

+0

哦,那麼多。 – user1830924

+0

找出你的嘗試真的很難。你說,只要標籤沒有被埋在ListView中,你就可以工作......這是不是足夠了? – ethorn10