引用内容:
<asp:datalist id="DataList1" runat="server" Width="545" RepeatDirection="Horizontal" RepeatColumns="2"
DataKeyField="classid" OnItemDataBound="BoundData" HorizontalAlign="Center">
<ItemTemplate>
<table cellpadding="0" cellspacing="1" border="0" bgcolor="#cccccc">
<tr>
<td bgcolor="#ffffff">
<asp:TextBox ID=txtBox Runat=server Visible=False Text='<%# DataBinder.Eval(Container.DataItem, "classid") %>'>
</asp:TextBox><%# DataBinder.Eval(Container.DataItem, "classname") %></td>
</tr>
<tr>
<td bgcolor="#ffffff" width="255" height="150" valign="top">
<asp:DataGrid ID="DataGrid1" AutoGenerateColumns="False" ShowHeader="False" ShowFooter="False"
Border="0" EnableViewState="False" Runat="server">
<Columns>
<asp:BoundColumn DataField="title"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
<tr>
<td bgcolor="#ffffff" align="right"><a href='list.aspx?classid=<%# DataBinder.Eval(Container.DataItem,"classid")%>'>更多新闻...</a></td>
</tr>
</table>
<br>
</ItemTemplate>
</asp:datalist>
.aspx.cs
引用内容: private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
getClassItem();
}
}
private void getClassItem()
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn=new SqlConnection(con);
conn.Open();
SqlCommand comm=new SqlCommand("select * from db_fClass order by classid ",conn);
SqlDataReader rd;
rd=comm.ExecuteReader();
DataList1.DataSource=rd;
DataList1.DataBind();
rd.Close();
conn.Close();
}
protected void BoundData(object sender,DataListItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
DataGrid dg=(DataGrid)e.Item.FindControl("DataGrid1");
// string index=this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
TextBox txtClassid=(TextBox)e.Item.FindControl("txtBox");
string index=txtClassid.Text;
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn=new SqlConnection(con);
conn.Open();
SqlCommand comm=new SqlCommand("select top 4 * from db_Article where classid="+index,conn);
SqlDataReader rd;
rd=comm.ExecuteReader();
dg.DataSource=rd;
dg.DataBind();
rd.Close();
conn.Close();
}
}
转载于:https://www.cnblogs.com/longhorn008/archive/2005/08/26/223776.html