Solved

ASP.net Change button text for each line of GridView

Posted on 2015-01-20
2
352 Views
Last Modified: 2015-01-20
Hi

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?

1
       <asp:TemplateField HeaderText="Undecided" HeaderStyle-Font-Size ="Small"
                                            ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="SlateGray"
                                            ControlStyle-Font-Size ="Smaller">
                    <ItemTemplate>
                        <asp:Button ID="Button8" runat="server"
                      CommandName="MyButton3"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Undecided" />
                    </ItemTemplate>
                    <ControlStyle Font-Bold="False" Font-Size="Smaller" ForeColor="SlateGray" />
                    <HeaderStyle Font-Size="Small" />
                </asp:TemplateField>
0
Comment
Question by:murbro
2 Comments
 
LVL 14

Accepted Solution

by:
CtrlAltDl earned 500 total points
ID: 40560805
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"
                       Else
                           Button8.Hide()
                       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.
0
 

Author Closing Comment

by:murbro
ID: 40561307
Thanks very much
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

16 Experts available now in Live!

Get 1:1 Help Now