<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属性啊,怎么设置?

解决方案 »

  1.   


     Listbox1.DataSource = da.Table[0];
     Listbox1.DataTextField = "要显示的列";
     Listbox1.DataValueField = "值";
     Listbox1.DataBind();
      

  2.   

    没有明白我的意思,listbox是在DATALIST中的!
      

  3.   

    你应该在DataList的ItemDataBound里对ListBox进行绑定。如:
    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());
    }
    }
      

  4.   

    代码给你了呀,你只要改个表名,字段名就Ok了。你先试试是不是你想要的结果。当绑定DataList时首先触发该事件的。
      

  5.   

    <asp:ListBox ID="ListBox1" runat="server" Height="111px" Width="377px" DataSource=' <%#  getdata3(Eval("id").ToString())%>' DataTextField="你的字段" DataValueField="你的字段"> </asp:ListBox> 
      

  6.   

     to: amandag 
    <asp:ListBox ID="ListBox1" runat="server" Height="111px" Width="377px" DataSource=' <%#  getdata3(Eval("id").ToString())%>' DataTextField="你的字段" DataValueField="你的字段"> </asp:ListBox> 
    这个字段怎么写啊,是写getdata3()函数返回的datatable中的字段么,但是会提示错误的,测试时把这个字段当成是datalist中的字段了,而datalist有没有这个字段,哪个字段怎么写啊?
      

  7.   

    to:weiweir 
    那个参数怎么传递啊?不用传么?
      

  8.   

    不是。我那个是我的一个函数。你不是有个读取数据的方法吗,你可以这样:
    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());
    }
    你试试。我这没法测试
      

  9.   

    错误提示:用户代码未处理 IndexOutOfRangeException
    在位置0处没有任何行,
    是什么意思啊?
      

  10.   

    那你就直接在那个事件里先读出数据,然后在提取。我那个方法【new DbObject().Query(str)】是写在类库里的,里面包含了对数据库的操作。你可以直接写的
      

  11.   

    if(e.Item.ItemType==ListItemType.Item ||e.Item.ItemType==ListItemType.AlternatingItem) 
                    {
                        DataSet ds = GetData("id"); 
                        (e.Item.FindControl("ListBox1") as ListBox).Items.Add(ds.Tables[0].Rows[e.Item.ItemIndex]["id"].ToString()); 
                    } 
    他就提示这样的错误了!写得不对么?
      

  12.   

    if(e.Item.ItemType==ListItemType.Item ||e.Item.ItemType==ListItemType.AlternatingItem) 
                    {
                        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怎么传递啊?老大
      

  13.   

    你想在ListBoxt里显示什么的??想显示哪个字段就在[e.Item.ItemIndex]["字段"]。
      

  14.   


    你的查询中没有的字段,DataList当然不会显示,你的表结构也不贴出来,也不说究竟要显示那些数据,帮你的人只能和你一样瞎猜你的意思?不会一个问题不要紧,但连问题都描述不清,这个问题才大
      

  15.   

    table
    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
    不知道这样说能不能明白,就是这样的意思,不好意思,没有说明白
      

  16.   

    不能这么赋值的!
    要显示得调用dataBind()才行。你得先绑别的,有datasouse的地方用别的办法绑吧。
    我以前做过的!~