【问题标题】:How do I add multiple hyperlinks to a table cell from a list in the datasource?如何从数据源的列表中向表格单元格添加多个超链接?
【发布时间】:2017-12-25 14:36:51
【问题描述】:

我需要向 radgrid 中的表格单元格添加多个超链接,以便用户可以单击链接将他们重定向到另一个页面。网格的数据源是一个域,其中包含每个客户的链接列表。

 public class CustomerOverviewDomain
{
    public CustomerEntity Entity { get; set; }
    public IEnumerable<LeadDomain> Leads { get; set; }
    public IEnumerable<QualifiedLeadsDomain> QualifiedLeads { get; set; }
    public IEnumerable<ProspectDomain> Prospects { get; set; }
    public List<string> Links
    {
        get
        {
            //NavigateUrl = '<%# "~/Reporting/SalesProposal/ProposalDownload.aspx?proposalId="+Eval("entity.ProposalId") %>' >
            List<string> Links = new List<string>();
            foreach (LeadDomain lead in Leads)
            {
                string link = "~/LeadsManagement/Leads/LeadsDetail.aspx?leadId=" + lead.entity.LeadId;
                Links.Add(link);
            }
            foreach (QualifiedLeadsDomain qlead in QualifiedLeads)
            {
                string link = "!/LeadsManagement/QualifiedLeads/QualifiedLeadDetailPage.aspx?qualifiedLeadId=" + qlead.Entity.QualifiedLeadId;
            }
            foreach (ProspectDomain prospect in Prospects)
            {
                string link = "~/Prospects/ProspectDetailPage.aspx?prospectId=" + prospect.entity.ProspectMasterId;
                Links.Add(link);
            }
            return Links;
        }
    }
}

我不确定 radgrid 中的列应该是什么以及数据应该如何进行数据绑定。

 <%--<telerik:GridBoundColumn HeaderText="Links"
                    DataField="Links" SortExpression="Links" UniqueName="Links" 
                    ShowFilterIcon="false" CurrentFilterFunction="Contains" AutoPostBackOnFilter="false" FilterDelay="500">
                    <HeaderStyle Width="120px" />
                </telerik:GridBoundColumn>--%>

                <telerik:GridHyperLinkColumn DataTextField="Links" DataNavigateUrlFields="Links" UniqueName="Links">
                </telerik:GridHyperLinkColumn>

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

我猜想在数据绑定事件中必须做一些事情,但不确定具体是什么。我被困在这里。我能得到的最多的是一个超链接“System.Collections.Generic.List`1[System.String]”,它没有链接。有人能指出我正确的方向吗?

protected void rgCustomer_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            //Get the row from the grid.
            GridDataItem item = e.Item as GridDataItem;
            if (item != null)
            {
                List<string> links = item["Links"].; 
                //GridTableCell linkCell = (GridTableCell)item["Links"];

                //var Link = item["Links"];
                //if (Link != null)
                //{

                //    TableCell cell = item["Links"];
                //    if (cell != null)
                //    {

                //    }
                //}
            }
        }


       //// 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";
    }

【问题讨论】:

    标签: c# asp.net hyperlink telerik-grid radgrid


    【解决方案1】:

    您基本上需要的是模板列中的嵌套网格。我处理此问题的方法是将 radgrid 嵌套在模板列中,并在父行的 ItemDataBound 事件上,找到嵌套的 radGrid 和 DataKeyName。然后我使用 datakey 值提取当前行的数据并将数据绑定到嵌套的 RadGrid。

    ASPX:

    <telerik:RadGrid ID="RadGrid3" runat="server" OnNeedDataSource="RadGrid3_NeedDataSource" AutoGenerateColumns="false" OnItemDataBound="RadGrid3_ItemDataBound">
                <MasterTableView DataKeyNames="ID">
                    <Columns>
                        <%-- Other columns  --%>
                        <telerik:GridBoundColumn UniqueName="Id" DataField="ID" HeaderText="ID"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Name"></telerik:GridBoundColumn>
                        <%-- Column with nested template --%>
                        <telerik:GridTemplateColumn UniqueName="Links" HeaderText="Links">
                            <ItemTemplate>
                                <telerik:RadGrid ID="RadGrid4" runat="server" Skin="Windows7" RenderMode="Lightweight">
                                    <MasterTableView DataKeyNames="Id" AutoGenerateColumns="False" ShowHeader="false">
                                        <Columns>
                                            <%-- I have included an id and name column for demo purposes. they can be removed so only the link is displayed --%>
                                            <telerik:GridBoundColumn DataField="ID" ReadOnly="True" HeaderText="Id" SortExpression="Id" UniqueName="Id" DataType="System.Int32" FilterControlAltText="Filter Id column"></telerik:GridBoundColumn>
                                            <telerik:GridBoundColumn DataField="Name" ReadOnly="True" HeaderText="Name" SortExpression="Name" UniqueName="Name" DataType="System.Int32" FilterControlAltText="Filter Name column"></telerik:GridBoundColumn>
                                            <telerik:GridHyperLinkColumn DataNavigateUrlFields="Link" DataTextField="Link" HeaderText="Link" SortExpression="Link" UniqueName="Link" FilterControlAltText="Filter Link column"></telerik:GridHyperLinkColumn>
                                        </Columns>
                                    </MasterTableView>
                                </telerik:RadGrid>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
    

    C#:

    protected void RadGrid3_NeedDataSource(object sender, 
    GridNeedDataSourceEventArgs e)
    {
        //Populate parent table with temp data
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name");
        for (int i = 1; i <= 20; i++) {
            table.Rows.Add(i, "Name" + i.ToString());
        }
    
    RadGrid3.DataSource = table;
     }
    
    
    protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)
    {
        //If its a row item
        if (e.Item.ItemType == GridItemType.Item | e.Item.ItemType == GridItemType.AlternatingItem) {
        dynamic item = (GridDataItem)e.Item;
    
        //Find the nested Radgrid in the row
        RadGrid subGrid = (RadGrid)item("Links").FindControl("RadGrid4");
        //Get the current row's datakey value
        int currentRowDataKeyValue = item.GetDataKeyValue("ID");
    
        //Create temp data for nested radgrid
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name");
        table.Columns.Add("Link");
        for (int i = 1; i <= 5; i++) {
            table.Rows.Add(i, "Row " + currentRowDataKeyValue.ToString + ": Link " + i.ToString, "https://www.google.com");
        }
    
        //Set datasource for nested radgrid. This is where you would databind the list of strings from your domain. You will probably need to manipulate the data returned to match the nested grid structure if you want additional columns
        subGrid.DataSource = table;
        subGrid.DataBind();
    
    }
    

    或者,您可以将链接添加到 DetailTemplateColumn。这几乎是相同的解决方案,但它在行下方而不是在模板列中显示嵌套的 RadGrid。

    ASPX:

    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateColumns="false" OnItemDataBound="RadGrid1_ItemDataBound1">
      <MasterTableView DataKeyNames="ID">
        <Columns>
          <telerik:GridBoundColumn UniqueName="Id" DataField="ID" HeaderText="ID"></telerik:GridBoundColumn>
          <telerik:GridBoundColumn UniqueName="Name" DataField="Name" HeaderText="Name"></telerik:GridBoundColumn>
        </Columns>
        <DetailItemTemplate>
          <telerik:RadGrid ID="RadGrid2" runat="server" Skin="Windows7" RenderMode="Lightweight">
            <MasterTableView DataKeyNames="Id" AutoGenerateColumns="False" ShowHeader="false">
              <Columns>
                <telerik:GridBoundColumn DataField="ID" ReadOnly="True" HeaderText="Id" SortExpression="Id" UniqueName="Id" DataType="System.Int32" FilterControlAltText="Filter Id column"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Name" ReadOnly="True" HeaderText="Name" SortExpression="Name" UniqueName="Name" DataType="System.Int32" FilterControlAltText="Filter Name column"></telerik:GridBoundColumn>
                <telerik:GridHyperLinkColumn DataNavigateUrlFields="Link" DataTextField="Link" HeaderText="Link" SortExpression="Link" UniqueName="Link" FilterControlAltText="Filter Link column"></telerik:GridHyperLinkColumn>
              </Columns>
            </MasterTableView>
          </telerik:RadGrid>
        </DetailItemTemplate>
      </MasterTableView>
    </telerik:RadGrid>

    C#

     protected void RadGrid1_NeedDataSource(object sender, 
    GridNeedDataSourceEventArgs e)
    {
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name");
    for (int i = 1; i <= 20; i++) {
        table.Rows.Add(i, "Name" + i.ToString());
    }
    
    RadGrid1.DataSource = table;
    
    }
    
    
    
    protected void RadGrid1_ItemDataBound1(object sender, GridItemEventArgs e)
    {
    if (e.Item.ItemType == GridItemType.Item | e.Item.ItemType == GridItemType.AlternatingItem) {
        dynamic item = (GridDataItem)e.Item;
    
        //Find the grid in the DetailTemplate Cell
        RadGrid subGrid = (RadGrid)item.DetailTemplateItemDataCell.FindControl("RadGrid2");
        // Get the current row datakey value
        int currentRowDataKeyValue = Convert.ToInt32(item.GetDataKeyValue("ID"));
    
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name");
        table.Columns.Add("Link");
        for (int i = 1; i <= 5; i++) {
            table.Rows.Add(i, "Row " + currentRowDataKeyValue.ToString + ": Link " + i.ToString, "https://www.google.com");
        }
    
        subGrid.DataSource = table;
        subGrid.DataBind();
    
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 2016-01-04
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2017-04-21
      相关资源
      最近更新 更多