How to add a hyperlink to a dynamic gridview column

I have a problem someone can help with.

I have a dynamic one Gridview. I need to have a column hyperlinkin gridview. This hyperlink should open a popup window to display certain data when clicked.

I tried this with a dynamic template field. But even with data binding, I cannot get the hyperlink for the column. I can get data, but not a hyperlink.

This is the class HyperLinkTemplatethat implements 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();
    }
} 
+3
source share
1 answer

I'm not quite sure that I understand what you are trying to accomplish, but I do not think that you will need to create your own class of templates for this.

, , " gridview", GridView, -behind, GridView RowDataBound - :

    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);
        }
    }
+9

Source: https://habr.com/ru/post/1724279/


All Articles