Asp.net2.0使用技巧---WebParts与用户控件

发布时间: 2007-01-26 12:09    作者: 未知    来源: 未知    浏览:    评论

如果在网页的某些内容的表现方式一样,而且内容也一样,为了减少工作量,我们可以创建一个用户控件来重复使用,这样可以减少代码的维护。

一、下面就是WebuserControlContent.ascx的内容:

%@ Control Language="VB" AutoEventWireup="false" CodeFile="textcontent.ascx.vb" Inherits="textcontent" %>
<table style="width: 176px" cellpadding="0" cellspacing="0">
    
<tr>
        
<td style="width: 176px">
               <table>
                 <tr>
                    <td id="imgL"></td>
                    <td id="ImgM"></td>
                    <td id="ImgR"></td>
                  </tr>
               </table>
        
</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,结果如下:

<%@ Page Language="VB" MasterPageFile="~/text.master" AutoEventWireup="false" CodeFile="text_default.aspx.vb" Inherits="text_default" %>
<%@ Register TagPrefix ="uc" TagName ="textcontent" Src ="~/textcontent.ascx"  %>
<%@ Register TagPrefix="Nsquared2" Assembly="Nsquared2.Web" 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
Imports System.Data.SqlClient

Partial Class text_default
    
Inherits System.Web.UI.Page

    
Private _obj As Control = Nothing
    
Private controlID As String = ""

    
Sub GetChildControl(ByVal ParentControl As Control)
        
If ParentControl.HasControls Then
            
For Each ct1 As Control In ParentControl.Controls
                
If Not (ct1.ID Is NothingAndAlso ct1.ID.StartsWith(controlID, StringComparison.OrdinalIgnoreCase) Then
                    _obj 
= ct1
                    
Return
                
End If
                GetChildControl(ct1)
            
Next
        
End If
    
End Sub



    
Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
        
Dim classid As Integer() = {718245}
        
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 NothingThen
                
Dim contentUC As UserControl = CType(_obj, UserControl)
                
Dim td As HtmlTableCell
                td 
= CType(contentUC.FindControl("contentfooter"), HtmlTableCell)
                td.InnerHtml 
= "<a href='#'>" + "<img src='http://www.aspxclub.com/UploadFile/Material/1/1928.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:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
    
<asp:ListItem>browser</asp:ListItem>
    
<asp:ListItem>design</asp:ListItem>
    
<asp:ListItem>edit</asp:ListItem>
    
<asp:ListItem>catalog</asp:ListItem>
</asp:DropDownList>

 

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        
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部件属性的编辑、位置的移动、显示状态的改变等等。



TAG

Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符

Tags

SQL 数据库 asp.net C# XML 控件 .NET教程 程序 事件 数据 安全 代码 Server 客户端 验证 数据库专栏 接口 文件 Oracle DataSet 函数 DataGrid 问题 .net return C#语言 JavaScript 服务 IIS 对象 语句 windows 继承 时间 web.config 设计 开发 参数 变量 解决 字符 ADO.net 环境 VB.Net语言 web 异常 工具 服务器 计算 实例 OLEDB Application VB Word WebService insert asp net 安装 记录

精华推荐

更多

精品下载

更多