在gridview里查找模板里的button控件

这个问题,真是搞了我1天,这次记住他

第一种方法:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
    Button btn1 = drv.FindControl("button3") as Button;
    btn1.Text = "此订单不可取消";
    btn1.Enabled = false;
}

 

第二种方法:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //DataRowView drv = e.Row.DataItem as DataRowView;
    //System.Web.UI.WebControls.Button btn1 = e.Row.FindControl("button3") as System.Web.UI.WebControls.Button;
    //foreach (GridViewRow gvr in GridView1.Rows)
    //{
    // Button btn1 = GridView1.FindControl("button3") as Button;
    // btn1.Text = "此订单已经取消";
    // btn1.Enabled = false;
    //}
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        if(e.Row.FindControl("Button3") != null)
        {
            Button btn1 = (Button) e.Row.FindControl("Button3");
            btn1.Text = "此订单已经取消";
            btn1.Enabled = false;
        }
    }
}

 

posted @ 2018-03-23 09:14  ProZkb  阅读(224)  评论(0编辑  收藏  举报