Ahmad
Ahmad

Reputation: 12737

How to handle OnRowClick event of ASPxGridView in code behind?

I am using ASP.Net with VB to display records in a grid view. In a normal ASP GridView, I can simply handle SelectedIndexChanged Event from code behind like this:

Sub mySub(ByVal sender as Object, ByVal e As EventArgs) 
          Handles GridView1.SelectedIndexChanged
  Dim id as String = GridView1.SelectedRow.Item(0).ToString
  Response.Redirect("Customer.aspx?ID=" & id) 'Or Whatever
End Sub

Now I am trying to do the same but with ASPxGridView that comes with DevExpress. Obviosly, there is no built in Event that can be handled directly from code behind. I have to go through client click with javascript, which I don't mind to but all my attempts to pass the click event from the client to the server code behind failed.

Here is my ASP page

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
     KeyFieldName="id" Width="550px" OnSelectionChanged="row_selected" >
    <SettingsBehavior AllowFocusedRow="True" />
    <SettingsText Title="Customers" />
    <Columns>
        <dx:GridViewDataTextColumn FieldName="id" ReadOnly="True" VisibleIndex="1">
            <EditFormSettings Visible="False" />
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="Name" VisibleIndex="2">
        </dx:GridViewDataTextColumn>
    </Columns>
    <Settings ShowTitlePanel="True" />
    <ClientSideEvents FocusedRowChanged="function(s, e) {
        row_selected();
        }" />
</dx:ASPxGridView>

And here is my code behind (Which never gets called)

Sub row_selected()
    Dim id as String = ASPxGridView1.SelectedRow.Item(0).ToString
    Response.Redirect("Customers.aspx?ID=" & id) 
End Sub

How can I call a function from code behind by clicking on a row on ASPxGridView row?

Upvotes: 0

Views: 14641

Answers (2)

GWB_CODE_MAD
GWB_CODE_MAD

Reputation: 21

This will do what you want..

In my case,

I want to get contents of a field ('ID') from ASPxGridView when the user clicks on the row...

Create the ClientSideEvent for row click and put "RowClick(s, e);" in the function.

Create the actual function the event will call as shown below - and here is the tricky part;

do not use GetFocusedRowIndex() to get the index because it is the FOCUSED index.

Use e.visibleIndex

function RowClick(s, e) {
    // Do callback to get the row data for 'ID' using current row.
    MyAspxGridView.GetRowValues(e.visibleIndex, 'ID', OnGetRowId);
}

Create your call back to get the field you want. I'm getting 'ID'.

function OnGetRowId(idValue) {
    alert('ID: ' + idValue.toString());
}

Upvotes: 1

Filip
Filip

Reputation: 3347

Try this:
1. Set ASPxGridView1.SettingsBehavior.ProcessFocusedRowChangedOnServer to true.
2. Handle server side FocusedRowChanged event

Upvotes: 2

Related Questions