如果在网页的某些内容的表现方式一样,而且内容也一样,为了减少工作量,我们可以创建一个用户控件来重复使用,这样可以减少代码的维护。
一、下面就是WebuserControlContent.ascx的内容:
< table style = " width: 176px " cellpadding = " 0 " cellspacing = " 0 " >
< tr >
< td style = " width: 176px " >
</ td >
</ tr >
< tr >
< td style = "" >
</ td >
</ tr >
< tr >
< td style = "" >
< asp:DataList ID = " MainContent " runat = " server " RepeatColumns = " 1 " >
< ItemTemplate >
< table style = " width: 176px " >
< tr >
< td style = " width: 20px " >& nbsp;
< asp:Image ID = " Image1 " runat = " server " ImageUrl = " ~/images/s_img1.jpg " /></ td >
< td style = " width: 180px " >
< a href = ' textdetailsinfo.aspx?id=<%# Eval("newsid") %>'><%# Eval("news_title") %></a>
< img src = " images/new.gif " alt = " new " />
</ td >
</ tr >
</ table >
</ ItemTemplate >
</ asp:DataList >
</ td >
</ tr >
< tr >
< td id = " ContentFooter " runat = " server " style = "" align = " right " valign = " top " >
</ td >
</ tr >
</ table >
这里的做法是将页面中都出现的、与栏目相关的标记都设为服务器端控件,以便在使用时用程序进行动态的替换,如标题图片、标题图标、背景图片、内容列表和“更多”链接,都提炼成服务器控件来处理。
二、新建一个usercontrol.aspx文件,并往里面添加一个webpartmanager控件和Table,接下来从工具箱中拖放几个webpartzone,并修改其HeaderText属性,在webpartzone中插入几个webusercontrolcontent.ascx,其id分别为usercontrolcontenthufu、usercontrolcontentliuxinqushi、usercontrolcontentkexuegouyi,结果如下:
< %@ Register TagPrefix = " uc " TagName = " textcontent " Src = " ~/textcontent.ascx " % >
< %@ Register TagPrefix = " Nsquared2 " Assembly = " Nsquared2.Web " Namespace = Namespace="Nsquared2.Web.UI.WebControls.WebParts"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width: 576px" cellpadding ="0" cellspacing ="0">
<tr>
<td style="width: 288px;" align ="left">
<Nsquared2:TemplatedWebPartZone ChromeTemplateFile="~/Chrome_templates/WebPartTemplate.chrome" LayoutOrientation="Vertical" ID="WebPartZone1" runat="server" HeaderText="内容列一">
<zonetemplate>
<uc:textcontent ID="textcontenthufu" runat="server" title="护肤"/>
<uc:textcontent ID ="textcontentjiansheng" runat ="server" title="健身"/>
<uc:textcontent ID ="textcontentliuxinqushi" runat ="server" title="流行趋势"/>
</zonetemplate>
</Nsquared2:TemplatedWebPartZone>
</td>
<td style="width: 288px;" align ="left" >
<Nsquared2:TemplatedWebPartZone ChromeTemplateFile="~/Chrome_templates/WebPartTemplate.chrome" LayoutOrientation="Vertical" ID="WebPartZone2" runat="server" HeaderText="内容列二">
<zonetemplate>
<uc:textcontent ID ="textcontentmeifa" runat ="server" title="美发"/>
<uc:textcontent ID ="textcontentmeirong" runat ="server" title="美容"/>
<uc:textcontent ID ="textcontentkexuegouyi" runat ="server" title="科学购衣"/>
</zonetemplate>
</Nsquared2:TemplatedWebPartZone>
</td>
</tr>
</table>
</asp:Content>
三、编写后台处理程序
Imports System.Data.SqlClient
Partial Class text_default Class text_default
Inherits System.Web.UI.Page
Private _obj As Control = Nothing
Private controlID As String = ""
Sub GetChildControl()Sub GetChildControl(ByVal ParentControl As Control)
If ParentControl.HasControls Then
For Each ct1 As Control In ParentControl.Controls
If Not (ct1.ID Is Nothing) AndAlso ct1.ID.StartsWith(controlID, StringComparison.OrdinalIgnoreCase) Then
_obj = ct1
Return
End If
GetChildControl(ct1)
Next
End If
End Sub
Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim classid As Integer() = {7, 1, 8, 2, 4, 5}
Dim columnname As String() = {"hufu", "jiansheng", "liuxinqushi", "meifa", "meirong", "kexuegouyi"}
Dim columntitle As String() = {"护肤", "健身", "流行趋势", "美发", "美容", "科学购衣"}
Dim conntxt As New SqlConnection
conntxt.ConnectionString = Application("conn")
conntxt.Open()
GetChildControl(Form)
Dim i As Integer = 0
While i < 6
_obj = Nothing
controlID = "textcontent" + columnname(i)
GetChildControl(Form)
If Not (_obj Is Nothing) Then
Dim contentUC As UserControl = CType(_obj, UserControl)
Dim td As HtmlTableCell
td = CType(contentUC.FindControl("contentfooter"), HtmlTableCell)
td.InnerHtml = "<a href='#'>" + "<img src='images/more1.gif' alt='更多内容" + columntitle(i) + "...'/></a>"
Dim dl As DataList = CType(contentUC.FindControl("maincontent"), DataList)
Dim sqltxt As String = "select top 8 newsid,news_title,news_edittime from news where news_classid=" + classid(i).ToString + "order by news_edittime desc"
Dim cmdtxt As SqlCommand = New SqlCommand(sqltxt, conntxt)
Dim drtxt As SqlDataReader = cmdtxt.ExecuteReader
dl.DataSource = drtxt
dl.DataBind()
drtxt.Close()
End If
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
conntxt.Close()
conntxt.Dispose()
End Sub
End Class
至此一个web部件的页面就创建成功了。
四、web部件有四种显示方式,分别是BrowseDisplayMode、DesignDisplayMode、EditDisplayMode、CatalogDisplayMode,要实现这些功能可以通过一个按钮来完成,点击按钮的时候改变其Displaymode,具体如下:
< asp:ListItem > browser </ asp:ListItem >
< asp:ListItem > design </ asp:ListItem >
< asp:ListItem > edit </ asp:ListItem >
< asp:ListItem > catalog </ asp:ListItem >
</ asp:DropDownList >
Select Case DropDownList1.SelectedValue
Case "browser"
WebPartManager1.DisplayMode = WebPartManager.BrowseDisplayMode
Case "design"
WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode
Case "edit"
WebPartManager1.DisplayMode = WebPartManager.EditDisplayMode
Case "catalog"
WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode
End Select
End Sub
通过选择下拉菜单里的不同选项,可以对web部件属性的编辑、位置的移动、显示状态的改变等等。