I'm extracting data from two lists (a Questions list and an Answers list) and am displaying them as question on one line, then answer on the other, and then it repeats, all in one Label. I want the questions in one color, and the answers in one color. If it was in tabular form, it would be easier, but I'm not sure how to do that either. Is it possible? And if so, how? This is the code that I have so far:

    for (int index = 0; index < jobQuestionnaires.Count; index++)
            Label1.Text = Label1.Text + " " + jobQuestionnaires[index]["QuestionText"];
            Label1.Text = Label1.Text + " " + answers[index]["Answer"];

In the place of Label1, add following markup

<table id="tableQuestions" runat="server" border="0" style="border-collapse:collapse;">

Modify for loop as follows

HtmlTableCell question = new HtmlTableCell();
question.InnerText = "Question & Answer";
question.Style.Add(HtmlTextWriterStyle.FontStyle, "bold");
question.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#b8d1f3");
HtmlTableCell answer = new HtmlTableCell();
//answer.InnerText = "Answer";
//answer.Style.Add(HtmlTextWriterStyle.FontStyle, "bold");
//answer.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#b8d1f3");

HtmlTableRow row = new HtmlTableRow();
row.Style.Add(HtmlTextWriterStyle.Width, "100%");

for (int index = 0; index < jobQuestionnaires.Count; index++)
    HtmlTableRow r = new HtmlTableRow();
    question = new HtmlTableCell();
    question.InnerText = jobQuestionnaires[index]["QuestionText"].ToString();
    question.Style.Add(HtmlTextWriterStyle.FontStyle, "bold");
    question.Style.Add(HtmlTextWriterStyle.Color, "gray");

    r = new HtmlTableRow();
    answer = new HtmlTableCell();
    answer.InnerText = answers[index]["Answer"].ToString();
    answer.Style.Add(HtmlTextWriterStyle.Color, "black");
  • You only need to replace label.. others are dynamic controls. – Amal Hashim Nov 5 '14 at 13:42
  • Thanks for the response! Where am I defining tblTransactions ? – Adrian Nov 5 '14 at 13:43
  • @Adrian sorry typo. Corrected the answer – Amal Hashim Nov 5 '14 at 13:44
  • Awesome. On another note, it won't let me use answer and question in the for loop because they're already defined on top. If I remove the definition in the for loop, it will only print out the first question and answer. – Adrian Nov 5 '14 at 13:46
  • @Adrian try the updated code – Amal Hashim Nov 5 '14 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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