2012-02-28 74 views
1

我是新來的asp.net,最近一直在從代碼隱藏創建一個GridView,使它更加靈活,以便我最終可以根據用戶規範創建它。如何從類創建一個GridView

現在我正在探索類,我認爲創建一個GridView類是很酷的,所以無論何時我需要創建一個GridView,我都可以通過類的規範而不是重寫相同的代碼每個頁面的代碼隱藏。

我並沒有看到太多的例子來說明如何完成這個。你有沒有做過這個?這對我來說甚至是合理的嗎?

這是我目前如何使我的GridView與代碼隱藏。任何想法如何改變這個創建一個類的GridView?

.aspx頁面中:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
EmptyDataText="There are no data records to display." AllowPaging="True" 
CssClass="GridViewStyle" GridLines="None" Width="100%"> 
    <Columns>      
     <asp:HyperLinkField DataNavigateUrlFields="EmployeeID" 
      DataNavigateUrlFormatString="EmployeeProfile.aspx?EmployeeID={0}" 
      DataTextField="EmployeeID" 
      DataTextFormatString= "<img src='Images/icons/document-search-result.png' alt='View'/> <u>View</u>" > 
      <ControlStyle CssClass="titleLinksB" /> 
      <ItemStyle Wrap="False" /> 
     </asp:HyperLinkField>       
    </Columns> 
    <RowStyle CssClass="RowStyle" /> 
    <EmptyDataRowStyle CssClass="EmptyRowStyle" /> 
    <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" /> 
    <PagerStyle CssClass="PagerStyle" /> 
    <SelectedRowStyle CssClass="SelectedRowStyle" /> 
    <HeaderStyle CssClass="HeaderStyle" /> 
    <EditRowStyle CssClass="EditRowStyle" /> 
    <AlternatingRowStyle CssClass="AltRowStyle" /> 
    <SortedAscendingHeaderStyle CssClass="sortasc"></SortedAscendingHeaderStyle> 
    <SortedDescendingHeaderStyle CssClass="sortdesc"></SortedDescendingHeaderStyle>   
</asp:GridView> 

.aspx.vb代碼隱藏頁:

Partial Class GridTest2 
Inherits System.Web.UI.Page 

Sub Page_load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 
    If Not Page.IsPostBack Then 
     CreateGridColumns() 
     BindGrid() 
    End If 
End Sub 

Public Property SortExpression As String 
    Get 
     If ViewState("SortExpression") Is Nothing Then 
      ViewState("SortExpression") = "LastName ASC" 
     End If 
     Return ViewState("SortExpression").ToString 
    End Get 
    Set(ByVal value As String) 
     ViewState("SortExpression") = value 
    End Set 
End Property 

Private Sub CreateGridColumns() 
    Dim curLastName As New BoundField 
    curLastName.HeaderText = "Last Name" 
    curLastName.DataField = "LastName" 
    curLastName.SortExpression = "LastName" 

    GridView1.Columns.Insert(0, curLastName) 


    Dim curFirstName As New BoundField 
    curFirstName.HeaderText = "First Name" 
    curFirstName.DataField = "FirstName" 
    curFirstName.SortExpression = "FirstName" 

    GridView1.Columns.Insert(1, curFirstName) 

End Sub 

Private Sub BindGrid() 
    Try 
     Dim tblData = New DataTable 
     Using sqlCon As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString()) 
      Dim sql As String = "SELECT * FROM Employees" 
      Dim sqlCmd = New SqlClient.SqlCommand() 
      sqlCmd.CommandText = String.Format(sql, Me.SortExpression) 
      sqlCmd.Connection = sqlCon 
      Using objAdapter As New SqlClient.SqlDataAdapter(sqlCmd) 
       objAdapter.Fill(tblData) 
      End Using 
     End Using 
     GridView1.DataSource = tblData 
     GridView1.DataBind() 
     GridView1.HeaderRow.CssClass = "HeaderStyle" 

    Catch ex As Exception 
     ' TODO: log error ' 
     Throw 
    End Try 
End Sub 
Private Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging 
    Me.GridView1.PageIndex = e.NewPageIndex 
    BindGrid() 
End Sub 
Protected Sub GridView1_RowDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
    'Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
    Dim gridView As GridView = DirectCast(sender, GridView) 
    Dim sortColumn As String, sortDirection As String 
    sortColumn = Me.SortExpression.Split(" "c)(0) 
    sortDirection = Me.SortExpression.Split(" "c)(1) 

    If e.Row.RowType = DataControlRowType.Header Then 
     Dim cellIndex As Integer = -1 
     For Each field As DataControlField In gridView.Columns 
      If field.SortExpression = sortColumn Then 
       cellIndex = gridView.Columns.IndexOf(field) 
      End If 
     Next 

     If cellIndex > -1 Then 
      ' this is a header row, set the sort style 
      e.Row.Cells(cellIndex).CssClass = If(sortDirection = "ASC", "sortasc", "sortdesc") 
     End If 
    End If 

End Sub 




Private Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting 
    Dim currentSortColumn, currentSortDirection As String 
    currentSortColumn = Me.SortExpression.Split(" "c)(0) 
    currentSortDirection = Me.SortExpression.Split(" "c)(1) 
    If e.SortExpression.Equals(currentSortColumn) Then 
     ' switch sort direction ' 
     Select Case currentSortDirection.ToUpper 
      Case "ASC" 
       Me.SortExpression = currentSortColumn & " DESC" 
      Case "DESC" 
       Me.SortExpression = currentSortColumn & " ASC" 
     End Select 
    Else 
     Me.SortExpression = e.SortExpression & " ASC" 
    End If 
    BindGrid() 

End Sub 


End Class 

任何幫助,不勝感激!

回答

0

如果我理解正確,您正在嘗試使自定義Gridview類。你可以按照下面的鏈接尋求你的幫助。你所需要做的就是創建一個繼承Gridview類的類,以便覆蓋默認的功能以使其更通用。

Extending the GridView Control

Custom Gridview with paging and filtering

Creating Custom Gridview Control

+0

這個信息應該有所幫助。謝謝。 – Cineno28 2012-02-28 18:16:21

+0

其實我在這裏還是有點困惑。我想要做的是創建一個我可以使用的外部類,以便當我想在任何頁面上創建GridView時,我可以使用該類,而不是在每個頁面的代碼隱藏中手動創建它。我提供的代碼用於從代碼隱藏中創建GridView。我只是想弄清楚如何重寫它,以便它可以從類創建。 – Cineno28 2012-02-28 20:14:57

+0

對你來說很老實,我一年前在MSDN上親自問過這個問題,MSFT傢伙說這是一項非常大的工作。 Gridview的擴展並不容易。我提供的是一個好的開始,但從那裏你就是我的朋友。:)祝你好運 – 2012-02-29 16:38:46