0

如何根据 gridview 中的值更改 Gridview 中按钮的文本

例如,在下面的代码中,对于年龄低于 40 的行,如何将按钮文本设置为 YOUNG,对于高于或等于 40 的行,如何将按钮文本设置为 OLD

<asp:GridView ID="GV_DataByGroupAct" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Age" HeaderText="Age" />
        <asp:BoundField DataField="Hight" HeaderText="Hight" />
        <asp:ButtonField Text="Young/Old" />
    </Columns>
</asp:GridView>

谢谢

4

2 回答 2

0

您需要处理RowDataBound网格视图的事件,以便在绑定时更改每行按钮文本的内容,如下所示:

Protected Sub GridView1_RowDataBound(sender As [Object], e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        ' Put logic here to check age and then update button text
    End If
End Sub
于 2013-09-05T15:09:28.547 回答
0

我会用TemplateField一个真正的Button控制和RowDataBound你的逻辑:

aspx:

<asp:GridView ID="GV_DataByGroupAct" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Age" HeaderText="Age" />
        <asp:BoundField DataField="Hight" HeaderText="Hight" />
        <asp:TemplateField>
           <ItemTemplate>
              <asp:Button ID="BtnYoungOld" runat="server" Text="Young/Old" />
           </ItemTemplate>
       </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏:

Private Sub GV_DataByGroupAct_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GV_DataByGroupAct.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            Dim row = DirectCast(e.Row.DataItem, DataRowView)
            Dim age As Int32 = Int32.Parse(row("age").ToString)
            Dim BtnYoungOld = DirectCast(e.Row.FindControl("BtnYoungOld"), Button)
            BtnYoungOld.Text = If(age < 40, "YOUNG", "OLD")
    End Select
End Sub
于 2013-09-05T15:08:26.830 回答