Creating a generic Web Parts for hosting ASP.NET User Controls
2004-07-24 01:18
645 查看
In my previous post, I share a Calendar Web Part solution in C# for SharePoint Portal Server 2003, which demonstrates how to mix web parts and ASP.Net User Controls(.ASCX). Over there, you are forced to hardcode the name and path to a .ASCX file into the web part implementation. The following listing shows a part of codes.
// field to hold reference to user control object
private System.Web.UI.Control _innerUserControl;
// instantiate user control in CreateChildControls
protected override void CreateChildControls()
{
_innerUserControl = this.Page.LoadControl("/wpresources/WebUserControlMonth.ascx");
this.Controls.Add(_innerUserControl);
}
// render user control
protected override void RenderWebPart(HtmlTextWriter output)
{
// output.Write(SPEncode.HtmlEncode(Text));
this.EnsureChildControls();
_innerUserControl.RenderControl(output);
}
There is another solution by Barry Kouda regarding how to create a generic Web Parts for hosting ASP.Net User Control. It's really usefully to design web parts. The following contents show the main method of loading an ASP.NET user control into a Web Part and making it generic.
The approach involves using a shared Web Part property to track the name and path of the .ASCX file. Examine the following code:
// field to hold path to .ASCX file
private string _path;
// Web part property to make path configurable
[ WebPartStorage(Storage.Personal), Category("Miscellaneous"),
FriendlyName("User Control Path"),
Description("Path to .ascx file"),
Browsable(true), DefaultValue("")
]
public string ControlFile {
get { return _path; }
set { _path = value; }
}
// field to hold reference to user control object
Control uc;
// instantiate user control in CreateChildControls
protected override void CreateChildControls() {
this.Controls.Clear();
uc = this.Page.LoadControl(_path);
this.Controls.Add(uc);
}
// render user control
protected override void RenderWebPart(HtmlTextWriter output) {
uc.RenderControl(output);
}[/b]
Now, the file name and path to the .ASCX file are configurable. It’s possible for any SharePoint developer, Web designer or content manager to the UsercontrolHost web part to a web part page and configure the User Control File property through configuration functionality.
// field to hold reference to user control object
private System.Web.UI.Control _innerUserControl;
// instantiate user control in CreateChildControls
protected override void CreateChildControls()
{
_innerUserControl = this.Page.LoadControl("/wpresources/WebUserControlMonth.ascx");
this.Controls.Add(_innerUserControl);
}
// render user control
protected override void RenderWebPart(HtmlTextWriter output)
{
// output.Write(SPEncode.HtmlEncode(Text));
this.EnsureChildControls();
_innerUserControl.RenderControl(output);
}
There is another solution by Barry Kouda regarding how to create a generic Web Parts for hosting ASP.Net User Control. It's really usefully to design web parts. The following contents show the main method of loading an ASP.NET user control into a Web Part and making it generic.
The approach involves using a shared Web Part property to track the name and path of the .ASCX file. Examine the following code:
// field to hold path to .ASCX file
private string _path;
// Web part property to make path configurable
[ WebPartStorage(Storage.Personal), Category("Miscellaneous"),
FriendlyName("User Control Path"),
Description("Path to .ascx file"),
Browsable(true), DefaultValue("")
]
public string ControlFile {
get { return _path; }
set { _path = value; }
}
// field to hold reference to user control object
Control uc;
// instantiate user control in CreateChildControls
protected override void CreateChildControls() {
this.Controls.Clear();
uc = this.Page.LoadControl(_path);
this.Controls.Add(uc);
}
// render user control
protected override void RenderWebPart(HtmlTextWriter output) {
uc.RenderControl(output);
}[/b]
Now, the file name and path to the .ASCX file are configurable. It’s possible for any SharePoint developer, Web designer or content manager to the UsercontrolHost web part to a web part page and configure the User Control File property through configuration functionality.
相关文章推荐
- [.NET控件]Telerik RadControls for ASP.NET AJAX 2008 Q1 net 2.0 Web.UI
- MSPlus WebControls For ASP.NET. (服务器端控件)
- Deploying a Web Application that Uses NetAdvantage for ASP.NET Controls....
- [.NET控件]Telerik RadControls for ASP.NET AJAX 2008 Q1 net 3.5 Web.UI
- Deploying a Web Application that Uses NetAdvantage for ASP.NET Controls....
- Ajax Web Controls for ASP.NET!
- 【转】Creating a JSONP Formatter for ASP.NET Web API
- IIS7 beta FREE WEBHOSTING FREE HOSTING FOR ASP.NET 2.0 IIS7
- Janus Web/ASP.NET Server Controls v3(3.5.0.35) for VS2005
- iis7 发布mvc3 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容及Login on failed for "IIS APPPOOL\ASP.NET v4.0"问题
- Pass data from a ASP.NET page to ASCX user controls loaded dynamically
- ASP.NET tips: Golden rules for Dynamic Controls.
- Creating Custom ASP.NET Server Controls with Embedded JavaScript
- .NET 3.5 树型控件 John.WebControls.Tree for .NET 3.5 RTM
- IIS7 Login failed for user IIS APPPOOL\ASP.NET v4.0
- Server Controls for ASP.NET
- Web Parts 在 ASP.NET 2.0 中的应用
- ComponentArt Web.UI 2008.1 for ASP.NET
- ASP.Net 2.0 Web Parts in Action: Building Dynamic Web Portals
- Web Servers in Visual Studio for ASP.NET Web Projects