?
Solved

How to find Button in GridView edit template

Posted on 2009-05-04
7
Medium Priority
?
1,342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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