如何根据gridview值启用按钮

如何根据gridview值启用按钮

本文介绍了如何根据gridview值启用按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a column in SQL:

Status
open
Close

and Gridview with Boundfield value='Status'

When a user selects a row and the Status == open then it should display a button. Otherwise ist hiden.





我尝试过:





What I have tried:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {


        string y = Data.Rows[GridView1.SelectedIndex][5].ToString();


        if (y == "open")
        {
           btnAccept.Visible = true;
        }
        else
        {
            btnAccept.Visible = false;
        }
    }

推荐答案

 //  I think you should hide show the button on gridview_rowdatabound event like this.

       //look this is just the pseudo code
        //put the button you want to show/hide in the template field of gridview and try to find the control in the below event [OnRowDataBound]
        protected void gridview1_rowdatabound(object sender, GridViewRowEventArgs e)
        {
            //find the button control
            Button btnStatus = (Button)Gridview1.FindControl("yourControlName");
            foreach (GridViewRow gvrow in Gridview1.Rows)
            {
                // now read the status column value and enable/disable the button
                string status = gvrow.Column[yourColumnIndexStartingFrom 0].Text;
                if (status == "Open")
                { btnStatus.Visible = true; }
                else
                { btnStatus.Visible = false; }

            }
        }
// Let me know if it helps or not



这篇关于如何根据gridview值启用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 02:25