从SQL数据库值填充Accoridon

时间:2015-05-19 15:35:49

标签: c# html sql-server accordion

我正在创建一个将从sql数据库填充的手风琴。例如,如果数据库有5个记录(包括标题和描述),那么手风琴将有5个面板。标题绑定到面板标题字段,主体包含描述的标题。如下图所示:

enter image description here

到目前为止,我使用DataList并将accordion添加到项目模板中。

<asp:DataList ID="DynamicAccordion" runat="server" DataKeyField="Id" RepeatDirection="Vertical">
    <HeaderTemplate>
        <!--items in here are rendered once for the entire table and can be title, etc...)    -->
    </HeaderTemplate>
    <ItemTemplate>
        <div class="panel-group" id="accordion">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h4 class="panel-title">
                        <a runat="server" data-toggle="collapse" href="#collapseOne" data-parent="#accordion">
                            <asp:Label runat="server" Text='<%#Eval("Section") %>' ID="SectionTitle"></asp:Label>
                        </a>
                    </h4>
                </div>
                <div id="collapseOne" class="panel-collapse collapse in">
                    <div class="panel-body">
                         <a runat="server" data-toggle="collapse" href="#collapseOne" data-parent="#accordion">
                            <asp:Label runat="server" Text='<%#Eval("Description") %>' ID="Description"></asp:Label>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </ItemTemplate>
    <FooterTemplate>
        <!--Same as the header template just the footer-->
    </FooterTemplate>
</asp:DataList>

这是将手风琴绑定到sql记录的后端:

   protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadData();
    }

}

private void LoadData()
{
    var data = DataAccess.GetCurrentProject();


    try
    {
        //data.Columns.Add("hrefPath");
        foreach (DataRow dr in data.Rows)
        {
            dr["SectionTitle"] = dr["Section"];
            dr["Description"] = dr["Description"];
        }
        DynamicAccordion.DataSource = data;
        DynamicAccordion.DataBind();

    }
    catch (Exception ex)
    {

    }
}

//Data Access class code
       public static DataTable GetCurrentProject()
        {
            DataTable dt = new DataTable();
            try
            {
                string sqlCommandText = "Select * FROM RequestData";
                using (SqlConnection connect = new SqlConnection(strConn))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter(sqlCommandText, connect))
                    {
                        sda.Fill(dt);
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return dt;
        }

任何有助于创建这种动态手风琴的信息都会很棒。

由于

1 个答案:

答案 0 :(得分:0)

对aspx文件中的每个循环使用a来执行此操作: (请记住,您还需要附加到您的ID字段以使其唯一,否则您将最终重复)

<% foreach (DataRow dr in data.Rows) { %>
    <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">
                    <a runat="server" data-toggle="collapse" href="#collapseOne" data-parent="#accordion">
                        <%= dr["Section"] %>
                    </a>
                </h4>
            </div>
            <div id="collapseOne" class="panel-collapse collapse in">
                <div class="panel-body">
                     <a runat="server" data-toggle="collapse" href="#collapseOne" data-parent="#accordion">
                        <%= dr["Description"] %>
                    </a>
                </div>
            </div>
        </div>
 <% } %>