2013-07-24 97 views
-5

我需要在gridview中啓用或禁用按鈕。基本上每行都有這個按鈕,點擊這個按鈕後,它會在其他頁面上重定向到相應的行ID。我需要禁用某些特定用戶的按鈕。我將從會話中獲取用戶名。在gridview中啓用禁用按鈕

+2

你嘗試這樣做的努力是什麼?你有什麼嘗試?你甚至試過Google嗎? –

+0

您可以爲RowDataBound事件創建一個處理程序,並在那裏操作按鈕。 –

回答

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; 
    } 
} 
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; 
    } 
} 
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; 
} 
}