0

I am working on a weboage that will display 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

2 Answers 2

1

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...

3
  • Your response is very helpful, but the client will have to click on an image icon "Show Answer" to view the answer. They have the ability to click the same image to hide the answer. Can I do a show / hide in a repeater control?
    – DNR
    Aug 9, 2011 at 16:38
  • 1
    @DotNetRookie - Of course you can, but the best way is to write the show/hide part in javascript Aug 9, 2011 at 16:46
  • Just like Joel said. Use JavaScript for show / hide the answer Aug 9, 2011 at 16:59
1

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 the Response.Write() method.
1
  • The data is a simple stored proc with a select statement to one table that contains all the rows. As far as getting data from the database, I am working on that part
    – DNR
    Aug 9, 2011 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.