How To: handle DataGrid row Click Event that passes rows column values to server-side code behind fu

D

Dennis Fricker

I've looked through many posted messages, and have tried several
things but have not seemed to solve this (what you'd think would be a
simple) task. Most likely it is staring me smack in the face.

I have a DataGrid web server control that does not have and user does
not want linkbutton/buttoncolumn to retrieve multiple databound column
specific row values. In other words I need to clinck anywhere on a
row, retrieve all databound column values of that row, pass those
values to a vb code behind function or event to process.

I'm somewhat new to ASP.NET and not real strong on javascript. I've
had most of
my experience with asp, vbscript and vb. I have found examples posted
that trigger doubleclicked, clicked and mouseover row events,
hightliand rows, pass _doPostBack to page, that actually pass the
clicked DataBound Column row values to a client-side Javascript
function. Mostly the perform alert("....") calls.

What I really need now is a clear code example on how to have that
javascript function, call a vb code behind function or event passing
it individual or a collection of the clicked row databound column
values.

Here's what I've figured out or found out thus far. Maybe someone can
extend these examples:

EXAMPLE 1:
=============================================================
Code Behind 'ItemDataBound' Event for DoubleClicking on Row
=============================================================
Sub MyDataGrid_ItemDataBound(ByVal sender as object, Byval e as
DataGridItemEvenArgs)

Dim lblSID As Label
Dim tblStoreName as TableRow

Select Case e.ItemType
Case ListItemType.Item
lblSID = Ctype(e.Item.FindControl("hdnStoreID"), Label)
tblStoreName = e.Item.FindControl("lblStoreName")Parent.Parent
tblStoreName.Attributes.Add("OnDblClick", "ShowDetails("& lblSID
& ");")
Case ListItemType.AlternatingItem
lblSID = CType(e.Item.FindControl("hdnStoreID"), Label)
tblStoreName = e.Item.FindControl("lblStoreName")Parent.Parent
tblStoreName.Attributes.Add("OnDblClick", "ShowDetails("& lblSID
& ");")
End Select

End Sub

===============================================
javascript clinet-side function: "ShowDetails"
===============================================
<script language=javascript>
function ShowDetails(sid, sn, sadd, scity, sstate, szip)
{
alert("You selected \n"+ unescape(sid) + "" + unescape(sn) + "" +
sadd + ""
+ scity + "" + sstate + szip);
}
</script>


EXAMPLE 2:
=================================================================
Code Behind 'ItemDataBound' Event for postback of Row ItemIndex
=================================================================
Sub MyDataGrid_ItemDataBound(ByVal sender as object, Byval e as
DataGridItemEvenArgs)

Dim jsDataGridID As String
Dim test As String
jsDataGridID = dgLossTrends.ID + "$" ' find the datagrid name and
add a
dollar sign

'This code will highlight a row when the mouse moves over. It will
'not behave correctly if you have separately colored columns... It
'also triggers a DoubleClick event.

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

With e.Item
.Attributes.Add("onmouseover", "this.style.backgroundColor =
'Khaki';")
.Attributes.Add("onmouseout", "this.style.backgroundColor =
'white';")
.Attributes.Add("ondblclick", "javascript:__doPostBack('" +
jsDataGridID
+ "_ctl" + (e.Item.ItemIndex + 3).ToString()
+ "$_ctl0','');")
End With

End If

End Sub

EXAMPLE 3:
=================================================================
Code Behind 'ItemDataBound' Event for Clinking on Row
=================================================================
Sub MyDataGrid_ItemDataBound(ByVal sender as object, Byval e as
DataGridItemEvenArgs)


'Check to see you are NOT dealing with a Header or Footer row, please
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer
Then

Dim dv As DataView = DataGrid1.DataSource
Dim dcCol As DataCoulumn
Dim dc As DataColumnCollection = dv.Table.Columns
Dim strID as String
For Each dcCOl In dv.Table.Columns
If e.Item.ItemType = ListItemType.AlternatingItem or _
e.Item.ItemType = ListItemType.Item Then

e.Item.Cells(dcIndexOf(dc.(dcCol.ColumnName))).Attributes.Add("onclick",_
"javascript:window.open('dganyrowclicked1.aspx?id="& strID &
"'," _
& "'MyPage','height=300,width=300')")
End If
Next

End If

End Sub

Dennis
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top