GridVIew中有一模板列,添加了一个Button按钮,需要根据令一列中的值设置按钮列中对应Button的Enabled状态,请问怎样做?
DataTable table = em.GetExam();
GridView1.DataSource = table;
GridView1.DataBind();
for (int i = 0; i < table.Rows.Count; i++)
{
if (table.Rows[i][5].ToString() == "已考" || table.Rows[i][5].ToString() == "考试中")
{
Button btn = (Button)GridView1.Rows[i].FindControl("button");
btn.Enabled = false;
}
}
这是我写的方法,绑定后根据Datatable中第六列的字符串,设置Gridview对应行中的Button,但是没有效果,很急啊,请大家帮帮忙吧
DataTable table = em.GetExam();
GridView1.DataSource = table;
GridView1.DataBind();
for (int i = 0; i < table.Rows.Count; i++)
{
if (table.Rows[i][5].ToString() == "已考" || table.Rows[i][5].ToString() == "考试中")
{
Button btn = (Button)GridView1.Rows[i].FindControl("button");
btn.Enabled = false;
}
}
这是我写的方法,绑定后根据Datatable中第六列的字符串,设置Gridview对应行中的Button,但是没有效果,很急啊,请大家帮帮忙吧
放到GridView的RowDataBind事件中设置!!
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string state= e.Row.Cells[列号].Text.Trim();
if (state== "已考" || state== "考试中")
{
(e.Row.Cells[行号].FindControl("button") as Button).Enabled = false;
} }
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string state= e.Row.Cells[列号].Text.Trim();
if (state== "已考" || state== "考试中")
{
(e.Row.Cells[行号].FindControl("button") as Button).Enabled = false;
} }
}