您好我想在网格视图中禁用具有特殊值条件的链接底部控件。例如,如果行的计数变为0,则该行的链接底部应该是不可见的。我该怎么办?我应该在哪里写代码? 这是我在行命令网格视图中写的鳕鱼,但它仅适用于推送链接底部!!但我想在加载前将此鳕鱼应用于我的页面。 请指导我
int idx = Convert.ToInt32(e.CommandArgument);
idx = idx - (GridView1.PageSize * GridView1.PageIndex);
int ID = (int)GridView1.DataKeys[idx].Value;
string connStr = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(connStr);
SqlCommand sqlcmd = new SqlCommand();
sqlcmd = new SqlCommand("SELECT count(ID) FROM ReviwerArticle where ArticleID=@ArticleID", sqlconn);
sqlcmd.Parameters.AddWithValue("@ArticleID", ID);
sqlconn.Open();
int count = ((int)sqlcmd.ExecuteScalar());
sqlconn.Close();
if (count == 0)
{
((LinkButton)GridView1.Rows[idx].Cells[0].FindControl("LinkButton4") as LinkButton).Visible = false;
}
答案 0 :(得分:0)
然后不要尝试onrowcommand
,因为这只会在执行某个操作时触发。如果您在代码隐藏中使用函数来加载数据,那么检查有多少行被推送到gridview。如果行为零,则禁用该按钮。如果行大于0则启用按钮。
答案 1 :(得分:0)
在GridView的RowDataBound事件中执行..
if (e.Row.RowType == DataControlRowType.DataRow)
{
int ID = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value;
string connStr = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(connStr);
SqlCommand sqlcmd = new SqlCommand();
sqlcmd = new SqlCommand("SELECT count(ID) FROM ReviwerArticle where ArticleID=@ArticleID", sqlconn);
sqlcmd.Parameters.AddWithValue("@ArticleID", ID);
sqlconn.Open();
int count = ((int)sqlcmd.ExecuteScalar());
sqlconn.Close();
if (count == 0)
{
((LinkButton)e.Row.Cells[0].FindControl("LinkButton4") as LinkButton).Visible = false;
}
}