日期:2014-05-17  浏览次数:20531 次

gridview嵌套datalist
想在gridview里面嵌套一个datalist,当点击 "详情 "时,在此行的下面用datalist显示当前行的详细信息.这个怎么做啊?

------解决方案--------------------
完整的例子
HTML code
<%@ Page Language="C#" EnableViewState="false" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">  


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add(new System.Data.DataColumn("Id", typeof(System.Int32)));
            dt.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));

            for (int i = 0; i < 8; i++)
            {
                dt.Rows.Add(new Object[] { i, "孟子章" + i.ToString() });
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }



    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            String UnitId = DataBinder.Eval(e.Row.DataItem, "Id").ToString();
            //这里只是测试,取数据方法可以连接数据库
            System.Data.DataTable dt2 = new System.Data.DataTable();
            dt2.Columns.Add(new System.Data.DataColumn("DDId", typeof(System.Int32)));
            dt2.Columns.Add(new System.Data.DataColumn("Description", typeof(System.String)));

            for (int i = 0; i < 8; i++)
            {
                dt2.Rows.Add(new Object[] { i, "单位id=" + UnitId + "的信息" + i.ToString() });
            }

            DataList d = e.Row.FindControl("det") as DataList;
            d.DataSource = dt2;
            d.DataBind();
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
        function show(id) {
            d = document.getElementById("div" + id);
            d.style.display = d.style.display == "none" ? "" : "none";
        }
    </script>
</head>
<body>
    <form runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <%#Eval("Title") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <a href='' onclick='show(<%#Eval("Id") %>);return false;'>详情</a>
                    <div style="display: none" id="div<%#Eval("Id") %>">
                        <asp:DataList ID="det" runat="server">
                            <ItemTemplate>
                                <%#Eval("Description") %>
                            </ItemTemplate>
                        </asp:DataList>
                    </div>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>