如何在GridView按钮中显示一些文本

时间:2014-06-11 15:44:57

标签: c# asp.net gridview

我有以下代码,它是一个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>

最终结果如下:

enter image description here

我似乎无法给ButtonField ID,所以我可以每行显示文字。如何在代码隐藏的按钮中显示1等的增量值?

1 个答案:

答案 0 :(得分:2)

点击RowDataBound事件,并添加逻辑:

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

按钮通常是您定义它的单元格的第一个控件,因为它是第一列,因此单元格索引为零。