Gridview:根据同一行中单元格的值更改链接按钮文本

时间:2014-03-28 16:07:28

标签: c# asp.net gridview

以下是我想要的网格视图, ID,名称状态是标签,操作是LinkBut​​ton

ID -------------姓名-------------状态-------------行动

1 -------------- Name1 ------------ Active ------------禁用

2 -------------- Name2 ------------ In-active ---------启用

3 -------------- Name3 ------------ Active ------------禁用

如何将 LinkBut​​ton 文字设置为“停用”或“启用”取决于状态的值(文字) (始终有效无效)?

我的链接按钮如下所示,如何将文字“你想继续”更改为“你想禁用”或“你想要吗? 启用'基于上面的逻辑

<asp:LinkButton ID="lbtAction" runat="server"
               CommandArgument='<%# Eval("ID")%>'
               OnClientClick="return confirm('Do you want to proceed?')"
               OnClick="DoAction"></asp:LinkButton>

2 个答案:

答案 0 :(得分:3)

使用RowDataBound并将您的逻辑放在那里:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton lbtAction = (LinkButton) e.Row.FindControl("lbtAction");
        Label lblStatus = (Label) e.Row.FindControl("lblStatus");
        bool active = lblStatus.Text == "Active";
        lbtAction.Text = active ? "Disable" : "Enable";
        string onClientClick  = string.Format("return confirm('Do you want to {0}?')",
                                               active ? "Disable" : "Enable");
        lbtAction.OnClientClick = onClientClick;
    }
}

答案 1 :(得分:1)

使用Gridview RowDataBound事件来实现相同的目标。如下所示(不是确切的代码,但你可以一样开始)

void RowDataBound(object sender, GridViewRowEventArgs e)
{
 if(e.Row.RowType == DataControlRowType.DataRow)
    {
        if(((Label)e.Row.FindControl("Status")).Text == "Active")
{
  //disable
}
else
{
//enable
}
}
}