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

Discussion in 'ASP .Net Datagrid Control' started by Dennis Fricker, Oct 4, 2004.

  1. 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
    Dennis Fricker, Oct 4, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,150
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. =?Utf-8?B?UG9udGlNYXg=?=

    DataGrid: Server-side event for row click?

    =?Utf-8?B?UG9udGlNYXg=?=, May 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,404
    =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    May 7, 2004
  3. =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=
    Replies:
    2
    Views:
    6,460
    rick74
    May 27, 2008
  4. PontiMax

    Server-side event for row click?

    PontiMax, May 7, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    110
    PontiMax
    May 7, 2004
  5. Replies:
    0
    Views:
    156
Loading...

Share This Page