我想要獲取根網站下的所有網站集並使用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;
}
您的代碼缺少關鍵信息:Url來自哪裏?你是否收到錯誤?按照你的方式,'.NavigateUrl'只是空的?當您將'.NavigateUrl'設置爲像google.com這樣的隨機URL時會發生什麼? – 2011-12-20 11:35:12
我添加了上面的代碼。 – 2011-12-20 11:45:25