如何在GridView Row DataBound命令中启用/禁用超链接

问题描述:

我想在 GridView RowDataBound $ c $启用/禁用超链接 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