Change button text for each line of GridView


I have built the a web page with the GridView shown below (markup of one button further). I want to change the text on the buttons for each line and hide  certain buttons too. How do I do this?

       <asp:TemplateField HeaderText="Undecided" HeaderStyle-Font-Size ="Small"
                                            ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="SlateGray"
                                            ControlStyle-Font-Size ="Smaller">
                        <asp:Button ID="Button8" runat="server"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Undecided" />
                    <ControlStyle Font-Bold="False" Font-Size="Smaller" ForeColor="SlateGray" />
                    <HeaderStyle Font-Size="Small" />
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
CtrlAltDlConnect With a Mentor Commented:
You need a RowDataBound event.  It will call this event while it creates each row at that point you can rename buttons and or hide them.

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        ' Required to ignore the header and footer rows
        If e.Row.RowType = DataControlRowType.DataRow Then
                 'Check if it's Edit mode
                 If e.Row.RowState = DataControlRowState.Edit Then
                      'To access controls in a ItemTemplate
                      Dim gender= row.Field(Of String)("gender")
                      Dim Button8= DirectCast(e.Row.FindControl("Button8"), Button)

                       If gender = "Male" Then
                           Button8.Text = "Save Boy"
                       End If
                 End If
        End If
    End Sub

Open in new window

Disclaimer: My code may have some syntax errors, since I'm a C# dev.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.