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

本文介绍如何在ASP.NET中使用用户控件(textcontent.ascx)来展示新闻列表,包括设置不同栏目内容、动态绑定数据及实现Web部件的多种显示模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

一、下面就是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='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: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部件属性的编辑、位置的移动、显示状态的改变等等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值