DataGrid onClick

N

Neven Klofutar

Hi,

I need to create the following ...

I have DataGrid filled with information (ID, name, price ...).
I need to call a function when I click on the item in the DataGrid (and pass
ID as an argument to that function). I tried to add DataGrid_ItemDataBound
function, but I can't add Command and CommandArgument properties to each
DataGrid Item.


Thanx, Neven
 
E

Eliyahu Goldin

Neven,

Is the function on the client or server side?

If it is on the client, or even if it is on server, but you want to do
something on the client, for example highlighting the selected row, you can
take the following way.

Setup client side onclick event for items like in the following snippet:

private void dg_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;
if ((itemType == ListItemType.Pager) ||
(itemType == ListItemType.Header) ||
(itemType == ListItemType.Footer))
return;
e.Item.Attributes["onclick"] = "onSelectRowClick(this)";
}

Client side function onSelectRowClick accepts a reference to the row. This
makes all cell values available on client side. Now, if you need to pass one
of them back to the server you can do it in one of several ways of passing
form data from client to server.

Eliyahu
 
N

Neven Klofutar

I need to call server side function in order to put item in session object,
something like buying on online shop.

Thanx, Neven


Eliyahu Goldin said:
Neven,

Is the function on the client or server side?

If it is on the client, or even if it is on server, but you want to do
something on the client, for example highlighting the selected row, you can
take the following way.

Setup client side onclick event for items like in the following snippet:

private void dg_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;
if ((itemType == ListItemType.Pager) ||
(itemType == ListItemType.Header) ||
(itemType == ListItemType.Footer))
return;
e.Item.Attributes["onclick"] = "onSelectRowClick(this)";
}

Client side function onSelectRowClick accepts a reference to the row. This
makes all cell values available on client side. Now, if you need to pass one
of them back to the server you can do it in one of several ways of passing
form data from client to server.

Eliyahu

Neven Klofutar said:
Hi,

I need to create the following ...

I have DataGrid filled with information (ID, name, price ...).
I need to call a function when I click on the item in the DataGrid (and pass
ID as an argument to that function). I tried to add DataGrid_ItemDataBound
function, but I can't add Command and CommandArgument properties to each
DataGrid Item.


Thanx, Neven
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top