Solved

How to find Button in GridView edit template

Posted on 2009-05-04
7
1,338 Views
Last Modified: 2012-05-06
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 !
0
Comment
Question by:ki_ki
  • 4
  • 3
7 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 75 total points
ID: 24297191
yeah MS dropped the ball on this one, you have to do a bit check to see if the alternate row is in an edit state. The snippet below works for all rows
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState & DataControlRowState.Edit) > 0))
            {
                Button btn = (Button)e.Row.FindControl("insertDateCodeButton");
                btn.CommandArgument = e.Row.RowIndex.ToString();
                btn.Text = e.Row.RowIndex.ToString();
            }
        }

Open in new window

0
 
LVL 4

Author Comment

by:ki_ki
ID: 24297271
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?
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24297318
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
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 4

Author Comment

by:ki_ki
ID: 24297379
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!!!!!!
0
 
LVL 4

Author Closing Comment

by:ki_ki
ID: 31577625
Thank you!
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24297408
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

0
 
LVL 12

Expert Comment

by:wht1986
ID: 24297410
Cool glad you got it working :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question