如何将一个超链接添加到动态的GridView列 [英] How to add a Hyperlink to a dynamic gridview column

查看:165
本文介绍了如何将一个超链接添加到动态的GridView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题希望有人可以提供帮助。

我有一个动态的的GridView 。我需要有一个超级链接在GridView的列。这些超级链接应该打开一个弹出窗口上单击显示某些数据。

我通过具有动态模板现场试过这个。但是,即使在结合数据,我无法获得超链接的列。我能够获取数据,但不是超链接。

这是正在实施 HyperLinkTemplate 了Itemplate

 公共类HyperLinkTemplate:了Itemplate
{
    私人字符串m_ColumnName;
    公共字符串的ColumnName
    {
        {返回m_ColumnName; }
        集合{m_ColumnName =价值; }
    }    公共HyperLinkTemplate()
    {
        //
        // TODO:添加构造逻辑这里
        //
    }
    公共HyperLinkTemplate(字符串的ColumnName)
    {
        this.ColumnName =的ColumnName;    }    公共无效InstantiateIn(System.Web.UI.Control ThisColumn)
    {
        超链接HyperLinkItem =新的超链接();
        HyperLinkItem.ID =HL+的ColumnName;
        HyperLinkItem.DataBinding + = HyperLinkItem_DataBinding;
        ThisColumn.Controls.Add(HyperLinkItem);    }    私人无效HyperLinkItem_DataBinding(对象发件人,EventArgs的发送)
    {
        超链接HyperLinkItem =(超链接)发送;
        GridViewRow CurrentRow =(GridViewRow)HyperLinkItem.NamingContainer;
        反对CurrentDataItem =的DataBinder.Eval(CurrentRow.DataItem,的ColumnName);
        HyperLinkItem.Text = CurrentDataItem.ToString();
    }
}


解决方案

我不完全肯定,我明白你试图完成什么,但我不认为你应该建立自己的模板类这一点。

您可能意味着比我用这个词思考动态GridView的其他东西,但如果你需要一个超链接添加到一个GridView列的每一行,如果你需要这样做在code-身后,那么我会建议处理GridView的RowDataBound事件,并做类似的事件处理程序如下:

 保护无效grdData_RowDataBound(对象发件人,GridViewRowEventArgs E)
    {
        如果(e.Row.RowType == DataControlRowType.DataRow)
        {
            超链接链接=新的超链接();
            link.Text =这是一个链接!
            link.NavigateUrl =导航基于数据的话:+ e.Row.DataItem;
            e.Row.Cells [ColumnIndex.Column1] .Controls.Add(链接);
        }
    }

I have an issue hope someone can help.

I have a dynamic Gridview. I need to have a hyperlink on gridview column. These hyperlink should open a popup to display certain data on clicking.

I tried this by having a dynamic template field . But even on binding the data , I'm unable to get the hyper link for the column. I'm able to get the data but not the hyperlink.

This is the HyperLinkTemplate class which is implementing ITemplate.

public class HyperLinkTemplate : ITemplate
{
    private string m_ColumnName;
    public string ColumnName
    {
        get { return m_ColumnName; }
        set { m_ColumnName = value; }
    }

    public HyperLinkTemplate()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public HyperLinkTemplate(string ColumnName)
    {
        this.ColumnName = ColumnName;

    }

    public void InstantiateIn(System.Web.UI.Control ThisColumn)
    {
        HyperLink HyperLinkItem = new HyperLink();
        HyperLinkItem.ID = "hl" + ColumnName;
        HyperLinkItem.DataBinding += HyperLinkItem_DataBinding;
        ThisColumn.Controls.Add(HyperLinkItem);

    }

    private void HyperLinkItem_DataBinding(object sender, EventArgs e)
    {
        HyperLink HyperLinkItem = (HyperLink)sender;
        GridViewRow CurrentRow = (GridViewRow)HyperLinkItem.NamingContainer;
        object CurrentDataItem = DataBinder.Eval(CurrentRow.DataItem, ColumnName);
        HyperLinkItem.Text = CurrentDataItem.ToString();
    }
} 

解决方案

I'm not entirely sure that I understand what you are trying to accomplish, but I don't think that you should have to build your own template class for this.

You might mean something other than what I'm thinking by the term "dynamic gridview", but if you need to add a hyperlink to each row of a column in a GridView, and if you need to do this in the code-behind, then I would suggest handling the GridView's RowDataBound event and doing something like the following in the event handler:

    protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink link = new HyperLink();
            link.Text = "This is a link!";
            link.NavigateUrl = "Navigate somewhere based on data: " + e.Row.DataItem;
            e.Row.Cells[ColumnIndex.Column1].Controls.Add(link);
        }
    }

这篇关于如何将一个超链接添加到动态的GridView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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