启用和禁用gridview的链接按钮 [英] Enable and disable link button on gridview

查看:87
本文介绍了启用和禁用gridview的链接按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要启用或基于条件的GridView的某些行禁用LinkBut​​ton的..我可以一行启用的LinkBut​​ton和禁用它?我的code相同的网格视图的另一行此处

 保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{LinkBut​​ton的lnk2 =(LinkBut​​ton的)e.Row.FindControl(LinkBut​​ton2);
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {        的SqlCommand CMD12 =新的SqlCommand(选择student_vs_testsession_details testsession_status那里testsession_id ='+ v_testid.Text +',con12);
        SqlDataReader的DR12 = cmd12.ExecuteReader();
        而(dr12.Read())
        {
            串test_status = DR12 [0]的ToString();
            LinkBut​​ton的lnk2 =(LinkBut​​ton的)e.Row.FindControl(LinkBut​​ton2);
            的foreach(在GridView1.Rows GridViewRow行)
            {
                如果(v_testtype ==理论测试&放大器;&安培; test_status ==已完成)
                {
                    lnk2.Visible = TRUE;
                }
                其他
                {
                    lnk2.Visible = FALSE;
                }            }
        }


解决方案

是的,你可以很容易地做到这一点在RowDataBound事件,但你在使用 lnk2.Visible 您的财产code。

您可以使用另一个要求可见财产,但只是想你确认它是用来显示/隐藏唯一的LinkBut​​ton的。要启用/ disble一个LinkBut​​ton,使用已启用的LinkBut​​ton的财产。如:

  lnk2.Enabled = TRUE; //使LinkBut​​ton的。
lnk2.Enabled = FALSE; //禁用的LinkBut​​ton。

如果你想使用的rowIndex做到这一点,那么你就可以 e.Row.RowIndex 来找到的GridView的RowDatabound`事件中当前行的索引。如:

 如果(e.Row.RowIndex == 2)
{
  LinkBut​​ton的lnk2 =(LinkBut​​ton的)e.Row.FindControl(LinkBut​​ton2);
  lnk2.Enabled = FALSE;
}

如果您要根据同一行的一些列的值,以启用/禁用LinkBut​​ton的,那么你可以做的RowDataBound 事件中一样。如:

 字符串Namecolumnvalue = Convert.ToString(DataBinder.Eval的(e.Row.DataItem,姓名));
LinkBut​​ton的lnk2 =(LinkBut​​ton的)e.Row.FindControl(LinkBut​​ton2);
如果(Namecolumnvalue ==禁止)
{
  lnk2.Enabled = FALSE;
}
其他{
  lnk2.Enabled = TRUE;
}

I wants to enable or disable linkbutton on some rows of gridview based on condition.. Can i enable linkbutton on one row and disable it on another row of same grid view ??my code is here

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        SqlCommand cmd12 = new SqlCommand("Select testsession_status from student_vs_testsession_details where  testsession_id='" + v_testid.Text + "' ", con12);
        SqlDataReader dr12 = cmd12.ExecuteReader();
        while (dr12.Read())
        {
            string test_status = dr12[0].ToString();
            LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
            foreach (GridViewRow row in GridView1.Rows)
            {
                if (v_testtype == "Theory Test" && test_status == "Completed")
                {
                    lnk2.Visible = true;
                }
                else
                {
                    lnk2.Visible = false;
                }

            }




        }

解决方案

Yes you can easily do it in RowdataBound Event, but you have used lnk2.Visible property in your code.

you may be using Visible property for another requirement but just want to confirm you that it is used to show/hide the Linkbutton only. To enable/disble a Linkbutton, use Enabled property of Linkbutton. as:

lnk2.Enabled = true;// to enable linkbutton.
lnk2.Enabled = false;// to disable linkbutton.

If You want to do it using rowindex, then you can e.Row.RowIndex to find the current row index inside 'RowDatabound` event of gridview. as:

if(e.Row.RowIndex==2)
{
  LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
  lnk2.Enabled=false;
}

If you want to enable/ disable Linkbutton based on value of some other column in the same row, then you can do the same inside Rowdatabound event. as:

string Namecolumnvalue = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));
LinkButton lnk2 = (LinkButton)e.Row.FindControl("LinkButton2");
if(Namecolumnvalue =="Disable")
{      
  lnk2.Enabled=false;
}
else{
  lnk2.Enabled=true;
}

这篇关于启用和禁用gridview的链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆