2017-07-20 5 views
0

Ich muss mehrere Hyperlinks zu der Tabellenzelle in Radgrid hinzufügen, damit Benutzer auf den Link klicken können, um sie auf eine andere Seite umzuleiten. Die Datenquelle des Rasters ist eine Domäne, die eine Liste von Links für jeden Kunden enthält.Wie füge ich mehrere Hyperlinks zu einer Tabellenzelle aus einer Liste in der Datenquelle hinzu?

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

Ich bin nicht sicher, was die Spalte in der RadGrid sein sollte und wie die Daten datengebundene werden.

<%--<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>--%> 

Ich vermute, etwas muss in der datengebundenen Ereignis getan werden, aber nicht sicher, was genau. Ich stecke hier fest. Das meiste, was ich bekommen konnte, ist ein Hyperlink "System.Collections.Generic.List`1 [System.String]", der nirgends verlinkt. Könnte mir jemand in die richtige Richtung zeigen?

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

Antwort

0

Was Sie im Grunde brauchen, ist ein verschachteltes Grid in einer Template-Spalte. Die Art, wie ich damit umgegangen bin, besteht darin, ein Radgrid in eine Template-Spalte zu verschachteln und auf dem ItemDataBound -Ereignis der Elternzeile das verschachtelte radGrid und das DataKeyName zu finden. Ich verwende dann den datakey-Wert, um die Daten für die aktuelle Zeile zu ziehen und die Daten an das verschachtelte RadGrid zu binden.

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(); 

} 

Alternativ können Sie auch die Links zu den DetailTemplateColumn hinzuzufügen. Dies ist praktisch die gleiche Lösung, aber es zeigt das verschachtelte RadGrid unterhalb der Zeile anstatt in einer Vorlagenspalte an.

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(); 

} 

} 
Verwandte Themen