Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Referencing an asp:Checkbox contorl to a row (item) in a datagrid


Message #1 by "Sascha Ederer" <sascha@e...> on Thu, 18 Oct 2001 11:10:56
Dear Experts,



I use a DataGrid to diplay the contents of a database table on a .aspx 

site. I added a column, bearing a checkbox, to select items out of the 

DataGrid.

 

I tried to use an event handler to react to every click at the checkboxes. 

When an item (row of the DataGird) is selected / deselected, the 

coresponding item should be stored  /deleted immediately in a separate 

database. 



The problem is, that I am not able to get a reference from the checkbox to 

the corresponding datarow of the datagrid, when the "oncheckedchanged" 

event of the checkbox is fired. My first idea was to use the ID of the 

checkboxes, but the checkboxes in the different rows of the datagrid have 

all the same ID, as defined at the ItemTemplate within the "<ASP:DataGrid" 

tag.



Is there any other (hopefully comfortable) way to reference to a row 

within a datagrid, or an other event (maybe a helpful DataGrid event)? I 

already tried the OnItemCommand event of the datagrid, but this seems to 

work only with command buttons and not with checkboxes.



Thanks in advance for your helpful advices.



Regards,



Sascha
Message #2 by "Chris Scott" <chris@e...> on Thu, 18 Oct 2001 13:16:31 -0700
Hi Sascha,



I found a solution to a similar problem, but was using buttons instead of a

check box:



- add an OnItemCommand attribute to the datagrid e.g.

OnItemCommand="myHandler"

- add CommandName attributes to the buttons e.g. CommandName="Save" &

CommandName="Delete"

- Create the handler:



void MyHandler(object sender, DataGridCommandEventArgs e)

{

    if(e.CommandName="Save")

    {

        ...save code here...

    }



    if(e.CommandName="Delete")

    {

        ...delete code here

    }

}



In your save/delete code you can access the datagrid cells for the row:

string myCellValue=e.Item.Cells[1].Text;



HTH



Chris



Message #3 by "Sascha Ederer" <sascha@e...> on Thu, 18 Oct 2001 14:18:17
Dear Chris,



I tried your solution, but the problem with checkboxes is, that they don't 

have the "CommandName" attribute that buttons have. But I keep your clue 

in mind and try to use Buttons instead of Checkboxes, if there is no other 

comfortable solution.



Thanks alot,



Sascha






  Return to Index