3

I have several pages that all have a GridView control. Within that control there are several events that need to be implemented (OnRowDataBound, OnEditing, etc...)

Any suggestions as far as inheriting from a page that will force me (virtual functions) to implement each of these events? I can't visualize how that would look because of the GridView being a control. How do I inherit from a control?

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
duckmike
  • 1,006
  • 4
  • 16
  • 39

1 Answers1

2

A common way of doing this is to contain your GridView in a UserControl

If you place your grid on the user control, and subscribe to all the events there, then you can reuse this implementation anywhere you want in the application - you are essentially just wrapping the user control with the functionality you want, and then reusing this wrapper everywhere.

The neat thing is that you only have to write the wire-up code once.

That's what I think you want to do.

If you want to extend or handle these events outside the usercontrol, though, you'll need to reimplement them (i.e. add your own event handlers and let those bubble up).

You can't force anyone to subscribe to your events though.

If you wanted to force an implementation of the events then you can put the events on an interface, and implement that interface on the class or control you add it to. As the events are on the interface, you will not be able to compile until the interface has been implmented.

If you have a specific use case I can try to add an example.

dash
  • 89,546
  • 4
  • 51
  • 71
  • Then how do I force every page that uses this control to implement all of the events? Would I put similar functionality, not specific to this control, within this new user control? – duckmike Feb 27 '12 at 16:09
  • The page doesn't need to implement these events; it just needs to subscribe to them. You can't force someone to subscribe to an event. If you want to make sure these events are always implemented then you do that in your usercontrol, but it really depends on exactly what you want to do. – dash Feb 27 '12 at 16:12
  • can you please give a link that has implemented a wrapping of gridview in usercontrol? Thanks – Pankaj Feb 28 '12 at 15:49
  • @PankajGarg You aren't really wrapping the GridView itself - you are putting your GridView in a user control, and then doing all the wiring up, data handling etc in there. You've effectively wrapped your "functionality" in a user control that happens to be using a GridView. If you have a specific function/problem you want to see/solve I'd be happy to try and give a demo. – dash Feb 28 '12 at 15:54
  • I was very curious to know about the advantages of wrapping gridview control inside usercontrol. We normally write RowBoundData / RowCommand events and bind the data and each GridView has some specific control IDs which we want to find in these events so what can be the basic benefit of confining this control in user control ? As there seems nothing common to keep it in one place and reuse it. Normally we use it in Database driven applications. – Pankaj Feb 28 '12 at 16:02
  • will you like to explain it with an example or link ? – Pankaj Feb 28 '12 at 16:08
  • In which case, there are no advantages in wrapping it in a UserControl - if it is displaying data differently each time then you wont get any real advantage from it (except where you do similar things like styling etc). You only encapsulate in a user control if you feel like you are going to be sharing similar functionality in many places. It's basically the same as the generic answer here: http://stackoverflow.com/questions/8540679/advantages-and-disadvantages-of-usercontrol-in-asp-net/8540741#8540741,also http://www.codeproject.com/Articles/59160/Extended-GridView-with-Fixed-Header-and-Pager – dash Feb 28 '12 at 16:20