网格视图上的分页/排序
本文关键字:排序 分页 视图 网格 | 更新日期: 2023-09-27 18:34:34
(这是另一个问题,因为我最初的帖子,我在一个帖子中问了太多问题(
让我先说明这一点。我对ASP编码几乎是全新的。我正在做一个小的副项目,它要求我使用ASP而不是我以前做的PHP。在过去的 2 天里,我一直在寻找解决方案,并尝试了很多东西,但似乎无法让它与我的代码一起使用。我加入这个网站已经有一段时间了,所以我知道它是如何工作的。如果我在一段时间内没有尝试自己做这件事,我就不会在这里问。我在这里学到了大量关于SQL的信息,所以我希望用ASP做同样的事情。
问题:
排序/分页
我知道我需要OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting"
和代码隐藏来使其适用于我的集,否则我会收到:"GridView 'GridView'触发事件排序未处理。只是我在那里找到的任何解决方案,我都无法让它在我的代码中工作。
网格视图1 :
<asp:TextBox ID="TextBox1" runat="server" Width="265px" Height="22px" CssClass="myBox"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search Fields" CssClass="myButton" />
<asp:GridView ID="GridView1" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" runat="server" AutoGenerateColumns="true" CellPadding="4" EnableModelValidation="True" EnableTheming="True" ForeColor="#333333" GridLines="None" Width="100%" style="margin-top: 0px; text-align: center;" AllowPaging="True" AllowSorting="True" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
代码隐藏(取出数据库名称和密码(:
SqlConnection vid = new SqlConnection("Data Source=ENF;Initial Catalog=***Database Name***;Persist Security Info=True;User ID=sa;Password=***Password***");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String str = "SELECT ab.NAME as [Customer] ,ISNULL(ab.TELEPHONE1,'') as [Phone #] ,ISNULL(pb.NAME,'') as [Product] ,ISNULL(aeb.NEW_PRODUCTVERSION,'') as [Version] ,CASE WHEN ab.STATUSCODE = 1 THEN 'Active' ELSE 'Inactive' END as [Status] ,ISNULL('Sal : ' + c.SALUTATION + ' / ','') + ISNULL('Title : ' + c.JOBTITLE + ' / ','') + ISNULL(a.PRIMARYCONTACTIDNAME,'') as [Primary Contact] ,ISNULL(c.TELEPHONE1,'') as [Contact Phone] FROM ACCOUNTBASE ab LEFT JOIN ACCOUNTEXTENSIONBASE aeb on ab.ACCOUNTID = aeb.ACCOUNTID LEFT JOIN PRODUCTBASE pb on aeb.NEW_PRIMARYPRODUCTID = pb.PRODUCTID LEFT JOIN ACCOUNT a on ab.ACCOUNTID = a.ACCOUNTID LEFT JOIN CONTACT c on a.PRIMARYCONTACTID = c.CONTACTID WHERE ((ab.NAME LIKE '%' + @search + '%') OR (aeb.NEW_PRODUCTVERSION LIKE '%' + @search + '%') OR (pb.NAME LIKE '%' + @search + '%') OR (a.PRIMARYCONTACTIDNAME LIKE '%' + @search + '%')) ORDER BY ab.NAME";
SqlCommand xp = new SqlCommand(str, vid);
xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
vid.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
GridView1.DataSource = ds;
GridView1.DataBind();
vid.Close();
}
其他获取数据((
private DataSet GetData()
{
String str = "SELECT ab.NAME as [Customer] ,ISNULL(ab.TELEPHONE1,'') as [Phone #] ,ISNULL(pb.NAME,'') as [Product] ,ISNULL(aeb.NEW_PRODUCTVERSION,'') as [Version] ,CASE WHEN ab.STATUSCODE = 1 THEN 'Active' ELSE 'Inactive' END as [Status] ,ISNULL('Sal : ' + c.SALUTATION + ' / ','') + ISNULL('Title : ' + c.JOBTITLE + ' / ','') + ISNULL(a.PRIMARYCONTACTIDNAME,'') as [Primary Contact] ,ISNULL(c.TELEPHONE1,'') as [Contact Phone] FROM ACCOUNTBASE ab LEFT JOIN ACCOUNTEXTENSIONBASE aeb on ab.ACCOUNTID = aeb.ACCOUNTID LEFT JOIN PRODUCTBASE pb on aeb.NEW_PRIMARYPRODUCTID = pb.PRODUCTID LEFT JOIN ACCOUNT a on ab.ACCOUNTID = a.ACCOUNTID LEFT JOIN CONTACT c on a.PRIMARYCONTACTID = c.CONTACTID WHERE ((ab.NAME LIKE '%' + @search + '%') OR (aeb.NEW_PRODUCTVERSION LIKE '%' + @search + '%') OR (pb.NAME LIKE '%' + @search + '%') OR (a.PRIMARYCONTACTIDNAME LIKE '%' + @search + '%')) ORDER BY ab.NAME";
SqlCommand xp = new SqlCommand(str, vid);
xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
vid.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds, "Name");
vid.Close();
return ds;
}
将此添加到代码隐藏:
protected void GridView1_PageIndexChanging(object sender, EventArgs e)
{ }
受保护的无效GridView1_Sorting(对象发送器,GridViewSortEventArgs e( { }
现在,当单击列名称或分页页面时,它只是闪烁页面,没有任何反应。
你必须实现排序事件
<asp:GridView OnSorting="GridView1_Sorting" />
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//sort the data
}
在我向页面 aspx.cs 文件添加以下行后,错误消失了:
protected void GridView1_PageIndexChanging(object sender, EventArgs e)
{
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
}