我正在創建一個將從sql數據庫填充的手風琴。例如,如果數據庫有5條記錄(包括標題和說明),那麼手風琴將有5個面板。綁定到面板標題字段的標題和由說明組成的主體。像下面的圖片:從SQL數據庫值填充Accoridon
這裏是我迄今爲止使用DataList和添加手風琴的項目模板。
<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;
}
的任何信息創建這個動態的手風琴將是巨大的幫助。
感謝
你不是說用什麼樣的錯誤,如果有的話,你用當前的代碼得到...... – matt
@matt,沒有錯誤。手風琴只是不顯示。這導致我相信我沒有循環遍歷每條記錄並顯示數據庫中的值。 – CSharpDev4Evr