Last post Feb 02, 2016 09:12 AM by Candice Zhou
Jan 09, 2016 07:59 PM|mavila|LINK
I have a Gridview control in a User Control which I use on a number of different pages and wizard control steps. Depending upon the page or wizard control step, I configure the gridview control differently - making buttons visible, other buttons not visible,
checkboxes visible or not visible, etc. This is easily accomplished in the rowdatabound event of the gridview control. However, handling that event in the User Control's code behind requires me to determine which page the User Control, or which wizard step
the User Control is on which can be a bit of a headache.
I've found that I can manage these different configurations from the parent/containing page by looping through the rows on the gridview and configuring as needed. But it seems to me that this is an extra step that shouldn't be necessary. If I could handle
the rowdatabound event on the parent page's code behind, then the configuration could be made as the event is fired without the need to go back and loop through the rows to make the configuration.
I've read a lot about bubbling events, delegates, and many examples, but I don't understand the concept very well and most every example I've seen is in C#. Would anyone know of a good example in visual basic? Is this even the best way to handle my issue?
Any information or advice is appreciated.
Jan 11, 2016 05:48 AM|Candice Zhou|LINK
You could refer to the following steps to create a generic GridView as a user control, then you can extend it in its parent page as per your requirements.
1.Create a user control with a GridView.
2.To access its column collection in the parent page, create a Columns property.
Public ReadOnly Property Columns() As DataControlFieldCollection
3.Created a function to bind the datasource to the GridView.
Public Sub BindData(dt As DataTable)
Simple_DGV.DataSource = dt
4.Now our basic GridView is done. Open the ASPX page where you are using this control and drag and drop this control.
5.Now open the aspx.cs file. There, you can add the columns to the grid and pass the datasource to bind to.
Private Sub BindData()
'clear previous all column
Dim b As New BoundField()
'create bound column
b.HeaderText = "Column1"
b.DataField = "c"
'add column to GridView
Dim b1 As New BoundField()
b1.HeaderText = "Column2"
b1.DataField = "c1"
' pass datasource to bind
6.Now create the events in the User control to access it on the parent (ASPX) page. We will create a RowDataBound event which gets fired at the time of databinding.
7.Add an event handler for RowDataBound and specify the event.
Protected Sub Simple_DGV_RowDataBound(sender As Object, e As GridViewRowEventArgs)
8.Then in the event function, check that no one else is using the event.
Protected Sub OnRowDataBound(e As GridViewRowEventArgs)
RaiseEvent GridRowDataBound(Me, e)
9.To access this event in the parent page, we need to use a delegate, so define a delegate and its event in the user control.
Public Delegate Sub RowDataBound(sender As Object, e As GridViewRowEventArgs)
Public Event GridRowDataBound As RowDataBound
10.To use this event in the parent page, we need to add an event handler in the InitializeComponent method of the parent page.
Protected Overrides Sub OnInit(e As EventArgs)
Private Sub InitializeComponent()
Me.Load += New System.EventHandler(Me.Page_Load)
Inc_GridView1.GridRowDataBound += New Inc_GridView.RowDataBound
11.Now we are ready to access the event. I am changing the color of the row here. We can also specify a condition when to change the color.
Private Sub Inc_GridView1_GridRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.BackColor = System.Drawing.Color.FromArgb(236, 245, 248)
Jan 17, 2016 08:04 PM|mavila|LINK
Thank you Candice. I'm trying your example.
In step #10, in the InitializeComponent Sub, I'm getting some warnings.
For "Me.Load," I'm getting, "Public Event Load(Sender As Object, e As System.EventArgs) is an event, and cannot be called directly. Use a "Raise Event" statement to raise an event."
For "Inc_GridView1.GridRowDataBound," I'm getting, "Inc_GridView1 is not declared." Same for "Inc_Gridview1.RowDataBound."
Thanks, again, for your help.
Jan 19, 2016 09:14 AM|Candice Zhou|LINK
Sorry for my mistake. It needs to use the AddHandler statement, not +=, that is c# syntax.
Jan 29, 2016 01:43 AM|mavila|LINK
I appreciate the time you took to help me, but I still can't make it work. Thank you, but I guess I will have to look for another solution. I guess AddHandler it beyond my abilities. It seems like this should be a pretty common thing for developers to have
to manage, but I find the solutions are either in C# which doesn't translate very well on this particular coding (I've used the translaters a bunch), or not quite on point with what my needs are. Oh well, sometimes you have to move on with a less than optimal
Feb 02, 2016 09:12 AM|Candice Zhou|LINK
Modify it like below:
AddHandler Me.Load , AddressOf New System.EventHandler(Me.Page_Load)