使用 AutoGenerateColumns 将超链接绑定到 GridView

发布于 2024-10-12 22:10:15 字数 272 浏览 4 评论 0原文

我有一个数据表,其中包含在运行时动态生成的列。此 DataTable 绑定到 AutoGenerateColumns 设置为 true 的 GridView。我遇到了一个问题,因为 DataTable 中的某些数据是 HyperLink 对象,因此它不会显示表中的实际链接,而是显示“System.Web.UI.WebControls.HyperLink”。

通常,我只会在 GridView 中使用 HyperLinkField,但由于 GridView 的列是自动生成的,我不确定如何执行此操作。有什么想法吗?

I have a DataTable which has columns that are generated dynamically at runtime. This DataTable is bound to a GridView that has AutoGenerateColumns set to true. I've run into a problem with this since some of the data in the DataTable are HyperLink objects, so instead of displaying the actual link in the table, it displays "System.Web.UI.WebControls.HyperLink".

Normally, I would just use a HyperLinkField in my GridView, but since the GridView's columns are generated automatically, I'm not sure how to do this. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-10-19 22:10:15

您可以动态添加新列,您只需要隐藏自动生成的列即可。

对于此解决方案,您可以将超链接存储在 2 列中 - 1 列用于链接,1 列用于您想要显示的文本,或者如果您想要显示通用内容(例如“单击此处”),您可以只存储链接(例如“http ://example.com.au/Default.aspx”)。

protected void GridView1_RowBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        //add new header
        TableCell tc = new TableCell();
        tc.Text = "Groovy Link";
        e.Row.Cells.Add(tc);
        //hide original column that has been autobound - skip column we just added
        for (int i = 0; i < e.Row.Cells.Count - 1; i++)
        {
            BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
            if (field.DataField == "AutoGeneratedColumnName")
                field.Visible = false;
        }
    }
    else if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //create new tablecell
        TableCell tc = new TableCell();
        //do a check to see if the data is stored as a hyperlink in DB
        if (DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString().StartsWith("<a") == true)
        {
            //create hyperlink
            HyperLink hyp = new HyperLink();
            hyp.NavigateUrl = DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString();
            hyp.Text = "click here";
            tc.Controls.Add(hyp);
        }
        else
        {
            //just text
            tc.Text = DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString()
        }
        //add tablecell to row
        e.Row.Cells.Add(tc);
        //hide original column that has been autobound
        for (int i = 0; i < e.Row.Cells.Count - 1; i++)
        {
            BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
            if (field.DataField == "AutoGeneratedColumnName")
                field.Visible = false;
        }
    }
}

You can add a new column dynamically, you'll just have to hide the automatically generated columns also.

For this solution you can either store the hyperlink in 2 columns - 1 for the link and 1 for the text you want displayed, or if you want something generic displayed (like 'Click Here') you can just store the link (eg "http://example.com.au/Default.aspx").

protected void GridView1_RowBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        //add new header
        TableCell tc = new TableCell();
        tc.Text = "Groovy Link";
        e.Row.Cells.Add(tc);
        //hide original column that has been autobound - skip column we just added
        for (int i = 0; i < e.Row.Cells.Count - 1; i++)
        {
            BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
            if (field.DataField == "AutoGeneratedColumnName")
                field.Visible = false;
        }
    }
    else if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //create new tablecell
        TableCell tc = new TableCell();
        //do a check to see if the data is stored as a hyperlink in DB
        if (DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString().StartsWith("<a") == true)
        {
            //create hyperlink
            HyperLink hyp = new HyperLink();
            hyp.NavigateUrl = DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString();
            hyp.Text = "click here";
            tc.Controls.Add(hyp);
        }
        else
        {
            //just text
            tc.Text = DataBinder.Eval(e.Row.DataItem, "AutoGeneratedColumnName").ToString()
        }
        //add tablecell to row
        e.Row.Cells.Add(tc);
        //hide original column that has been autobound
        for (int i = 0; i < e.Row.Cells.Count - 1; i++)
        {
            BoundField field = (BoundField)((DataControlFieldCell)e.Row.Cells[i]).ContainingField;
            if (field.DataField == "AutoGeneratedColumnName")
                field.Visible = false;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文