Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine event handling for separate DataGrids? 2

Status
Not open for further replies.

kubu

Programmer
Jan 10, 2001
23
US
I've got an .aspx page containing five DataGrids. I'd like to be able to handle the various events for these grids in common codebehind sub procedures.

For example, I want to have a sub procedure called EditGridRow() in the codebehind, and give each <asp: datagrid> element in the .aspx page the attribute OnEditCommand=&quot;EditGridRow&quot;.

Question is, how do I determine which DataGrid fired the event?

I know that the EditGridRow procedure will expect a source Object object and a DataGridCommandEventArgs object, but is it possible to determine the identity of the DataGrid itself from one of these two objects?

Thanks!
Mark
 
No, there isn't. At least not that I've been able to figure out...

The events bubble up in their own little fashion, and if someone clicks a row on the fifth grid on the page, then by the time your code knows anything about it, the first four have come and gone.

However, there is good news.

(1)They are smart enough to know who's who.

By that, I mean that they won't get confused as to which one raised the event, and each one handles its own events autonomously.

(2)You can construct your own scheme to get events to cascade through the grids.

For example, in a recent project, I had a variable number of grids on a page, and each were sortable. Problem was that if a user clicked a sort, then using the default sort routine, only the grid they clicked got sorted (see #1).

To workaround this &quot;problem&quot;, I took off the normal sorting routine, and instead, made the header of each row call a common javascript function, and sent information about the sort to that function (much like you would have implemented a sort in ASP Classic).

This javascript set a hidden form variable to that value, which I then evaluated first thing on the page_load event.

At that point, I had a variable that I could set via properties to each of my datagrids to apply the sort.

You could implement the same type of logic w/ editable rows, but you're going to run into many more problems. You're going to have lots of values hanging out there in space w/ little or no way to tie them back to the proper datagrid (remember that all you get by default is an &quot;edititemindex&quot; to do your edits). But I'll leave those details up to you.

:)
paul
penny1.gif
penny1.gif
 
If I understood the question, u want to know which grid is currently handling the event, right?

If that's so, I think u can know the name of the grid used using the line:

sender.id

This gives u the name of the current datagrid.
NetAngel
 
Thanks, both!

Actually, Paul, I think netangel's solution will work for me, as my grids are static on the page rather than dynamically built. I guess I could have worded my question better (or at least simplified it somewhat). Essentially, all I need to know is what DataGrid fires the event. I didn't know if the sender Object was the grid itself, or the grid item (row). However, your advice will come in handy for other ideas I have.

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top