Click事件对于跨网页提交 [英] Click Event For Cross Page PostBack
问题描述
我有文本框和按钮 Search.aspx
页。在回发
是 Results.aspx
里面有一个GridView。正如我与QueryDesigner检查它的SQL查询工作。
在理论上,伪搜索应通过键入一个值到文本框,然后点击按钮完成。当我输入一个值,然后单击,在 Results.aspx
网页打开,但是空白。
在单击事件的 Search.aspx
无非是:
保护无效Button2_Click(对象发件人,EventArgs五){}
这个设置的确在Web项目中工作,但是,不会在网站上,它这是。这样看来,单击事件不被未提交值到 Results.aspx
页的工作。我有没有运气找到一个click事件,这将正常工作。我想AP preciate任何帮助。
编辑:SQL查询是复杂的,但正如我所说,当在查询设计器中输入一个值,它工作正常。我添加了空间来作为的.cs Web站点,而不是Web项目,通常没有命名空间。
Search.aspx
<%@页面语言=C#AutoEventWireup =真codeBehind =Search.aspx.cs继承=LinqTest.Search%GT; <%@注册大会=AjaxControlToolkit命名空间=AjaxControlToolkit标签preFIX =ASP%GT;
!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV>
< ASP:文本框=服务器ID =名称>< / ASP:文本框>
< ASP:按钮的ID =Button1的=服务器文本=搜索一项PostBackUrl =〜/ Results.aspx/>< / DIV>
< /表及GT;
< /身体GT;
< / HTML>
Search.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls; 命名空间LinqTest
{
公共部分类搜索:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ } 保护无效的button1_Click(对象发件人,EventArgs的发送)
{ }
}
}
ResultsSearch.aspx
<%@页面语言=C#AutoEventWireup =真codeFILE =ResultsSearch.aspx.cs继承=RecipeFaire.ResultsSearch%GT; <%@注册大会=AjaxControlToolkit命名空间=AjaxControlToolkit标签preFIX =ASP%GT;
<%@ previousPageType VirtualPath =〜/ Search.aspx%GT;
!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头ID =头像1=服务器>
<标题>< /标题>
< /头>
<身体GT;
<表ID =form1的=服务器>
<! - 这是对search.aspx完成的搜索结果 - >
< DIV>
< ASP:的ScriptManager ID =ScriptManager1=服务器>
< / ASP:ScriptManager的>
< ASP:ListView控件ID =ListView1的=服务器的DataKeyNames =RecipeID的DataSourceID =SqlDataSource1>
<&ItemTemplate中GT;
&所述; TR>
&所述; TD行跨度=2>
< ASP:图片ID =Image1的=服务器WIDTH =100px的HEIGHT =80px的ImageUrl ='<%#〜/ Handler.ashx RecipeID =?+的eval(RecipeID)% >' />
< / TD>
< TD ROWSPAN =2WIDTH =100px的>
< ASP:等级ID =额定值1=服务器ALIGN =权利VALIGN =顶CurrentRating ='<%#的eval(RatingAVG)%>'
MaxRating =5只读=真StarCssClass =ratingStarWaitingStarCssClass =savedRatingStar
FilledStarCssClass =filledRatingStarEmptyStarCssClass =emptyRatingStar>
< / ASP:评级>
&安培;&NBSP放大器;&NBSP放大器;&NBSP LT; BR>< / BR>
< ASP:标签ID =Label7=服务器ALIGN =右文本='<%#的eval(计数)%GT;' />&安培;&NBSP放大器; NBSP
< ASP:超链接ID =家FONT-SIZE =小=服务器NavigateUrl ='<%#绑定(RecipeID,../../Comments.aspx?RecipeId={0 })%GT;'
文本=评论/>
< / TD>
&所述; TD>
< ASP:超链接ID =HyperLink2=服务器NavigateUrl ='<%#绑定(RecipeID,PageLinkDetails.aspx RecipeId = {0}?)%GT;'
文字='<%#的eval(RecipeName中)%>'>< / ASP:超链接>
< / TD>
< / TR>
< TR风格=WIDTH:500px的>
&所述; TD>
< ASP:标签ID =DescriptionLabelVALIGN =顶=服务器文本='<%#的eval(说明)%GT;' />
< / TD>
< / TR>
&所述; TR>
< TD合并单元格=10的风格=颜色:浅灰>
< ASP:标签ID =Label2的=服务器FONT-SIZE =1px的HEIGHT =1px的文本='<%#的eval(RecipeID)%>'>< / ASP :标签>
<小时风格=BORDER-风格:点/>
< / TD>
< / TR>
< / ItemTemplate中>
<&LayoutTemplate模板GT;
&安培;&NBSP放大器;&NBSP放大器;&NBSP放大器;&NBSP放大器;&NBSP放大器;&NBSP放大器;&NBSP放大器;&NBSP放大器;&NBSP放大器; NBSP               <asp:LinkButton
=服务器ID =SortByName的CommandName =排序CommandArgument =RecipeName中&GT;排序配方名称和LT; / ASP:LinkButton的&GT;
              
&LT; ASP:LinkButton的=服务器ID =SortByPrice的CommandName =排序CommandArgument =RatingAVG&GT;按评分排序&LT; / ASP:LinkButton的&GT;
&LT;表ID =表1=服务器&GT;
&LT; TR ID =TR1=服务器&GT;
&LT; TD ID =TD1=服务器&GT;
&LT;表ID =itemPlaceholderContainer=服务器BORDER =0的风格=&GT;
&LT; TR ID =TR2=服务器的风格=&GT;
百分位ID =Th1细胞=服务器&GT;
&LT; /第i
&LT; / TR&GT;
&LT; TR ID =itemPlaceholder=服务器&GT;
&LT; / TR&GT;
&LT; /表&gt;
&LT; / TD&GT;
&LT; / TR&GT;
&LT; TR ID =Tr3的=服务器&GT;
&LT; TD ID =TD2=服务器的风格=ALIGN =中心&GT;
&LT; ASP:DataPager的ID =DataPager2PagedControlID =ListView1的每页=8=服务器&GT;
&LT;&领域GT;
&LT; ASP:NumericPagerField ButtonCount =8/&GT;
&LT; /场&GT;
&LT; / ASP:DataPager的&GT;
&LT; / TD&GT;
&LT; / TR&GT;
&LT; /表&gt;
&LT; / LayoutTemplate模板&GT;
&LT; / ASP:的ListView&GT;
&LT; ASP:SqlDataSource的ID =SqlDataSource1=服务器的ConnectionString =&LT;%$的ConnectionStrings:RecipeUploadConnectionString%&gt;中
的SelectCommand =SELECT pr.RecipeID,pr.CategoryName,pr.CategoryType,pr.RecipeName,pr.Description,COUNT(rr.RecipeID)AS计数,AVG(rr.Rating)AS RatingAVG
从PostedRecipes公关LEFT OUTER JOIN RecipeRatings例如RR ON pr.RecipeID = rr.RecipeID
WHERE pr.RecipeName LIKE'%'+ @RecipeName +'%'或pr.CategoryName LIKE'%'+ @CategoryName +'%'
OR pr.CategoryType LIKE'%'+ @CategoryType +'%'或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&GT;
&LT; SelectParameters&GT;
&LT; ASP:FormParameter FormField =RecipeName中NAME =RecipeName中类型=字符串/&GT;
&LT; ASP:FormParameter FormField =类别名称NAME =类别名称类型=字符串/&GT;
&LT; ASP:FormParameter FormField =CategoryTypeNAME =CategoryType类型=字符串/&GT;
&LT; ASP:FormParameter FormField =CuisineOriginNAME =CuisineOrigin类型=字符串/&GT;
&LT; ASP:FormParameter FormField =CuisineTypeNAME =CuisineType类型=字符串/&GT;
&LT; / SelectParameters&GT;
&LT; / ASP:SqlDataSource的&GT;
&LT; / DIV&GT;
&LT; /表及GT;
&LT; /身体GT;
&LT; / HTML&GT;
ResultsSearch.cs
使用系统;
使用System.Data这;
使用System.Data.SqlClient的;
使用System.Configuration;
System.Collections中使用;
使用的System.Web;
使用System.Web.Security;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
使用System.IO;
使用System.Text;
使用System.Web.SessionState;
命名空间RecipeFaire
{公共部分类ResultsSearch:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
}
}
}
您绝对没有我们展示所有$ C $的C-落后,但我们假设你正在做的这个地方:
ListView1.DataSource = SqlDataSource1;
ListView1.DataBind();
你这样做之后,,然后您可以找到标签
并设置其文本(不要在<$ C做到这一点$ C>的Page_Load 之前绑定)。
保护无效ListView1_ItemDataBound(对象发件人,ListViewItemEventArgs E)
{
如果(e.Item.ItemType == ListViewItemType.DataItem)
{
标签为recipename =(标签)e.Item.FindControl(labRecipeName);
recipeName.Text =的Request.QueryString [RecipeName中]。的ToString()
}
}
要使用的查询字符串值,你的的SqlDataSource
,使用 QueryStringParameter
:
&LT; SelectParameters&GT;
&LT; ASP:QueryStringParameter NAME =RecipeName中QueryStringField =RecipeName中类型=字符串/&GT;
&LT; / SelectParameters&GT;
I have a Search.aspx
page with TextBox and Button. The postback
is to Results.aspx
which has a GridView. The Sql query works as I have checked it with QueryDesigner.
In theory, a pseudo-search should be done by typing a value into the text box and clicking the button. When I enter a value and click, the Results.aspx
page opens but is blank.
The click event in the Search.aspx
is nothing more than:
protected void Button2_Click(object sender, EventArgs e){}
This setup does work in a Web Project, however, will not in a Web Site, which this is. It would appear that the click event is not working by not submitting the value to the Results.aspx
page. I have had no luck finding a click event which will work. I would appreciate any help.
EDIT: The sql query is complicated but as I said, it works fine when a value is entered in the Query Designer. I did add a "namespace" to the .cs as Web Sites, as opposed to Web Projects, normally do not have a namespace.
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>
   <br></br>
<asp:Label ID="Label7" runat="server" align="right" Text='<%# Eval("Count") %>' />  
<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>
                         <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>
</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)
{
}
}
}
You definitely aren't showing us all of your code-behind, but we'll assume you are doing this somewhere:
ListView1.DataSource = SqlDataSource1;
ListView1.DataBind();
After you've done that, THEN you can find the Label
and set its text (do not do this in the Page_Load
prior to binding).
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()
}
}
To use a querystring value for your SqlDataSource
, use a QueryStringParameter
:
<SelectParameters>
<asp:QueryStringParameter Name="RecipeName" QueryStringField="RecipeName" Type="String" />
</SelectParameters>
这篇关于Click事件对于跨网页提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!