使用ListView进行跨页回发

时间:2013-09-05 21:48:40

标签: asp.net

我有一个Source和Target页面。 Source.aspx使用按钮向Target.aspx提交搜索。浏览器地址栏显示通过显示ResultsTest.aspx发生的回发?RecipeName = First + Full + Test。如果使用Label(以下是目标.cs),实际搜索也将发布到Label中的目标页面

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

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

        }

    }

但是,为了发布发布,Label必须位于页面上的ListView之外。 ListView包含我想要检索数据的所有字段,并且Sql Select的工作方式与我在查询生成器/设计器中检查它一样。如果Page_Load更改为ListView内的Label,例如从Label 1到Label2,则会显示当前上下文中不存在Label的错误消息。

如何让ListView接受回发并检索数据并显示在目标页面上?

编辑:为了简洁起见,我从下面的ListView中删除了一些字段。但是,重点是,如果我更改Page_load以读取“labRecipeName”的标签ID,则会显示“not in context”错误。如果我在列表视图的“外部”放置相同的标签,则会发生回发并显示在目标页面上。我想要做的是在源页面上创建一个条目并将其回发到目标页面 - 这确实发生了。我无法让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中包含的ListView中执行该搜索。 ASPX。根据上面的描述,我已经验证了跨页回发确实发生了。我已经验证了Sql Select查询确实有效。所以,似乎我的问题不在回发中,也不在查询中。但是,使用上面给出的Target.aspx.cs(除了将Label1更改为labRecipeName以符合以下代码)当“labRecipeName”用于ListView的“内部”(作为项目)时,通过输入尝试回发“xxx”,出现错误消息“labRecipeName not not current context”。如果我将“labRecipeName”移到ListView之外,但仍然在Target.aspx上,则错误消息消失,“xxx”在Target页面上显示为文本。我的问题是:如何让“xxx”转到ListView中的项目,例如labRecipeName,以便启动查询并使查询结果出现在ListView中?可能我完全不正确的想法是如何实现最终结果。如果是这样,我会很感激我应该做的任何指导。

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

0 个答案:

没有答案