Link to home
Start Free TrialLog in
Avatar of ki_ki
ki_kiFlag for United States of America

asked on

How to find Button in GridView edit template

Hi all,
I have gridview1 with template columns.
In one of the columns I have gridView2 with template columns too. In the edittemplate, gridview2, I have a button (insertDateCodeButton) and I want to set the commandArgument of that button to e.Row.RowIndex. How do I "find" insertdateCodeButton?
I am using:

Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then

            If e.Row.RowState = DataControlRowState.Edit Then
               
            CType(e.Row.FindControl("addNewDateCodeButton"), Button).CommandArgument = e.Row.RowIndex
               
            End If

        End If

End Sub

The if statement is true for only row number:0,2,4 , etc... How do i get the alternate rows?
Thank you !
ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America 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 ki_ki

ASKER

Thank you wht1986,

"e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState & DataControlRowState.Edit) > 0))" is the same as what I already have (in vb.net):
" If e.Row.RowType = DataControlRowType.DataRow Then
            If e.Row.RowState = DataControlRowState.Edit Then " 
Am I missing something?
it's not the same, look at the code

(e.Row.RowState & DataControlRowState.Edit) > 0

is a bitwise compare

the problem is on alternate rows, the row state is actually a combination of edit and alternate row states, so what the above statement does is looks at the bit mask for just the edit portion to see if it is set
Avatar of ki_ki

ASKER

I converted your c# code wrongly to vb.net.

But now it works:

If e.Row.RowType = DataControlRowType.DataRow And (e.Row.RowState And DataControlRowState.Edit) > 0 Then
      CType(e.Row.FindControl("addNewDateCodeButton"), Button).CommandArgument = e.Row.RowIndex
End If

Thank you!!!!!!
Avatar of ki_ki

ASKER

Thank you!
Here's the VB.NET Version
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If ((e.Row.RowType = DataControlRowType.DataRow) And ((e.Row.RowState And DataControlRowState.Edit) > 0)) Then
            Dim btn As Button = e.Row.FindControl("insertDateCodeButton")
            btn.CommandArgument = e.Row.RowIndex.ToString()
            btn.Text = e.Row.RowIndex.ToString()
        End If
    End Sub

Open in new window

Cool glad you got it working :)