how to get value of datarow column on ItemDataBound

X

xzzy

For each row in a dataset bound to a datagrid, each row can differ by
MyType ( ie 1, 2, 3, . . . ).

for different MyType, I need to call different javascript functions and pass
the values in the datagrid's cells

Problem: no values are passed to the javascript functions.


protected void DataGrid_ItemDataBound(object sender, DataGridItemEventArgs
e)
{
try
{
if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem )
{
int myType =
Convert.ToInt32(ctlSearchResults.SelectedItem.Cells[1].Text);
switch ( myType )
{
case 1:
e.Item.Attributes.Add("onclick", "MyClientSideFunction01("
+ ctlSearchResults.SelectedItem.Cells[2].Text) + "," +
ctlSearchResults.SelectedItem.Cells[3].Text + ")");
break;
case 2:
e.Item.Attributes.Add("onclick", "MyClientSideFunction02("
+ ctlSearchResults.SelectedItem.Cells[2].Text) + "," +
ctlSearchResults.SelectedItem.Cells[3].Text + ")");
break;
default:
e.Item.Attributes.Add("onclick", "MyClientSideFunction01("
+ ctlSearchResults.SelectedItem.Cells[2].Text) + "," +
ctlSearchResults.SelectedItem.Cells[3].Text + ")");
break;
}
}
}
}


Thank you,

John
 
B

Bhuva

Hi John,

Instead of SelectedItem.Cells[1].Text, use e.Item.Cells[1].Text to pass
the datagrid values to javascript function.

Here is the modified code.

try
{
if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem )
{
int myType =Convert.ToInt32(e.Item.Cells[0].Text);
switch ( myType )
{
case 1:
e.Item.Attributes.Add("onclick", "Function1('"+
e.Item.Cells[1].Text +"','" +e.Item.Cells[2].Text + "');");

break;
case 2:
e.Item.Attributes.Add("onclick", "Function2('"+
e.Item.Cells[1].Text +"','" +
e.Item.Cells[2].Text + "');");
break;
default:
e.Item.Attributes.Add("onclick", "Function3('"+
e.Item.Cells[1].Text +"','" +
e.Item.Cells[2].Text + "');");
break;
}
}
}

-Bhuva
[www.syncfusion.com
http://www.syncfusion.com/faq/aspnet/default.aspx]
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top