如何在GridView Row DataBound命令中启用/禁用超链接 [英] How to enable/disable Hyperlink in GridView Row DataBound Command

查看:89
本文介绍了如何在GridView Row DataBound命令中启用/禁用超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 GridView RowDataBound 超链接 c>根据指定条件进行命令。我从另一个公开声明变量的函数传递url。我的代码如下,但是这会产生如下错误:

I want to Enable/Disable Hyperlink at GridView RowDataBound Command according to specify condition. I am Passing the url from another function that is publicly declared variable. My code is as follows but this gives an error like :

"Object reference not set to instance of object"




protected void grd_myActivity_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        object row_items = e.Row.DataItem;
        object items = e.Row.DataItem;
        HyperLink href = (HyperLink)e.Row.FindControl("href_link");
        string url = Convert.ToString(DataBinder.Eval(row_items, "Url"));
        
        if (url == "")
        {
            href.Enabled = false;      
        }
        else
        {
            href.Enabled = true;      
        }
    }
}





谢谢

Sunil Sharma



Thanks
Sunil Sharma

推荐答案

你的方法应该像下一个:

Your method should be like next one:
protected void grd_myActivity_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex < 0) // This check is important!
        return; // 
    //
    DataRow dataRow = ((DataRowView)e.Row.DataItem).Row;
    HyperLink href = (HyperLink)dataRow.FindControl("href_link");
    href.Enabled = href.NavigateUrl != ""; 
}


可能是超链接的id不同检查href_link这个超链接是否在网格中给出,而且必须是asp控件..代码是正确的没有问题
May be id of hyperlink is different check href_link this hyperlink is given in the grid or not, and it must be asp control .. code is correct no issue


这篇关于如何在GridView Row DataBound命令中启用/禁用超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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