<body>
<form id="form1" runat="server">
<div>
</div>
<div style="width: 80%; height: 272px">
<asp:DataList ID="DataList1" runat="server" >
<ItemTemplate>
<table style="width: 805px">
<tr>
<td style="width: 276px">
<%# DataBinder.Eval(Container.DataItem, "id")%>
</td>
</tr>
<tr>
<td style="height: 142px" colspan="2">
<asp:ListBox ID="ListBox1" runat="server" Height="111px" Width="377px" DataSource='<%# getdata3(Eval("id").ToString())%>' ></asp:ListBox></td>
<td style="height: 142px" colspan="2">
<asp:ListBox ID="ListBox2" runat="server" Height="116px" Width="361px" DataSource='<%# getdata(Eval("id").ToString())%>'></asp:ListBox></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
cs
protected DataSet getdata3(string id)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
con.Open();
SqlDataAdapter dr = new SqlDataAdapter("select * from table2 where id='" + id+ "'", con);
DataSet tab = new DataSet();
dr.Fill(tab);
con.Close();
return tab;
}
protected DataTable getdata(string id)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
con.Open();
SqlDataAdapter dr = new SqlDataAdapter("select * from table1 where id='" + id +"'", con);
DataTable tab = new DataTable();
dr.Fill(tab);
con.Close();
return tab;
}
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
con.Open();
SqlDataAdapter dr = new SqlDataAdapter("select * from table", con);
DataSet da = new DataSet();
dr.Fill(da);
DataList1.DataSource = da;
DataList1.DataKeyField = "id";
DataList1.DataBind();
con.Close();
}
请各位老大帮忙看看有什么问题没》listbox中没有数据显示,是不是要设置datatextfield属性啊,怎么设置?
<form id="form1" runat="server">
<div>
</div>
<div style="width: 80%; height: 272px">
<asp:DataList ID="DataList1" runat="server" >
<ItemTemplate>
<table style="width: 805px">
<tr>
<td style="width: 276px">
<%# DataBinder.Eval(Container.DataItem, "id")%>
</td>
</tr>
<tr>
<td style="height: 142px" colspan="2">
<asp:ListBox ID="ListBox1" runat="server" Height="111px" Width="377px" DataSource='<%# getdata3(Eval("id").ToString())%>' ></asp:ListBox></td>
<td style="height: 142px" colspan="2">
<asp:ListBox ID="ListBox2" runat="server" Height="116px" Width="361px" DataSource='<%# getdata(Eval("id").ToString())%>'></asp:ListBox></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
cs
protected DataSet getdata3(string id)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
con.Open();
SqlDataAdapter dr = new SqlDataAdapter("select * from table2 where id='" + id+ "'", con);
DataSet tab = new DataSet();
dr.Fill(tab);
con.Close();
return tab;
}
protected DataTable getdata(string id)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
con.Open();
SqlDataAdapter dr = new SqlDataAdapter("select * from table1 where id='" + id +"'", con);
DataTable tab = new DataTable();
dr.Fill(tab);
con.Close();
return tab;
}
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
con.Open();
SqlDataAdapter dr = new SqlDataAdapter("select * from table", con);
DataSet da = new DataSet();
dr.Fill(da);
DataList1.DataSource = da;
DataList1.DataKeyField = "id";
DataList1.DataBind();
con.Close();
}
请各位老大帮忙看看有什么问题没》listbox中没有数据显示,是不是要设置datatextfield属性啊,怎么设置?
解决方案 »
- winform窗体显示问题
- 已经打开了多个浏览器窗口,分别显示不同网页内容,怎样定时逐个显示每个浏览器窗口
- 急,求高手指点阿,写好的C#程序安装的问题
- PictureBox关于作图模式的问题
- return的问题 (急,在线等)
- 用C#实现远程TELNET远程访问路由器,哪位大侠做过?求个思路,小弟人生的第一个项目啊!~
- .net的winform程序有没有什么可以不装.net环境运行的东东。
- [200分请教!!!!!!!!!!!!]RSACryptoServiceProvider的问题
- 在线等一问题解决思路,如何判断一次操作结束
- 帮忙每行翻译下
- 难题:不可删除的文件夹,如何通过程序读取文件夹内容?
- C# 半透明窗口控件不透明
Listbox1.DataSource = da.Table[0];
Listbox1.DataTextField = "要显示的列";
Listbox1.DataValueField = "值";
Listbox1.DataBind();
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
string str = "select top 10 * from table order by id desc";
DataSet ds = new DataSet();
ds = new DbObject().Query(str);
if(ds.Tables[0].Rows.Count>0)
{
(e.Item.FindControl("ListBox1") as ListBox).Items.Add(ds.Tables[0].Rows[e.Item.ItemIndex]["name"].ToString());
}
}
<asp:ListBox ID="ListBox1" runat="server" Height="111px" Width="377px" DataSource=' <%# getdata3(Eval("id").ToString())%>' DataTextField="你的字段" DataValueField="你的字段"> </asp:ListBox>
这个字段怎么写啊,是写getdata3()函数返回的datatable中的字段么,但是会提示错误的,测试时把这个字段当成是datalist中的字段了,而datalist有没有这个字段,哪个字段怎么写啊?
那个参数怎么传递啊?不用传么?
if(e.Item.ItemType==ListItemType.Item ¦ ¦e.Item.ItemType==ListItemType.AlternatingItem)
{
DataSet ds = getdata3("字段");
(e.Item.FindControl("ListBox1") as ListBox).Items.Add(ds.Tables[0].Rows[e.Item.ItemIndex]["字段"].ToString());
}
你试试。我这没法测试
在位置0处没有任何行,
是什么意思啊?
{
DataSet ds = GetData("id");
(e.Item.FindControl("ListBox1") as ListBox).Items.Add(ds.Tables[0].Rows[e.Item.ItemIndex]["id"].ToString());
}
他就提示这样的错误了!写得不对么?
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
con.Open();
SqlDataAdapter dbadapter = new SqlDataAdapter("select * from table1 where id='" + id + "'", con);
DataSet ds = new DataSet();
dbadapter.Fill(ds);
con.Close();
(e.Item.FindControl("ListBox1") as ListBox).Items.Add(ds.Tables[0].Rows[e.Item.ItemIndex]["title"].ToString());
}
我的代码是这样的,但是ID怎么传递啊?老大
你的查询中没有的字段,DataList当然不会显示,你的表结构也不贴出来,也不说究竟要显示那些数据,帮你的人只能和你一样瞎猜你的意思?不会一个问题不要紧,但连问题都描述不清,这个问题才大
id title content
1 ,rr,ggggg
2,tt,jjjj
table2
id,title,content,time
1,ss,hhhhhh,17:00
2,rr,gggggg,18:00
.....
datalist1的数据是table的数据
datalist1中的listbox要显示table1中的title和content信息,条件是table.title=table1.title
不知道这样说能不能明白,就是这样的意思,不好意思,没有说明白
要显示得调用dataBind()才行。你得先绑别的,有datasouse的地方用别的办法绑吧。
我以前做过的!~