0

ソースとターゲットのページがあります。Source.aspx はボタンを使用して、検索を Target.aspx に送信します。ブラウザのアドレス バーには、ResultsTest.aspx?RecipeName=First+Full+Test が表示され、ポストバックが発生したことが示されます。ラベルが次のように使用されている場合、実際の検索もラベルのターゲット ページに投稿されます (以下はターゲット .cs)。

 protected void Page_Load(object sender, EventArgs e)
    {
        if ((Request.QueryString["RecipeName"] != null))
        {

            Label1.Text = Request.QueryString["RecipeName"].ToString();  

        }

    }

ただし、投稿を行うには、Label がページの ListView の外にある必要があります。ListView には、データを取得したいすべてのフィールドがあり、Query Builder/Designer でチェックしたとおりに Sql Select が機能します。Page_Load が ListView 内の Label に変更された場合 (たとえば、Label 1 から Label2 に変更された場合)、Label が現在のコンテキストに存在しないというエラー メッセージが表示されます。

ListView がポストバックを受け入れてデータを取得し、ターゲット ページに表示するにはどうすればよいですか?

編集: 簡潔にするために、以下の ListView からいくつかのフィールドを削除しました。ただし、重要なのは、「labRecipeName」のラベル ID を読み取るように Page_load を変更すると、「コンテキストにない」というエラーが表示されることです。リスト ビューの「外側」に同じラベルを付けると、ポストバックが発生し、ターゲット ページに表示されます。私がやりたいことは、ソース ページにエントリを作成し、それをターゲット ページにポストバックすることです。これは実際に行われます。ListView で実際にクエリ ワードを受け入れてデータを取得することはできません。

 <asp:ListView ID="ListView1" runat="server" DataKeyNames="RecipeID" DataSourceID="SqlDataSource1">

        <ItemTemplate>
            <tr>
                <td>
                    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%#  Bind("RecipeID", "ResultsTest.aspx?RecipeId={0}") %>'
                        Text='<%# Eval("RecipeName") %>'></asp:HyperLink>
                    <asp:Label ID="labRecipeName" runat="server" Text='<%# Eval("RecipeName") %>'></asp:Label>
                </td>
            </tr>
            <tr style="width: 500px">
                <td>

                </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>

編集:

ターゲット ページ .cs:

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

 namespace RecipeFaire
 {
 public partial class ResultsTest : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Request.QueryString["RecipeName"] != null))
        {

            labRecipeName.Text = Request.QueryString["RecipeName"].ToString();  

        }

    }
   }
 }

ソース ページ .cs:

 using System;
 using System.Configuration;
 using System.Data;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.HtmlControls;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;



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


    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string targetURL;

        targetURL = "ResultsTest.aspx?";

        targetURL += "RecipeName=" + Server.UrlEncode(txtRecipeName.Text.Trim());

        Response.Redirect(targetURL);
    }
 }
}

編集

私がする必要があるのは、Source.aspx のテキスト ボックスに「xxx」と入力し、「xxx」を検索として Target.aspx に投稿し、Target.aspx に含まれるリストビューでその検索を実行することです。上記の説明に従って、クロスページ ポストバックが発生することを確認しました。Sql Select クエリが機能することを確認しました。したがって、私の問題はポストバックにもクエリにもないようです。ただし、上記の Target.aspx.cs では (次のコードに準拠するように Label1 を labRecipeName に変更することを除いて)、"labRecipeName" が ListView の "内部" (アイテムとして) で使用されている場合、次のように入力してポストバックを試みます。 "xxx"、エラー メッセージ "labRecipeName not in current context" が表示されます。「labRecipeName」をListViewの外に移動しても、ターゲット上にある場合。aspx を使用すると、エラー メッセージが消え、「xxx」がターゲット ページにテキストとして表示されます。私の問題は次のとおりです: クエリを開始し、クエリの結果を ListView 内に表示するために、ListView 内のアイテム、たとえば labRecipeName に "xxx" を移動させるにはどうすればよいですか? この最終結果がどのように達成されるべきかについて、私は完全に間違った考えを持っているかもしれません。もしそうなら、私が何をすべきかについてのガイダンスをいただければ幸いです。

    <ItemTemplate>
        <tr>
            <td>
                <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%#  Bind("RecipeID", "ResultsTest.aspx?RecipeId={0}") %>'
                    Text='<%# Eval("RecipeName") %>'></asp:HyperLink>
                <asp:Label ID="labRecipeName" runat="server" Text='<%# Eval("RecipeName") %>'></asp:Label>
            </td>
        </tr>
        <tr style="width: 500px">
            <td>

            </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>
4

0 に答える 0