简体   繁体   中英

How to display some text in a GridView button

I have the following code which is a GridView:

<asp:GridView ID="BookingResults" runat="server" EnableModelValidation="True" OnRowCommand="BookingResults_RowCommand" AutoGenerateColumns="False">
                <Columns>
                <asp:ButtonField Text=" " ButtonType="Button" CommandName="ViewAll"  HeaderText="Show Guideline" ItemStyle-HorizontalAlign="Center">
                    </asp:ButtonField>
                    <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-VerticalAlign="Top" />
                    <asp:BoundField DataField="Topic" HeaderText="Topic" SortExpression="Topic" ItemStyle-VerticalAlign="Top" />
                    <asp:BoundField DataField="Location" HeaderText="Location" ItemStyle-VerticalAlign="Top" />
                    <asp:BoundField DataField="Specialty" HeaderText="Specialty" ItemStyle-VerticalAlign="Top" />
                    <asp:BoundField DataField="Provider" HeaderText="Provider" ItemStyle-VerticalAlign="Top" />
                    <asp:TemplateField HeaderText="Summary" ItemStyle-VerticalAlign="Top">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Summary") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Summary") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Guideline" HeaderText="Guideline" />
                </Columns>
             </asp:GridView>

The end result is this:

在此处输入图片说明

I can't seem to give the ButtonField an ID so I can display text per line. How can I display an increment value from 1 and so forth in the button from code-behind?

Tap into RowDataBound event, and add the logic:

var btn = (IButtonControl)e.Row.Cells[0].Controls[0];
btn.Text = indexVariable;

The button is usually the first control of the cell you defined it, which since it's the first column, would be cell index zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM