我有一个源和目标页面。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 包含我想要为其检索数据的所有字段,并且 Sql Select 的工作原理与我在 Query Builder/Designer 中检查过的一样。如果将Page_Load更改为ListView内部的Label,例如从Label 1变为Label2,则会显示Label在当前上下文中不存在的错误信息。
如何让 ListView 接受回发并检索数据并显示在目标页面上?
编辑:为简洁起见,我从下面的 ListView 中删除了一些字段。但是,关键是,如果我更改 Page_load 以读取“labRecipeName”的标签 ID,则会出现“不在上下文中”错误。如果我将相同的标签放在列表视图的“外部”,则回发会发生并显示在目标页面上。我想要做的是在源页面上创建一个条目并将其回发到目标页面 - 这确实发生了。我无法让 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>
                         <asp:LinkButton
runat="server" ID="SortByName" CommandName="Sort" CommandArgument="RecipeName">Sort By Recipe Name</asp:LinkButton>
              
<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 中/上的 ListView 中执行该搜索。根据上述描述,我已验证确实发生了跨页面回发。我已经验证了 Sql Select 查询确实有效。因此,看来我的问题既不在回发中,也不在查询中。但是,对于上面给出的 Target.aspx.cs(将 Label1 更改为 labRecipeName 以符合以下代码除外),当 ListView 的“内部”(作为项目)使用“labRecipeName”时,当通过输入尝试回发时“xxx”,出现错误信息“labRecipeName not in current context”。如果我将“labRecipeName”移到 ListView 之外,但仍在目标上。aspx,错误消息消失,“xxx”在目标页面上显示为文本。我的问题是:如何让“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>
                         <asp:LinkButton
runat="server" ID="SortByName" CommandName="Sort" CommandArgument="RecipeName">Sort By Recipe Name</asp:LinkButton>
              
<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>