先在你的DataList上绑定DataMember的数据,也就是你要查询的字段。如下:
<asp:DataList ID="DataList1" runat="server" DataMember="ID"></<asp:DataList>
然后再添加一个事件:OnItemCreated在这个事件里添加你的代码。代码如下:
aspx页面:
<asp:DataList ID="DataList1" runat="server" DataMember="ID" OnItemCreated="DataList1_ItemCreated"></<asp:DataList> cs页面:
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
int ID = ((MtNets.ItsWebsite.Model.Category)e.Item.DataItem).ID;
IList list = MtNets.ItsWebsite.BLL.ItsWebInfo.GetByID(ID);
DataList dl = ((Repeater)e.Item.FindControl("DataList2"));
dl.DataSource = list;
dl.DataBind();
}
<asp:DataList ID="DataList1" runat="server" DataMember="ID"></<asp:DataList>
然后再添加一个事件:OnItemCreated在这个事件里添加你的代码。代码如下:
aspx页面:
<asp:DataList ID="DataList1" runat="server" DataMember="ID" OnItemCreated="DataList1_ItemCreated"></<asp:DataList> cs页面:
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
int ID = ((MtNets.ItsWebsite.Model.Category)e.Item.DataItem).ID;
IList list = MtNets.ItsWebsite.BLL.ItsWebInfo.GetByID(ID);
DataList dl = ((Repeater)e.Item.FindControl("DataList2"));
dl.DataSource = list;
dl.DataBind();
}
解决方案 »
- 求获取非标签内容的正则表达式
- 菜鸟高分求一条简单的查询语句
- 身份验证
- TreeView绑定数据库的问题
- 没有权限加入收藏夹!!!!
- 使用ajax时 回发或回调参数无效 怎么办
- C#中做asp.net中this.Page.SmartNavigation在浏览中不起作用??
- 网页中gridview、treeview中字体的大小不随IE浏览器中字体的改变而改变
- 100分求局域网用户认证解决方案
- 在站点移植过程中,只有.net中才出现的IIS配置问题
- FIF ASP 互动 教材 VIP版(ASP教程.视频) 网络书包+事例代码+vip所有章节+课后习题答案+VIP书包
- ===Session过期的问题。===
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal" OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<div style="background-color:Green">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Sex") %>' Visible="false"></asp:Label>
<%#bool.Parse(Eval("Sex").ToString()) == true ? "男" : "女" %>
</div>
<asp:DataList ID="DataList2" runat="server">
<ItemTemplate>
<%#Eval("RealName") %>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class DataListDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindSex();
}
}
//绑定顶级项目
private void BindSex()
{
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
SqlCommand command = new SqlCommand("select distinct sex from UserInfo", connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable data = new DataTable();
adapter.Fill(data);
DataList1.DataSource = data;
DataList1.DataBind();
}
//当绑定DataList1中的每一项时的处理方法
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
//如果要绑定的项是交替项或者是普通项
//注意此外还有脚模板和脚模版
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//e.Item表示当前绑定的那一行
//利用e.Item.FindControl("Label1")来找到那一行的id为"Label1"的Label控件
Label lbSex = (Label)(e.Item.FindControl("Label1"));
//利用e.Item.FindControl("Label1")来找到那一行的id为"Label1"的Label控件
DataList dl2 = (DataList)(e.Item.FindControl("DataList2"));
bool male = bool.Parse(lbSex.Text);
dl2.DataSource = GetDataTable(male);
dl2.DataBind();
}
}
/// <summary>
/// 根据性别来查找符合条件的用户
/// </summary>
/// <param name="male">是否为男性</param>
/// <returns></returns>
private DataTable GetDataTable(bool male)
{
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
SqlCommand command = new SqlCommand("select top 3 RealName from UserInfo where Sex=@Sex order by UserID", connection);
command.Parameters.AddWithValue("@Sex", male);//添加SqlParameter参数
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable data = new DataTable();
adapter.Fill(data);
return data;
}
}对上面的程序代码作几点说明:(1)在上面的代码中使用了两个DataList控件,其中第二个是位于第一个的ItemTemplate模版里面,这个用于绑定符合当前项中条件的数据,并且我们在第一个的ItemTemplate里面还用到了一个Label控件,这个Label是不可见的(Visible="false"),使用这个控件并不是为了显示数据,而是为了保存第二个DataList要绑定显示的数据的条件。在这里我们是以性别作为顶级分类的,其实这个没有必要在数据库查询并进行绑定显示,这里只是通过这种方法来演示DataList如何进行嵌套绑定。(2)在嵌套绑定的时候我们利用了DataList的ItemDataBound事件,在绑定DataList中的每一项时候都会激发这个事件,当要绑定的项是普通项或者是交替项时,项模版里就有要显示子数据的DataList控件和我们隐藏Label控件,我们利用FindControl()方法找到这两个控件,利用Label的Text属性值作为条件去数据库查找满足条件的数据,并将返回的数据源绑定到第二个DataList上,这样就完成了DataList的嵌套绑定。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhoufoxcn/archive/2008/10/24/3134608.aspx
<asp:datalist id="dlFileType" RepeatColumns="2" runat="server">
<ItemTemplate>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%" height="88" align="center" valign="top">
<img src='<%#DataBinder.Eval(Container.DataItem, "cnvcPicURL")%>' width="80" height="80">
</td>
<td valign="top">
<table width="96%" border="0" cellpadding="0" cellspacing="0">
<tr width="100%">
<td colspan="2"><img src='<%#PageBase.strStyleRoot+"/picture/pic_fwzn_08.gif"%>' width="154" height="20">
<asp:Label id="labFileType" runat="server" Visible=False Text='<%# DataBinder.Eval(Container.DataItem,"cniFileTypeID")%>'>
</asp:Label></td>
</tr>
<tr>
<td width="300">
<asp:DataList id="dlFileList" runat="server" RepeatColumns="1" Width="100%">
<ItemTemplate>
<TABLE cellSpacing="1" cellPadding="1" width="100%" border="0">
<tr>
<td width="7%" height="20" align="center">
<img src='<%#PageBase.strStyleRoot+"/picture/pic_fwzn_dot.gif"%>' width="3" height="3"></td>
<td width="93%">
<font color="#393939">
<%#GetTitle((string)Convert.ToString(DataBinder.Eval(Container.DataItem, "cnvcTitle")),(string)Convert.ToString(DataBinder.Eval(Container.DataItem, "cnvcFileType")),(string)Convert.ToString(DataBinder.Eval(Container.DataItem, "cniBaseFileID")),(DateTime)DataBinder.Eval(Container.DataItem, "cndtPublishTime"))%>
</font>
</td>
</tr>
</TABLE>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
<tr>
<td colspan="2" bgcolor="E6E6E6" height="1"><img src='<%#PageBase.strStyleRoot+"/picture/1X1.gif"%>' width="1" ></td>
</tr>
<tr align="center">
<td height="22" colspan="2"><a href="#" title="可查看到更多相关内容"><img src='<%#PageBase.strStyleRoot+"/picture/more.gif"%>' width="34" height="11" border="0"></a></td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>
--------后台cs代码------
内层控件数据绑定与事件声明在外层的ItemDataBind中实现private void dlFileType_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList dl = null;
Label labTypeID = null;
dl = (DataList)e.Item.FindControl("dlFileList")
labTypeID = (Label)e.Item.FindControl("lbFileType"); string typeID = labTypeID.Text.ToString();
int iTypeID = Convert.ToInt32(typeID); string commandText = "select * from tbfile";
commandText = commandText + " Where TypeID=" + iTypeID;
//------------
string connString = ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connString;
conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(commandText,conn);
DataSet ds = new DataSet();
myCommand .Fill(ds,"tbFile");
conn.Close();
//------------ dl.DataSource = ds.Tables["tbFile"];;
dl.DataBind();
}
}
呵呵,写错了
int iTypeID = (int)DataBinder.Eval(e.Item.DataItem, "cniFileTypeID");
http://blog.csdn.net/insus/archive/2008/09/02/2865244.aspx