2011-12-20 79 views
0

我想要獲取根網站下的所有網站集並使用SPGridView顯示它們。在SPGridView中,我使用HyperlinkField顯示名稱,網址併爲每個網站創建。 我的問題是我無法將該網站的URL附加到HyperlinkField。任何想法如何可以在代碼中完成?如何在SPGridView中設置超鏈接字段的navigationUrl

[ToolboxItemAttribute(false)] 
public class SPGridViewDemo : WebPart 
{ 
    private SPGridView grid; 
    private ObjectDataSource gridDS; 

    public DataTable SelectData() 
    { 
     DataTable dataSource = new DataTable(); 

     dataSource.Columns.Add("Name"); 
     dataSource.Columns.Add("Created"); 
     dataSource.Columns.Add("Url"); 

     SPWebApplication webApplication = SPContext.Current.Site.WebApplication; 
     SPSiteCollection siteCollections = webApplication.Sites; 
     SPSite currentSite = SPContext.Current.Site; 

     foreach (SPSite siteCollection in siteCollections) 
      { 
       dataSource.Rows.Add(siteCollection.RootWeb.Title, siteCollection.RootWeb.Created, siteCollection.RootWeb.Url); 
      } 

     return dataSource; 
    } 


    protected sealed override void Render(HtmlTextWriter writer) 
    { 
     BoundField column = new BoundField(); 
     HyperLinkField hyplink = new HyperLinkField(); 

     hyplink.DataTextField = "Name"; 
     hyplink.NavigateUrl = "Url"; 
     hyplink.SortExpression = "Name"; 
     hyplink.HeaderText = "Workspaces"; 
     grid.Columns.Add(hyplink); 

     column = new BoundField(); 
     column.DataField = "Created"; 
     column.SortExpression = "Created"; 
     column.HeaderText = "Created"; 
     grid.Columns.Add(column); 

     column = new BoundField(); 
     column.DataField = "Url"; 
     column.SortExpression = "Url"; 
     column.HeaderText = "Url"; 
     grid.Columns.Add(column); 

     grid.Sort("Name",System.Web.UI.WebControls.SortDirection.Descending); 
     grid.DataBind(); 

     base.Render(writer); 
    } 

    protected sealed override void CreateChildControls() 
    { 
     const string gridId = "grid"; 
     const string dataSourceId = "gridDS"; 

     gridDS = new ObjectDataSource(); 
     gridDS.ID = dataSourceId; 
     gridDS.SelectMethod = "SelectData"; 
     gridDS.TypeName = this.GetType().AssemblyQualifiedName; 
     gridDS.ObjectCreating += new ObjectDataSourceObjectEventHandler(gridDS_ObjectCreating); 
     this.Controls.Add(gridDS); 

     grid = new SPGridView(); 
     grid.ID = gridId; 
     grid.DataSourceID = gridDS.ID; 
     grid.AutoGenerateColumns = false; 

     // Paging 
     grid.AllowPaging = true; 
     grid.PageSize = 5; 

     // Sorting 
     grid.AllowSorting = true; 

     this.Controls.Add(grid); 

     SPGridViewPager pager = new SPGridViewPager(); 
     pager.GridViewId = grid.ID; 

     this.Controls.Add(pager); 
    } 

    private void gridDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e) 
    { 
     e.ObjectInstance = this; 
    } 
+0

您的代碼缺少關鍵信息:Url來自哪裏?你是否收到錯誤?按照你的方式,'.NavigateUrl'只是空的?當您將'.NavigateUrl'設置爲像google.com這樣的隨機URL時會發生什麼? – 2011-12-20 11:35:12

+0

我添加了上面的代碼。 – 2011-12-20 11:45:25

回答

0

您應該使用HyperLinkField.DataNavigateUrlFields Property代替:

HyperLinkField hyplink = new HyperLinkField(); 

hyplink.DataTextField = "Name"; 
hyplink.DataNavigateUrlFields = new string[] { "Url" }; 
hyplink.SortExpression = "Name"; 
hyplink.HeaderText = "Workspaces"; 
grid.Columns.Add(hyplink); 

HyperLinkField.NavigateUrl Property用於呈現靜態URL:

使用NavigateUrl屬性來指定網址導航到當一個超鏈接在HyperLinkField對象中單擊。設置此屬性時,每個超鏈接共享相同的導航URL。

+0

嗨Stefan。感謝您的回覆。你說的是對的。我用它是這樣的:DataNavigateUrlFields = new [] {「Url」}, DataNavigateUrlFormatString =「{0}」, – 2011-12-20 13:29:07

+0

根據文檔'DataNavigateUrlFormatString'對於基本顯示是不需要的。 – Stefan 2011-12-20 14:18:02

相關問題