dynamically populating div in asp.net
I am working on a weboage that will displ开发者_如何学编程ay questions and answers (maybe 5 at one time, maybe 7 at another time) returned from a database table. The questions will each be displayed in a div and the related answers displayed in another div. The question will have an icon "Show Answer / Hide Answer"
How can I go about creating a div and then populating it with values from a table?
Thanks
I would use repeater for that.
1.Create data source pulling data from your database
<asp:sqlDataSource Id="sqldsQuestionsAnswers" ... />
2.Create repeater linking to that data source:
<asp:repeater DataSourceId="sqldsQuestionsAnswers" runat="server">
<itemTemplate>
<div>
<%# Eval("question") %>
<hr/>
<%# Eval("answer") %>
</div>
</itemTemplate>
</asp:repeater>
The repeater will display anything whats in <itemTemplate>
tag for every row returned by your query.
So if your query returns 2 questions like that:
Question-------------Answer
-----------------------------------
question1?----------answer1
question2?----------answer2
The output would be:
<div>
question1?
<hr/>
answer1
</div>
<div>
question2?
<hr/>
answer2
</div>
I hope it helps...
We need to know more about how you retrieve your question data and the context of the rest of your page, but you can do a few things here (roughly in order of preference):
- Bind your data to an
<asp:Repeater >
control (or even one of the grid controls) - Build a custom or user control to render your questions and drop that on your page
- Build your desired html as a string in code and set it to
<asp:Panel >
control (Panels render as div tags). If you want to be able to refresh your div without reloading the entire page (AJAX), you can use an<asp:UpdatePanel >
. - Build your desired html in code and write directly to the response, either via
<%= %>
or<%: %>
bee-stings or with theResponse.Write()
method.
精彩评论