Link to home
Start Free TrialLog in
Avatar of golfDoctor
golfDoctor

asked on

What is the "correct" way to get record ID value from gridview row?

The code I have works fine, but I'm sure it's not the proper way to do it.

I have a gridview (GV), with ID column and with a checkbox field for Active (see attached picture).  The GV is using an SQL data source (SDS).  The SDS has an update statement for updating the Active column, with 2 parameters, the active value and the record ID.

I'm calling the SDS Update statement from code behind, using OnCheckedChanged, and I am passing the ID value in the tabindex field.  Using the tabindex field is where this is "non-standard" practice.

The question is, how would I access the ID of the GV row, rather than use the TabIndex improperly, to hold the ID value to pass into this procedure.
'...other fields
<asp:TemplateField HeaderText="Active" SortExpression="Active">
       <ItemTemplate>
             <asp:CheckBox ID="Active" name='1' runat="server" Checked='<%# Eval("Active")%>' OnCheckedChanged='UpdateStatus' AutoPostBack="true" TabIndex='<%# Eval("ID")%>'></asp:CheckBox>
        </ItemTemplate>
</asp:TemplateField>
 
'Code behind
Public Sub UpdateStatus(ByVal sender As Object, ByVal e As System.EventArgs)
        SDS.UpdateParameters("ID").DefaultValue = Convert.ToInt32(sender.TabIndex) 'ID stored in TabIndex
        SDS.UpdateParameters("Active").DefaultValue = Convert.ToInt32(sender.Checked) 'Checkbox checked or not
        SDS.Update()  'Update the data in SDS
End Sub

Open in new window

grid.jpg
SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of golfDoctor
golfDoctor

ASKER

examples?
I'd like to continue using the procedure UpdateStatus, to retain the sender.checked value.  Can the datakey be accessed within that procedure?
Yes it is possible see the Ex.

<asp:gridview id="CustomersGridView"
        datasourceid="CustomersSource"
        autogeneratecolumns="true"
        emptydatatext="No data available."
        autogenerateselectbutton="true"    
        datakeynames="CustomerID"
       
        runat="server">



Code behine in Procedure
Dim index As Integer = CustomersGridView.SelectedIndex

    ' Display the primary key value of the selected row.
   
    Response.wRite( CustomersGridView.DataKeys(index).Value.ToString() )
Public Sub UpdateStatus(ByVal sender As Object, ByVal e As System.EventArgs)
Dim index As Integer = CustomersGridView.SelectedIndex
        SDS.UpdateParameters("ID").DefaultValue = Convert.ToInt32(CustomersGridView.DataKeys(index).Value) 'ID stored in TabIndex
        SDS.UpdateParameters("Active").DefaultValue = Convert.ToInt32(sender.Checked) 'Checkbox checked or not
        SDS.Update()  'Update the data in SDS
End Sub
I saw that example on MS.  OK, I didn't think the SelectedIndex would be available from the checkbox action.  I will test.
I didn't think that would work, because the select index change event doesn't seem to occur from the checkbox action. It returns -1 as the index.
Whoops, I didn't mean to try to select my answer to close this question.  Was just checking out this noew feature, I hadn't seen it before.

Please ignore the "Notice: The Asker has requested that this comment be " thing...
Anyone have any other suggestions?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I do like my solution, although non-standard, like any coding there's always several way to do something.  I just wanted to find out if there is a "correct" way.  But, based on that article, it seems mine is much less code and easier.
yup; looks like a great workaround and a lot easier to read when you have to change your code in say 6 months or so for whatever reason :) so id stick with your code if i where you
Thanks for the comments.  I guess I'll stick with my answer.
I discovered a better way to do this.  The TabIndex field is Int16, and when you get into higher numbers, using TabIndex will error.   Works fine for small numbers though.

So what you do instead is use a hidden label field in the itemtemplate and put the ID field in that, and then use FindControl("labelID") to get the id:

Dim row As GridViewRow = CType(CType(sender, CheckBox).NamingContainer, GridViewRow)
Dim id As Integer = CInt(CType(row.FindControl("mylabel"), Label).Text)