Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table Dinamically Created

Status
Not open for further replies.

Gixonita

IS-IT--Management
Oct 17, 2005
103
0
0
US
Hi experts :)

I'm creating a little website to train our employees, it goes a little like this:

- Employee logs in (Done)
- A list of available presentations is shown (Done)
- Employee selects presentation and goes through it (Done)
- Employee takes a test and gets scored (Partly Done, problem)

After the presentation is taken I dynamically fill out a table that has 2 columns, the first one has the question, the second one has a Radio Button List with multiple choice answers. To present this I have an empty "marker" in my html for the table like
Code:
<asp:Table ID="Table_Questions" runat="server" BorderColor="#400040" GridLines="Both">
                    </asp:Table>
<asp:Button ID="Button_ScoreTest" runat="server" Height="43px" Text="Calculate Score"
                            Width="181px" OnClick="Button_ScoreTest_Click" />

then on the code behind I generate all the rows and columns
Code:
protected void Button_StartTest_Click(object sender, EventArgs e)
        {
            PresentationData presentationData;
            presentationData = (PresentationData)Session["PresentationData"];

            training_results.Visible = false;
            test.Visible = true;

            Label_Welcome.Text = (string)Session["EmployeeName"] + ", please answer the questions. No cheating !!";

            SqlDataReader QuestionsAndAnswers;

            if ((QuestionsAndAnswers = Constants.GetQuestionsAndAnswers(presentationData.Presentation_ID)).HasRows)
            {
                Table TestTable = new Table();

                TestTable.EnableViewState = true;

                TableHeaderRow HeaderRow = new TableHeaderRow();
                HeaderRow.Width = Unit.Parse("700px");
                HeaderRow.BorderColor = System.Drawing.Color.FromName("#400040");
                HeaderRow.EnableViewState = true;
                
                TableHeaderCell HeaderQuestion = new TableHeaderCell();
                HeaderQuestion.Width = Unit.Parse("350px");
                HeaderQuestion.BorderColor = System.Drawing.Color.FromName("#400040");
                HeaderQuestion.BackColor = System.Drawing.Color.FromName("#00C000");
                HeaderQuestion.EnableViewState = true;
                HeaderQuestion.Text = "QUESTIONS";

                HeaderRow.Cells.Add(HeaderQuestion);

                TableHeaderCell HeaderAnswer = new TableHeaderCell();
                HeaderAnswer.Width = Unit.Parse("350px");
                HeaderAnswer.BorderColor = System.Drawing.Color.FromName("#400040");
                HeaderAnswer.BackColor = System.Drawing.Color.FromName("#00C000");
                HeaderAnswer.EnableViewState = true;
                HeaderAnswer.Text = "ANSWERS";
                
                HeaderRow.Cells.Add(HeaderAnswer);

                Table_Questions.Rows.Add(HeaderRow);

                bool Loop = true;

                QuestionsAndAnswers.Read();

                string Question;

                bool More_Answers;
                
                while (Loop)
                {
                    Question = (string)QuestionsAndAnswers["Question"];

                    TableRow QuestionRow = new TableRow();

                    QuestionRow.Width = Unit.Parse("700px");
                    QuestionRow.BorderColor = System.Drawing.Color.FromName("#400040");
                    QuestionRow.EnableViewState = true;

                    TableCell QuestionCell = new TableCell();

                    QuestionCell.Width = Unit.Parse("350px");
                    QuestionCell.BorderColor = System.Drawing.Color.FromName("#400040");
                    QuestionCell.EnableViewState = true;
                    QuestionCell.Text = Question;

                    QuestionRow.Cells.Add(QuestionCell);

                    RadioButtonList AnswersRadioButtonList = new RadioButtonList();
                    AnswersRadioButtonList.EnableViewState = true;

                    More_Answers = true;

                    while (More_Answers)
                    {
                        ListItem AnswerListItem = new ListItem();

                        AnswerListItem.Text = (string)QuestionsAndAnswers["Answer"];
                        AnswerListItem.Value = ((int)QuestionsAndAnswers["Score_Value"]).ToString();
                        
                        AnswersRadioButtonList.Items.Add(AnswerListItem);

                        Loop = QuestionsAndAnswers.Read();

                        if (Loop)
                        {
                            if (Question != (string)QuestionsAndAnswers["Question"])
                                More_Answers = false;
                        }
                        else
                            More_Answers = false;
                    }

                    TableCell AnswerCell = new TableCell();

                    AnswerCell.Width = Unit.Parse("350px");
                    AnswerCell.BorderColor = System.Drawing.Color.FromName("#400040");
                    AnswerCell.EnableViewState = true;
                    AnswerCell.Controls.Add(AnswersRadioButtonList);
                    
                    QuestionRow.Cells.Add(AnswerCell);

                    Table_Questions.Rows.Add(QuestionRow);
                }
                Session["Table_Questions"] = Table_Questions;
            }
            else
            {
                Label_Error.Text = "Error retreiving test. Please contatc Eileen Carlan.";
                Label_Error.Visible = true;
            }
        }

this works beautifully, the test is displayed on screen and the employee can go through the questions marking the answers.

The problem comes when the employee clicks on the button to score the test, ALL the dynamic content that I created disappears and I'm left with an empty table.

Does anyone know why this is happening? I'm using visual studio 2005 and c#.

I also tried saving the table in a session variable, and the table and its contents get saved perfectly but the only things that dont get saved are the selections the employee made, this I cant understand either, everything gets saved (rows, columns, radio button list, etc), except the selections, the property "selected" stays false for everything.

Any help will be appreciated :)

Luis Torres

PS the next piece of code would be:

Code:
protected void Button_ScoreTest_Click(object sender, EventArgs e)
        {
            int Score = 0;

            Table_Questions = (Table)Session["Table_Questions"];

            TableRowCollection Question_Rows = Table_Questions.Rows;

            for (int i = 1; i < Question_Rows.Count; i++)
            {
                RadioButtonList Question_RadioButton = (RadioButtonList)Question_Rows[i].Cells[1].Controls[0];

                for (int j = 0; j < Question_RadioButton.Items.Count; j++)
                    if (Question_RadioButton.Items[j].Selected)
                        Score += Int32.Parse(Question_RadioButton.Items[j].Value);
            }

            Response.Write(Score);
        }
 
dynamic content must be created with each request to the server, and it should be created in the init event.
if you assign ids to the controls dynamically added, then they survive postback.

it may be easier to navigate to a "take test" page after the presentation. in this page override OnInit and dynamically create the table.

depending on how many questionares there are you may have a easier time creating a web user control for each test. then dyanamically load the user control on the test page.
building entire dom heirachies in code behind is working harder not smarter with asp.net.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top