datalist 怎样从数据库中查询数据
2个回答
展开全部
就是用union就行了
比如你要的结果是
id name content
字段类型都是varchar,长度你自己定,我就说一下示意
然后有两个表,A(y,z) B(a,b,c)
假设A中的y对应name,z对应content
B中a对应id,b对应name,c对应content
那么
select '' as id,y as name,z as content from A
union all
select a as id,b as name,c as content from B
如果还有字段类型不一致的问题,可以用cast转换一下
比如
select '' as id,cast(y as varchar) as name,z as content from A
总之,要保证相互union的多个select语句的结果中字段数,类型,顺序完全一致.
比如你要的结果是
id name content
字段类型都是varchar,长度你自己定,我就说一下示意
然后有两个表,A(y,z) B(a,b,c)
假设A中的y对应name,z对应content
B中a对应id,b对应name,c对应content
那么
select '' as id,y as name,z as content from A
union all
select a as id,b as name,c as content from B
如果还有字段类型不一致的问题,可以用cast转换一下
比如
select '' as id,cast(y as varchar) as name,z as content from A
总之,要保证相互union的多个select语句的结果中字段数,类型,顺序完全一致.
展开全部
嵌套一套datalist
----------------
.aspx页面
在HeaderTemplate、ItemTemplate、FooterTemplate都有Label控件
<div>
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<HeaderTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%# Eval("proid") %>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</FooterTemplate>
</asp:DataList>
</div>
.aspx.cs文件
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer) //获取FooterTemplate中的Label
{
Label lbla = e.Item.FindControl("Label1") as Label;
lbla.Text = "aaaaaaa";
}
if (e.Item.ItemType == ListItemType.Header) //获取HeaderTemplate中的Label
{
Label lblb = e.Item.FindControl("Label2") as Label;
lblb.Text = "bbbbbbb";
}
if(e.Item.ItemType==ListItemType.Item) //获取ItemTemplate中的Label
{
Label lblc = e.Item.FindControl("Label3") as Label;
lblc.Text = "bbbbbbb";
}
if (e.Item.ItemType == ListItemType.AlternatingItem) //交替的偶数行
{
}
}
----------------
.aspx页面
在HeaderTemplate、ItemTemplate、FooterTemplate都有Label控件
<div>
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
<HeaderTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<%# Eval("proid") %>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</FooterTemplate>
</asp:DataList>
</div>
.aspx.cs文件
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer) //获取FooterTemplate中的Label
{
Label lbla = e.Item.FindControl("Label1") as Label;
lbla.Text = "aaaaaaa";
}
if (e.Item.ItemType == ListItemType.Header) //获取HeaderTemplate中的Label
{
Label lblb = e.Item.FindControl("Label2") as Label;
lblb.Text = "bbbbbbb";
}
if(e.Item.ItemType==ListItemType.Item) //获取ItemTemplate中的Label
{
Label lblc = e.Item.FindControl("Label3") as Label;
lblc.Text = "bbbbbbb";
}
if (e.Item.ItemType == ListItemType.AlternatingItem) //交替的偶数行
{
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询