Solved

How to find Button in GridView edit template

Posted on 2009-05-04
7
1,332 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Author Comment

by:ki_ki
Comment Utility
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
Comment Utility
Thank you!
0
 
LVL 12

Expert Comment

by:wht1986
Comment Utility
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
Comment Utility
Cool glad you got it working :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now