c# - The best way to perform actions on GridView rows -


i attempting perform action on rows in gridview. have read online, there several ways perform such actions. of these methods seem convoluted , manual plumbing involved.

for example, in order "delete" item gridview, i've come across these methods:

1: using gridview rowcommand event:

protected void gridview_rowcommand(object sender, gridviewcommandeventargs e) {     gridviewrow gridviewrow =          coordinatefilesgridview.rows[convert.toint32(e.commandargument)];     myentity entity = (myentity)gridviewrow.dataitem;      if(e.commandname.equals("delete"))     {         // perform delete action         delete(entity);     } } 

2: providing onclick event "delete" button

public void delete_clicked(object sender, system.eventargs e) {     var item = ((sender webcontrol).namingcontainer datalistitem);     var rowid = int.parse(((hiddenfield)item.findcontrol("rowid")).value);      gridviewrow gridviewrow = coordinatefilesgridview.rows[rowid];     myentity entity = (myentity)gridviewrow.dataitem;      // perform delete action     delete(entity); } 

3: linking url , breaking querystring down in page_load

if (querystring != null && querystring["action"] != null) {     if (querystring["action"].equals("delete") && querystring["rowid"] != null)     {         gridviewrow gridviewrow =              coordinatefilesgridview.rows[(int)querystring["rowid"]];         myentity entity = (myentity)gridviewrow.dataitem;          // perform delete action         delete(entity);     } } 

how perform such action? there better way this?

go either 1 or 2 based on own preference.

i go option 1 because built in gridview button handler, if have multiple buttons within rows, have 1 method handling these , reusing code -

gridviewrow gridviewrow =      coordinatefilesgridview.rows[convert.toint32(e.commandargument)]; myentity entity = (myentity)gridviewrow.dataitem; 

rather adding multiple click events , possibly adding code below individual click methods.

var item = ((sender webcontrol).namingcontainer datalistitem); var rowid = int.parse(((hiddenfield)item.findcontrol("rowid")).value); 

at end of day though, own preference. have used 1 , 2 on multiple occasions. cannot see reason why want use 3.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -