RadGrid 列中的可点击链接

发布于 2024-08-28 06:13:39 字数 376 浏览 8 评论 0原文

我有一个 RadGrid,其中网格中的一列包含一个 URL。当在列中输入值时,我可以看到 URL,但该 URL 不可单击(以转到该 URL)。如何使 URL 可点击?

这是我现在正在做的一个粗略示例:

DataTable table = new DataTable();
DataRow row = table.Rows[0];
row["URL"] = "http://www.google.com";
grid.DataSource = table;

此外,我真的很想显示特定的文本而不是 URL。类似于 HTML 中的 Link。有办法做到这一点吗?

I have a RadGrid where a column in the grid holds a URL. When a put a value in the column I can see the URL but the URL is not clickable (to go to the URL). How can I make the URL clickable?

Here is a rough example of what I'm doing now:

DataTable table = new DataTable();
DataRow row = table.Rows[0];
row["URL"] = "http://www.google.com";
grid.DataSource = table;

In addition I'd really like to show specific text instead of the URL. Something similar to <a href="http://www.google.com">Link</a> in HTML. Is there anyway to do this?

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

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

发布评论

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

评论(3

眉黛浅 2024-09-04 06:13:39

您尝试过 GridHyperLinkColumn 吗?下面是一个详细的例子。

<telerik:GridHyperLinkColumn FooterText="HyperLinkColumn footer" DataTextFormatString="Search Google for '{0}'" DataNavigateUrlFields="CompanyName" UniqueName="CompanyName" DataNavigateUrlFormatString="http://www.google.com/search?hl=en&q={0}&btnG=Google+Search" HeaderText="HyperLink<br/>Column" DataTextField="CompanyName"></telerik:GridHyperLinkColumn>

您还可以查看演示网站以了解其工作原理。
http://demos.telerik.com/aspnet-ajax /grid/examples/generalfeatures/columntypes/defaultcs.aspx

Have you tried the GridHyperLinkColumn? Below is a detailed example.

<telerik:GridHyperLinkColumn FooterText="HyperLinkColumn footer" DataTextFormatString="Search Google for '{0}'" DataNavigateUrlFields="CompanyName" UniqueName="CompanyName" DataNavigateUrlFormatString="http://www.google.com/search?hl=en&q={0}&btnG=Google+Search" HeaderText="HyperLink<br/>Column" DataTextField="CompanyName"></telerik:GridHyperLinkColumn>

You can also view the demosite to see how it works.
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx

清眉祭 2024-09-04 06:13:39

在 ascx 页面中手动添加所有列,并将要包含超链接的列设为 GridTemplateColumn:

<telerik:GridTemplateColumn 
    UniqueName="TemplateLinkColumn" 
    AllowFiltering="false" 
    HeaderText="URL">
    <ItemTemplate>
        <asp:HyperLink ID="Link" runat="server"></asp:HyperLink>
    </ItemTemplate>
</telerik:GridTemplateColumn>

确保网格具有 OnItemDataBound 方法:

<telerik:RadGrid 
    ID="RadGrid" 
    runat="server" 
    AutoGenerateColumns="False" 
    OnItemDataBound="RadGrid_ItemDataBound" >

在 OnItemDataBound 方法中将字段设置为 URL:

protected void RadGrid_ItemDataBound(object aSender, GridItemEventArgs anEventArgs)
{
    //Get the row from the grid.
    GridDataItem item = anEventArgs.Item as GridDataItem;
    GridTableCell linkCell = (GridTableCell)item["TemplateLinkColumn"];
    HyperLink reportLink = (HyperLink)reportLinkCell.FindControl("Link");

    // Set the text to the quote number
    reportLink.Text = "Google";

    //Set the URL
    reportLink.NavigateUrl = "http://www.google.com";

    //Tell it to open in a new window
    reportLink.Target = "_new";
}

Add all the columns manually in the ascx page and make the column you want to contain the hyperlink a GridTemplateColumn:

<telerik:GridTemplateColumn 
    UniqueName="TemplateLinkColumn" 
    AllowFiltering="false" 
    HeaderText="URL">
    <ItemTemplate>
        <asp:HyperLink ID="Link" runat="server"></asp:HyperLink>
    </ItemTemplate>
</telerik:GridTemplateColumn>

Make sure that your grid has an OnItemDataBound method:

<telerik:RadGrid 
    ID="RadGrid" 
    runat="server" 
    AutoGenerateColumns="False" 
    OnItemDataBound="RadGrid_ItemDataBound" >

In your OnItemDataBound method set the field to the URL:

protected void RadGrid_ItemDataBound(object aSender, GridItemEventArgs anEventArgs)
{
    //Get the row from the grid.
    GridDataItem item = anEventArgs.Item as GridDataItem;
    GridTableCell linkCell = (GridTableCell)item["TemplateLinkColumn"];
    HyperLink reportLink = (HyperLink)reportLinkCell.FindControl("Link");

    // Set the text to the quote number
    reportLink.Text = "Google";

    //Set the URL
    reportLink.NavigateUrl = "http://www.google.com";

    //Tell it to open in a new window
    reportLink.Target = "_new";
}
み青杉依旧 2024-09-04 06:13:39

您还需要检查类型是否正确,如下所示;

if (anEventArgs.Item.GetType().Name != "GridDataItem")
{
    return;
}

You will also need to check for the correct type, as follows;

if (anEventArgs.Item.GetType().Name != "GridDataItem")
{
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文