在gridview中启用\禁用按钮

时间:2013-07-24 08:16:01

标签: c# asp.net

我需要在gridview中启用或禁用按钮。基本上每行都有这个按钮,点击这个按钮,它会在具有相应行ID的其他页面上重定向。我需要某个特定用户的禁用按钮。我将从会话中获取用户名。

3 个答案:

答案 0 :(得分:1)

您可以在RowDataBound事件中找到按钮并根据条件将其禁用

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{               
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button buttonId= (Button )e.Row.FindControl("buttonId");
        if(Session["Role"] == "admin")
            buttonId.Enabled = false;
    }
}

答案 1 :(得分:1)

试试这个:

protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType != DataControlRowType.DataRow)
    {
        return;
    }

    Button button = (Button)e.Row.FindControl("btnSubmit");

    int Id = (int) ((DataRowView) e.Row.DataItem)["Id"];

    if(Id == Convert.ToInt32(Session["Id"]))
    {
        button.Enabled = false;
    }
    else
    {
        button.Enabled = true;
    }
}

答案 2 :(得分:0)

使用

protected void GridView1_DataBound(object sender, EventArgs e)
{

for (int i = 0; i < GridView1.Rows.Count; i++)
{

    if (GridView1.Rows[i].Cells[1].Text == "NA")
    {
      Button btnVal= (Button)e.Row.FindControl("buttonSubmit");
      btnVal.Enabled = false;

    }
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{               
if (e.Row.RowType == DataControlRowType.DataRow)
{
    Button btnVal= (Button )e.Row.FindControl("buttonSubmit");
    if(Session["Role"] == "admin")
        btnVal.Enabled = false;
}
}