首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gridview RowCommand事件

Gridview RowCommand事件
EN

Stack Overflow用户
提问于 2013-09-07 04:27:43
回答 1查看 6.9K关注 0票数 1

我有一个网格视图,其中的边界是这样的东西-

代码语言:javascript
复制
 <asp:BoundField  HeaderText="Approved" />

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

代码语言:javascript
复制
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";
    }
}

但它并没有给我展示我想要显示的所需的文本。有人能给我建议可能的解决办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-07 04:40:37

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

代码语言:javascript
复制
<asp:TemplateField HeaderText="Approved">
    <ItemTemplate>
        <asp:Label id="LabelApproved" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>

现在,在您的RowCommand事件中,您可以这样做:

代码语言:javascript
复制
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";
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18669703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档