2010-04-07 94 views
1

有人可以將我重定向到您自己的博客鏈接或任何其他鏈接,說明如何使用存儲過程來實現gridview自定義分頁的完整教程嗎?如何使用存儲過程實現Gridview自定義分頁?

我在谷歌的搜索給我從2006年的舊文章。現在我使用的是asp.net 3.5和c#。

+0

是否有必要使用存儲過程? – vittore 2010-04-07 20:26:29

+0

是的,先生,現在至少不能刪除現有的功能(SP)。現在數據足夠大,並且gridview中的默認分頁功能,你知道,不夠聰明... – Jango 2010-04-07 20:32:42

+0

您從未評論過我的解決方案 – 2010-04-13 20:14:05

回答

2

.aspx頁面中

<asp:DropDownList ID="ddlSelector" AutoPostBack="true" runat="server" 
     onselectedindexchanged="ddlSelector_SelectedIndexChanged" > 
     <asp:ListItem>Employee</asp:ListItem> 
     <asp:ListItem>Customer</asp:ListItem> 
    </asp:DropDownList> 

    <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click" Text="Search" /> 

    <br /> 
    <table> 
     <tr> 
      <td class="txtclmn"> 
       First Name:</td> 
      <td> 
       <asp:TextBox ID="txtFname" runat="server"></asp:TextBox> 
      </td> 
      <td class="txtclmn"> 
       Last Name:</td> 
      <td> 
       <asp:TextBox ID="txtLname" runat="server"></asp:TextBox> 
      </td> 
     </tr> 
     <tr> 
      <td class="txtclmn"> 
       Middle Name:</td> 
      <td> 
       <asp:TextBox ID="txtMname" runat="server"></asp:TextBox> 
      </td> 
      <td class="txtclmn"> 
       Phone Number:</td> 
      <td> 
       <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> 
      </td> 
     </tr> 
     <tr> 
      <td class="txtclmn"> 
       Email Address:</td> 
      <td> 
       <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox> 
      </td> 
      <td class="txtclmn"> 
      </td> 
      <td> 
      </td> 

     </tr> 

    </table> 
    <div runat="server" id="empCriteria"> 
    <table > 
     <tr> 
      <td class="txtclmn"> 
       <asp:Label ID="lblHdate" runat="server" Text="Hire Date:"></asp:Label> 
      </td> 
      <td> 
       <asp:TextBox ID="txtHMonth" runat="server" MaxLength="2" CssClass="input2char"></asp:TextBox> 
       <asp:Label ID="lblHdash1" runat="server" Text="-"></asp:Label> 
       <asp:TextBox ID="txtHDay" runat="server" MaxLength="2" CssClass="input2char"></asp:TextBox> 
       <asp:Label ID="lblHdash2" runat="server" Text="-"></asp:Label> 
       <asp:TextBox ID="txtHYear" runat="server" MaxLength="4" CssClass="input4char"></asp:TextBox> 
      </td> 
      <td class="txtclmn"> 
       <asp:Label ID="lblBdate" runat="server" Text="Birth Date:"></asp:Label> 
       </td> 
      <td> 
       <asp:TextBox ID="txtBMonth" runat="server" MaxLength="2" CssClass="input2char"></asp:TextBox> 
       <asp:Label ID="lblBdash1" runat="server" Text="-"></asp:Label> 
       <asp:TextBox ID="txtBDay" runat="server" MaxLength="2" CssClass="input2char"></asp:TextBox> 
       <asp:Label ID="lblBdash2" runat="server" Text="-"></asp:Label> 
       <asp:TextBox ID="txtBYear" runat="server" MaxLength="4" CssClass="input4char"></asp:TextBox> 
      </td> 
     </tr> 
     <tr> 
      <td class="txtclmn"> 
       <asp:Label ID="lblLoginId" runat="server" Text="Login ID:"></asp:Label> 
      </td> 
      <td> 
       <asp:TextBox ID="txtLogin" runat="server"></asp:TextBox> 
      </td> 
      <td class="txtclmn"> 
       <asp:Label ID="lblJobTitle" runat="server" Text="Job Title:"></asp:Label></td> 
      <td> 
       <asp:TextBox ID="txtJobTitle" runat="server"></asp:TextBox> 
      </td> 
     </tr> 
     <tr> 
      <td class="txtclmn"> 
       <asp:Label ID="lblGender" runat="server" Text="Gender:"></asp:Label> 
      </td> 
      <td> 
       <asp:DropDownList ID="ddlGender" runat="server" style="width: 73px"> 
        <asp:ListItem Value="-1">-----------</asp:ListItem> 
        <asp:ListItem Value="M">Male</asp:ListItem> 
        <asp:ListItem Value="F">Female</asp:ListItem> 
       </asp:DropDownList> 
      </td> 
      <td class="txtclmn"> 
       <asp:Label ID="lblMarStat" runat="server" Text="Marital Status:"></asp:Label> 
      </td> 
      <td> 
       <asp:DropDownList ID="ddlMarStat" runat="server"> 
        <asp:ListItem Value="-1">-----------</asp:ListItem> 
        <asp:ListItem Value="S">Single</asp:ListItem> 
        <asp:ListItem Value="M">Married</asp:ListItem> 
       </asp:DropDownList> 
      </td> 
     </tr> 
     <tr> 
      <td class="txtclmn"> 
       <asp:Label ID="lblActive" runat="server" Text="Active:"></asp:Label> 
      </td> 
      <td> 
       <asp:DropDownList ID="ddlActive" runat="server"> 
        <asp:ListItem Value="-1">-----</asp:ListItem> 
        <asp:ListItem Value="1">Yes</asp:ListItem> 
        <asp:ListItem Value="0">No</asp:ListItem> 
       </asp:DropDownList> 
      </td> 
      <td class="txtclmn"> 
       <asp:Label ID="lblSalary" runat="server" Text="Salaried:"></asp:Label></td> 
      <td> 
       <asp:DropDownList ID="ddlSalary" runat="server"> 
        <asp:ListItem Value="-1">-----</asp:ListItem> 
        <asp:ListItem Value="1">Yes</asp:ListItem> 
        <asp:ListItem Value="0">No</asp:ListItem> 
       </asp:DropDownList> 
      </td> 
     </tr> 
    </table> 
    </div> 
    <br /> 
    <br /> 
    <asp:GridView ID="gdvCust" runat="server" AllowPaging="True" 
     AutoGenerateColumns="False" 
     onpageindexchanging="gdvCust_PageIndexChanging"> 
     <Columns> 
      <asp:BoundField DataField="ContactID" HeaderText="ContactID" ReadOnly="True" 
       SortExpression="ContactID" /> 
      <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" 
       SortExpression="Title" /> 
      <asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True" 
       SortExpression="FirstName" /> 
      <asp:BoundField DataField="MiddleName" HeaderText="MiddleName" ReadOnly="True" 
       SortExpression="MiddleName" /> 
      <asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True" 
       SortExpression="LastName" /> 
      <asp:BoundField DataField="Suffix" HeaderText="Suffix" ReadOnly="True" 
       SortExpression="Suffix" /> 
      <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" 
       ReadOnly="True" SortExpression="EmailAddress" /> 
      <asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" 
       SortExpression="Phone" /> 
      <asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" /> 
     </Columns> 
    </asp:GridView> 


    <br /> 
    <asp:GridView ID="gdvEmp" runat="server" AllowPaging="True" 
     AutoGenerateColumns="False" 
     onpageindexchanging="gdvEmp_PageIndexChanging"> 
     <Columns> 
      <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True" 
       SortExpression="EmployeeID" /> 
      <asp:BoundField DataField="JobTitle" HeaderText="Job Title" ReadOnly="True" 
       SortExpression="Title" /> 
      <asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True" 
       SortExpression="FirstName" /> 
      <asp:BoundField DataField="MiddleName" HeaderText="MiddleName" ReadOnly="True" 
       SortExpression="MiddleName" /> 
      <asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True" 
       SortExpression="LastName" /> 
      <asp:BoundField DataField="Suffix" HeaderText="Suffix" ReadOnly="True" 
       SortExpression="Suffix" /> 
      <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" 
       ReadOnly="True" SortExpression="EmailAddress" /> 
      <asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" 
       SortExpression="Phone" /> 
      <asp:BoundField DataField="LoginID" HeaderText="LoginID" ReadOnly="True" 
       SortExpression="LoginID" /> 
      <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" 
       SortExpression="Title" /> 
      <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" ReadOnly="True" 
       SortExpression="BirthDate" /> 
      <asp:BoundField DataField="MaritalStatus" HeaderText="MaritalStatus" ReadOnly="True" 
       SortExpression="MaritalStatus" /> 
      <asp:BoundField DataField="Gender" HeaderText="Gender" ReadOnly="True" 
       SortExpression="Gender" /> 
      <asp:BoundField DataField="HireDate" HeaderText="HireDate" 
       ReadOnly="True" SortExpression="HireDate" /> 
      <asp:CheckBoxField DataField="SalariedFlag" HeaderText="SalariedFlag" 
       ReadOnly="True" SortExpression="SalariedFlag" /> 
      <asp:CheckBoxField DataField="CurrentFlag" HeaderText="CurrentFlag" 
       ReadOnly="True" SortExpression="CurrentFlag" /> 
     </Columns> 
    </asp:GridView> 


</form> 

代碼隱藏

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 

namespace TestProj 
{ 
public partial class Search : System.Web.UI.Page 
{ 

    static IQueryable<Contact> _conResults; 
    //static IQueryable<EmpContact> _empResults; 
    protected void Page_Load(object sender, EventArgs e) 
    { 


    } 


    protected void ddlSelector_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     gdvCust.DataSource = null; 
     gdvCust.DataBind(); 
     gdvEmp.DataSource = null; 
     gdvEmp.DataBind(); 
     bool flag; 
     if (ddlSelector.SelectedValue == "Employee") 
      flag = true; 
     else 
     {    
      flag = false; 
     } 
     foreach(Control c in empCriteria.Controls) 
     { 
      c.Visible = flag; 
     } 
    } 
    private IQueryable<Contact> CreateCustQuery() 
    { 
     TestDataClassDataContext dc = new TestDataClassDataContext(); 
     var predicate = PredicateBuilder.True<Contact>(); 
     var cust = from individual in dc.Individuals 
        join contact in dc.Contacts on individual.ContactID equals contact.ContactID 
        select contact; 
     if (!string.IsNullOrEmpty(txtLname.Text)) 
      predicate = predicate.And(e => e.LastName.Contains(txtLname.Text)); 
     if (!string.IsNullOrEmpty(txtFname.Text)) 
      predicate = predicate.And(e => e.FirstName.Contains(txtFname.Text)); 
     if (!string.IsNullOrEmpty(txtMname.Text)) 
      predicate = predicate.And(e => e.MiddleName.Contains(txtMname.Text)); 
     if (Utility.IsValidPhone(txtPhone.Text)) 
      predicate = predicate.And(e => e.Phone.Contains(txtPhone.Text)); 
     if (Utility.IsValidEmailAddress(txtEmail.Text)) 
      predicate = predicate.And(e => e.EmailAddress.Contains(txtEmail.Text)); 

     var results = cust.Where(predicate); 

     return results; 
    } 

    //private void CreateEmpQuery() 
    //{ 
    // TestDataClassDataContext dc = new TestDataClassDataContext(); 

    // var emp = from c in dc.Contacts 
    //    from e in dc.Employees 
    //    where c.ContactID == e.ContactID 
    //    select new 
    //    { 
    //     FirstName = c.FirstName,LastName = c.LastName, MiddleName =c.MiddleName, Phone = c.Phone, 
    //     EmailAddress = c.EmailAddress 
    //    }; 

    // var predicate = PredicateBuilder.True<>(); 
    // if (!string.IsNullOrEmpty(txtLname.Text)) 
    //  predicate = predicate.And(e => e.LastName.Contains(txtLname.Text)); 
    // if (!string.IsNullOrEmpty(txtFname.Text)) 
    //  predicate = predicate.And(e => e.FirstName.Contains(txtFname.Text)); 
    // if (!string.IsNullOrEmpty(txtMname.Text)) 
    //  predicate = predicate.And(e => e.MiddleName.Contains(txtMname.Text)); 
    // if (Utility.IsValidPhone(txtPhone.Text)) 
    //  predicate = predicate.And(e => e.Phone.Contains(txtPhone.Text)); 
    // if (Utility.IsValidEmailAddress(txtEmail.Text)) 
    //  predicate = predicate.And(e => e.EmailAddress.Contains(txtEmail.Text)); 

    // var results = emp.Where(predicate); 


    //} 

    private void GetCustResults() 
    { 
     _conResults = CreateCustQuery(); 
     gdvCust.DataSource = _conResults; 
     gdvCust.DataBind(); 
    } 

    //private void GetEmpResults() 
    //{ 
    // _empResults = CreateEmpQuery(); 
    // gdvEmp.DataSource = _empResults; 
    // gdvEmp.DataBind(); 
    //} 

    protected void gdvCust_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     gdvCust.PageIndex = e.NewPageIndex; 
     gdvCust.DataSource = _conResults; 
     gdvCust.DataBind(); 
    } 
    //protected void gdvEmp_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    //{ 
    // gdvCust.PageIndex = e.NewPageIndex; 
    // gdvEmp.DataSource = _empResults; 
    // gdvEmp.DataBind(); 
    //} 

    protected void btnSearch_Click(object sender, EventArgs e) 
    { 
     if (ddlSelector.SelectedValue == "Customer") 
      GetCustResults(); 
     //else 
      //GetEmpResults(); 
    } 



} 
} 

只有之前,我轉移到我使用LINQ當時另一個項目,但是這可能會幫助你完成卡斯特結果瞭解獲取GridView需要做什麼。這是使用LINQ用的AdventureWorks作爲示例數據庫現在你需要注意的是

<asp:GridView ID="gdvCust" runat="server" AllowPaging="True" 
     AutoGenerateColumns="False" 
     onpageindexchanging="gdvCust_PageIndexChanging"> 

和哪些代碼

解釋aspx頁面上的列和綁定字段,其中的DataField =列的名稱在數據庫中,並且Header字段是在gridview的列頂部標記的內容。

現在後面的代碼看

private void GetCustResults() 
{ 
    _conResults = CreateCustQuery(); 
    gdvCust.DataSource = _conResults; 
    gdvCust.DataBind(); 
} 
//and  
protected void gdvCust_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    gdvCust.PageIndex = e.NewPageIndex; 
    gdvCust.DataSource = _conResults; 
    gdvCust.DataBind(); 
} 

他們做的從數據源和尋呼控制結合。如果您需要更多解釋,請發表評論

+0

它是自定義分頁嗎? – Jango 2010-04-08 13:51:29

+0

是的,這是一個自定義分頁與這些功能。 GridViewPageEventArgs e是處理頁面更改所需的全部內容,否則是綁定到gridview控件的可綁定數據對象。我想你知道如何用你當前的存儲過程來做到這一點 – 2010-04-08 13:52:38

+0

謝謝我會嘗試並給你反饋 – Jango 2010-04-08 14:13:08

0

由於自2006年以來SQL的語法沒有太大改變,所以沒有太多更新。在存儲過程方面,您仍然會執行ROW_Number()調用。

With table AS 
(SELECT ID, NAME, 
    ROW_NUMBER() OVER (order by Name) as RowNumber 
    FROM Customers) 

select * 
from table 
Where RowNumber >= @RowStart and RowNumber <= @RowEnd end 
+0

我相信,我可以非常輕鬆地處理存儲過程級別的功能。但問題是使用sqldatasource在gridview中實現自定義分頁。有任何線索嗎? – Jango 2010-04-07 20:29:30

1

查看下面的代碼。

CREATE PROCEDURE getDeals 
    @StartIndex int, 
    @PageSize int, 
    @TotalCount int OutPut 
as 

select @TotalCount=count(1) from mstrDeals; 
WITH CTE AS 
(
    select top(@[email protected]) ROW_NUMBER() OVER(ORDER BY creationdate) RowNumber,dealid,dealTitle from mstrDeals 
) 
select * from CTE where RowNumber between @startIndex and (@[email protected]) 

然後,我們需要生成與總行沒有,頁面大小和當前頁面尋呼機,下面的是,這裏填充呼叫器鏈接總數的代碼示例5是可以通過重置局部變量進一步定製名爲totalLinkInPage,並且具有第一個和最後一個按鈕,當前頁面首先或最後相應地自動啓用或禁用。

退房生成分頁代碼和綁定gridview的代碼(參考): -

Custom paging in asp.net using stored procedure in gridview with nice pagination

0

首先實現你Custom Paging in SQL Server Stored Procedure然後用你的C#代碼中調用它,並填補了數據表。最後用這個數據表設置gridview的DataSource屬性。

請參閱ASP.NET GridView Custom Paging以使用自定義分頁與gridview。技巧是gridview屬性'VirtualItemCount',它由數據源的記錄總數設置。

+0

.Net 3.5(ASP 2.0)不支持VirtualItemCount,因此對於那些希望這樣做的人來說,他們將不得不推出他們的自己的分頁控制或使用現有的商業或開源的gridview控件。 – 2016-03-29 15:20:52

相關問題