Gridview RowCommand事件

时间:2013-09-07 04:27:43

标签: c# gridview

我有一个gridview,其中boundfield就像这样 -

 <asp:BoundField  HeaderText="Approved" />

在这个gridview的rowcommand事件中,我想根据命令名称显示一些文本,例如

protected void gwFacultyStaff_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Yes"))     
    {
        string id = e.CommandArgument.ToString();
        GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        int index = Convert.ToInt32(row.RowIndex);
        GridViewRow rows = gwFacultyStaff.Rows[index];
        rows.Cells[12].Text = "TRUE";     
    }
    else if (e.CommandName.Equals("No"))
    {
        string id = e.CommandArgument.ToString();
        GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        int index = Convert.ToInt32(row.RowIndex);
        GridViewRow rows = gwFacultyStaff.Rows[index];
        rows.Cells[12].Text = "FALSE";
    }
}

但它没有向我显示我想要显示的所需文本。任何人都可以向我建议可能的解决方案吗?

1 个答案:

答案 0 :(得分:1)

而不是BoundField使用TemplateField,如下所示:

<asp:TemplateField HeaderText="Approved">
    <ItemTemplate>
        <asp:Label id="LabelApproved" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>

现在,在RowCommand事件中,您可以执行此操作:

protected void gwFacultyStaff_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Yes"))     
    {
        GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        Label theLabel = row.FindControl("LabelApproved") as Label;
        theLabel.Text = "TRUE";
    }
    else if (e.CommandName.Equals("No"))
    {
        GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        Label theLabel = row.FindControl("LabelApproved") as Label;
        theLabel.Text = "FALSE";
    }
}