Hi dave,
Thanks very much for your feedback.
I see your concern, you used customized datagrid control, and you want to
handle the event in the control level not the page level.(I also see that
you prefer to use VB.net, so I will write my sample project in VB.net for
you)
===============================================
I think the implement based on your design of your web application.
If you want to handle the header "sort button" click event in your
customized datagrid control, you can add click event handler for the
button.
I suppose you only use the DataView as the datasource, then in the event
handler, you can get the dataview from the datasource.(cast the datasource
to dataview explicitly)
So you can set the Sort string in your control, then you can re-bind your
datagrid.
Sample code like this:
1).Customized datagrid control's code:
<ToolboxData("<{0}:Customdatagrid runat=server></{0}:Customdatagrid>")>
Public Class Customdatagrid
Inherits System.Web.UI.WebControls.DataGrid
Property SortExpression() As String
Get
Return CType(Me.ViewState("SortExpression"), String)
End Get
Set(ByVal Value As String)
Me.ViewState("SortExpression") = Value
End Set
End Property
Protected Overrides Sub OnItemCreated(ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Header Then
Dim dgi As DataGridItem = e.Item
Dim bt As Button = New Button
bt.ID = "bt"
bt.Text = "SortButton"
AddHandler bt.Click, AddressOf bt_Click
dgi.Cells(0).Controls.Add(bt)
End If
MyBase.OnItemCreated(e)
End Sub
Private Sub bt_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
If (Not Me.DataSource Is Nothing) And (TypeOf (Me.DataSource) Is
System.Data.DataView) Then
Dim dv As DataView = CType(Me.DataSource, DataView)
dv.Sort = Me.SortExpression
Me.DataBind()
End If
End Sub
End Class
2). The Web Page's code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Customdatagrid1.SortExpression = "job_id DESC"
Customdatagrid1.DataSource = getsource()
If (Not IsPostBack) Then
Me.Customdatagrid1.DataBind()
End If
End Sub
Private Function getsource() As DataView
Dim adapter As SqlDataAdapter = New SqlDataAdapter("select * from
jobs", "server=localhost;database=pubs;uid=sa;pwd=")
Dim ds As DataSet = New DataSet
adapter.Fill(ds)
Dim dv As DataView = ds.Tables(0).DefaultView
Return dv
End Function
More: I recommand you expose your header button's click event as its parent
control's(your customized datagrid) event, then you can handle this in the
page level.
This is called Bubble child control event to parent control.
For more information about how to Bubble event in .Net, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconbubblingcommandevent.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconeventbubblingcontrolsample.asp
================================================================
If you still have anything unclear, please feel free to tell me, I will
help you.
Have a nice day!!
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.