用户控件的使用
主页代码:
<%@register tagprefix= "uc1 " tagname= "newlist " src= "newlist.ascx "%>
<%@ Page Language= "vb " AutoEventWireup= "false " Codebehind= "Default.aspx.vb " Inherits= "TheArticleSYS._Default "%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<title> 运城农业职业技术学院新闻管理系统欢迎您 </title>
<meta name= "GENERATOR " content= "Microsoft Visual Studio .NET 7.1 ">
<meta name= "CODE_LANGUAGE " content= "Visual Basic .NET 7.1 ">
<meta name= "vs_defaultClientScript " content= "JavaScript ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id= "form1 " runat= "server ">
<table id= "table1 " cellspacing= "0 " cellpadding= "0 " width= "762 " border= "0 ">
<tr>
<td colspan= "2 "> <img src= "images/bg12.jpg "> </td>
</tr>
<tr>
<td valign= "top " width= "265 ">
<table id= "table2 " height= "145 " cellspacing= "1 " cellpadding= "1 " width= "248 " border= "1 ">
<tr>
<td colspan= "2 "> <img src= "images/search.jpg "> </td>
</tr>
<tr>
<td width= "57 " height= "30 "> 类别 </td>
<td height= "30 "> <asp:DropDownList ID= "dropdownlist1 " Runat= "server "> </asp:DropDownList> </td>
</tr>
<tr>
<td width= "57 "> <p> 检索内容 </p>
</td>
<td> <asp:TextBox ID= "textbox1 " Runat= "server "> </asp:TextBox> <asp:DropDownList ID= "dropdownlist2 " Runat= "server ">
<asp:ListItem Value= "title "> 标题 </asp:ListItem>
<asp:ListItem Value= "writer "> 作者 </asp:ListItem>
<asp:ListItem Value= "keyword "> 关键字 </asp:ListItem>
</asp:DropDownList> </td>
</tr>
<tr>
<td width= "92 " colspan= "2 "> <asp:Button ID= "button1 " Runat= "server " Text= "检索 "> </asp:Button> </td>
</tr>
</table>
</td>
<td>
<asp:DataList ID= "datalist1 " Runat= "server " ShowFooter= "False " BackColor= "white " BorderStyle= "Double "
Width= "474px " RepeatDirection= "Horizontal " RepeatColumns= "1 " BorderColor= "#336666 " CellPadding= "0 "
GridLines= "Horizontal " BorderWidth= "1px ">
<SelectedItemStyle Font-Bold= "True " ForeColor= "white " BackColor= "#336666 "> </SelectedItemStyle>
<HeaderTemplate>
</HeaderTemplate>
<SeparatorStyle Width= "1px "> </SeparatorStyle>
<ItemStyle HorizontalAlign= "Left " ForeColor= "#333333 " BackColor= "white "> </ItemStyle>
<ItemTemplate>
<blockquote style= "font-weight:bold; font-size:12pt; color:azure; background-color:#009999 "> <%#databinder.eval(container.dataitem, "akinds ")%>
<!--分类文章列表入库口->
<a href= ' <%# "articleslist.aspx?moreid= "&databinder.eval(container.dataitem, "id ")%> ' target=_blank>
<img src= "images/more.gif " border= "0 "> </a> </blockquote> <blockquote>
<!--通过web用户控件实例newlist1的aid属性的数据库绑定获取aid的值->
<uc1:newlist id= "newlist1 " runat= "server " aid= ' <%# databinder.eval(container.dataitem, "id ")%> '>
</uc1:newlist>
</blockquote>
</ItemTemplate>
<FooterStyle ForeColor= "#333333 " BackColor= "white "> </FooterStyle>
<HeaderStyle Font-Size= "larger " Font-Bold= "True " HorizontalAlign= "Left " ForeColor= "white " VerticalAlign= "Middle "
BackColor= "#336666 "> </HeaderStyle>
</asp:DataList> </td>
</tr>
<tr>
<td width= "265 "> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</HTML>
控件代码:
<%@ Control Language= "vb " AutoEventWireup= "false " Codebehind= "newlist.ascx.vb " Inherits= "TheArticleSYS.newlist " TargetSchema= "http://schemas.microsoft.com/intellisense/ie5 " %>
<meta name= "vs_showgrid " content= "true ">
<table id= "table1 " style= "BORDER-RIGHT:ghostwhite 1px solid; BORDER-TOP:ghostwhite 1px solid; BORDER-LEFT:ghostwhite 1px solid; WIDTH:400px; BORDER-BOTTOM:ghostwhite 1px solid; HEIGHT:48px "
cellspacing= "1 " cellpadding= "1 " width= "450 " border= "1 ">
<tr>
<td align= "center "> <font face= "宋体 "> </font> </td>
</tr>
<asp:repeater ID= "repeater1 " Runat= "server ">
<ItemTemplate>
<tr>
<td style= "font-size:11pt;border-top-style:solid;border-bottom:cornsilk 1pt solid;border-right-style:solid;border-left-style:solid;height:19pt "
align= "left ">
<a href= ' <%# "thearticle.aspx?aid= "&databinder.eval(container.dataitem, "id ")%> ' title= ' <%# "文章标题 "&chr(10)&databinder.eval(container.dataitem, "title ")%> '>
<%#iif(len(databinder.eval(container.dataitem, "title "))> 20,left(databinder.eval(container.dataitem, "title "),20)& "...-- ",databinder.eval(container.dataitem, "title ")& "-- ")%>
</a> <font style= "font-size:10pt;color:darkgray;font-style:italic "> [ <%#databinder.eval(container.dataitem, "sdate ")%> ] </font>
</td>
</tr>
</ItemTemplate>
</asp:repeater>
<tr>
<td align= "center "> <font face= "宋体 "> </font> </td>
</tr>
</table>
控件隐藏代码:
Imports System
Imports System.Web
Imports System.Data
Imports System.Configuration.ConfigurationSettings
Public Class newlist
Inherits System.Web.UI.UserControl
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents repeater1 As System.Web.UI.WebControls.Repeater
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
'如果传入的参数不为空,则显示出文章信息
If aid <> " " Then
'Dim constr As String = "provider=microsoft.jet.oledb.4.0;data source= " + Server.MapPath( ". ") + "\Articlesys_db.mdb "
'Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(constr)
Dim mysql As String
Dim constr As String = ConfigurationSettings.AppSettings( "constr ") & Server.MapPath( ". ") & ConfigurationSettings.AppSettings( "dbs ")
Dim mycon As OleDb.OleDbConnection = New OleDb.OleDbConnection(constr)
mysql = "select top 5 title,id,sdate from articles where akinds= " + aid + "order by id desc "
Try
Dim mycmd As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(mysql, mycon)
Dim dt As DataSet = New DataSet
mycmd.Fill(dt)
repeater1.DataSource = dt.Tables(0)
repeater1.DataBind()
Catch ex As Exception
Response.Write(ex.Message)
End Try
End If
End Sub
End Class
提示我aid没有声明,请问一下什么原因啊,是我控件没有注册好吗?谢谢!
[解决办法]
UC里没有AID 你在页上写着AID不行啊
在PAGE——LOAD里写一个方法把AID加进去吧