如何根据gridview值启用按钮? [英] How to enable button based on gridview value?
本文介绍了如何根据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
根据你的您想要根据网格的状态显示或隐藏按钮的问题。
所以它可以在GridView1_RowDataBound事件中完成。
参考这段代码它可以帮到你。
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells [Index] .Text; //这里索引你的状态显示
按钮btn =(按钮) GridView1.Columns [Index]; //这里索引你的按钮存在
if(status ==Open)
{btn.Visible = true; }
其他
{btn.Visible = false; }
}
}
As per your question you want to show or hide your button as per the Status of the grid.
So it can be done in GridView1_RowDataBound event.
Refer this code it may help you.
protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells[Index].Text;// Here Index where your status shows
Button btn = (Button)GridView1.Columns[Index];// Here Index where your button exist
if (status == "Open")
{ btn.Visible = true; }
else
{ btn.Visible = false; }
}
}
这篇关于如何根据gridview值启用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文