如何从数据源中的列表向表格单元格添加多个超链接?

时间:2017-07-20 04:59:53

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

我需要在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";
    }

1 个答案:

答案 0 :(得分:0)

您基本需要的是模板列中的嵌套网格。我处理这个问题的方法是将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();

}

}