Solved

How to find Button in GridView edit template

Posted on 2009-05-04
7
1,333 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disable and re-enable a dynamic System.Timers.Timer 6 40
ASP.NET Web API or ASP.NET Core MVC? 3 52
How do ASP.NET and MVC work together? 4 27
Name space syntax error 12 41
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

22 Experts available now in Live!

Get 1:1 Help Now