Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a Quiz App where users answers some questions with a dropdownlist. The problem I have is that I can't get the selectedvalue from each dropdown. I create the dropdowns dynamically. Do someone have an idea of how I can get the values?

Here is what I have:

FormQuiz.aspx
ASP.NET
<body style="background-color: #0f5298">
    <form id="form1" runat="server">
        <nav class="auto-style2" style="background-color: #d5f3fe">
            <div class="auto-style3">
                <a class="navbar-brand">
                    <img src="logo.png" alt="" class="auto-style1" /> 
                    HEDS
                </a>
            </div>
        </nav>
        <div class="card">
            <asp:Label ID="LabelName" runat="server" Text="Seleccione un Quiz:" CssClass="lbl"></asp:Label>
            <asp:DropDownList ID="DropDownList1" runat="server" CssClass="ddl" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
            <asp:ListItem Text="--- Seleccione ----" Value=" " />
            </asp:DropDownList>
            <asp:Label ID="Label1" runat="server" Text="Instrucciones" CssClass="lbl"></asp:Label>
            <asp:Label ID="LabelInstrucciones" runat="server" CssClass="I"></asp:Label>
            <asp:Panel ID="Panel1" runat="server" CssClass="panelQ">
            </asp:Panel> 
            <asp:Button ID="ButtonSubmit" runat="server" OnClick="ButtonSubmit_Click" Text="Submit" Width="150px"/>
        </div>
    </form>
</body>


FormQuiz.aspx.cs This is how I create the questions and their options:
C#
public void GetQuestionsAndOptions()
{
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        DataSet TypeQuestions = new DataSet();
        DataSet Options = new DataSet();
        DataSet Description = new DataSet();

        string questionsDescription = @"SELECT Id, Description FROM MANT.Questions 
                                        WHERE Quiz_Id=" + id_Quiz;
        string queryOptions = @"SELECT TypeQuestions_Id FROM Mant.AnswerOptions";
        string queryQuestion = @"SELECT Mant.Questions.TypeQuestions_Id
                                FROM Mant.Quizzes
                                INNER JOIN Mant.Questions ON Mant.Quizzes.Id = Mant.Questions.Quiz_Id
                                WHERE Mant.Quizzes.Id =" + id_Quiz;

        SqlCommand commandQueryOptions = new SqlCommand(queryOptions, con);
        SqlCommand commandQueryQuestion = new SqlCommand(queryQuestion, con);
        SqlCommand commandQueryDescription = new SqlCommand(questionsDescription, con);

        SqlDataAdapter adapterOptions = new SqlDataAdapter(commandQueryOptions);
        SqlDataAdapter adapterQuestion = new SqlDataAdapter(commandQueryQuestion);
        SqlDataAdapter adapterDescription = new SqlDataAdapter(commandQueryDescription);
        adapterOptions.Fill(Options);
        adapterQuestion.Fill(TypeQuestions);
        adapterDescription.Fill(Description);

        Panel panel = (FindControl("Panel1") as Panel);

        int c = 0;

        foreach (DataRow dataRow in Description.Tables[0].Rows)
        {
            string query = @"SELECT Mant.Questions.TypeQuestions_Id
                                FROM Mant.Quizzes
                                INNER JOIN Mant.Questions ON Mant.Quizzes.Id = Mant.Questions.Quiz_Id
                                WHERE Mant.Quizzes.Id =" + id_Quiz +
                                "AND Mant.Questions.Id=" + Lista[counter];

            SqlCommand SelectCommand = new SqlCommand(query, con);
            SqlDataReader myreader;

            con.Open();
            myreader = SelectCommand.ExecuteReader();
            while (myreader.Read())
            {
                id = Int32.Parse(myreader[0].ToString());
            }
            con.Close();

            string newquery = @"SELECT Options, Id 
                                FROM Mant.AnswerOptions
                                WHERE TypeQuestions_Id=" + id;

            Label labelQuestion = new Label();
            labelQuestion.CssClass = "question";
            labelQuestion.Text = Description.Tables[0].Rows[c]["Description"].ToString();
            labelQuestion.ID = Description.Tables[0].Rows[c]["Id"].ToString();
            int actualQuestionID = Int32.Parse(labelQuestion.ID.ToString());

            string querySection = @"SELECT Section_Id 
                                    FROM MANT.Questions 
                                     WHERE Id=" + actualQuestionID;
            int sectionId = 0;
            SqlCommand cmd = new SqlCommand(querySection, con);
            SqlDataReader myrdr;

            con.Open();
            myrdr = cmd.ExecuteReader();
            while (myrdr.Read())
            {
                sectionId = Int32.Parse(myrdr[0].ToString());
            }
            con.Close();

            DropDownList ddl = new DropDownList();
            ddl.CssClass = "ddl";
            SqlCommand comm = new SqlCommand(newquery, con);
            SqlDataAdapter adapter = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            ddl.DataSource = ds;
            ddl.ID = "ddldynamic" + c;
            ddl.DataTextField = "Options";
            ddl.DataValueField = "Id";
            ddl.DataBind();
            ds.Clear();
            ddl.Items.Insert(0, new ListItem("Please select"));

            Label lblsection = new Label();
            lblsection.Text = GetSectionName(sectionId);
            lblsection.CssClass = "section";

            Panel newPanel = new Panel();

            if (String.Equals(lblsection.Text, sectionName) != true)
            {
                Panel sectionPanel = new Panel();
                sectionPanel.Controls.Add(lblsection);
                panel.Controls.Add(sectionPanel);
                sectionName = lblsection.Text;
            }
            panel.Controls.Add(newPanel);
            panel.Controls.Add(labelQuestion);
            panel.Controls.Add(ddl);
            c++;
            counter++;

        }
    }
}


What I have tried:

I have tried using a View State but I really don't know how to use it.
Posted
Updated 10-Jun-21 4:32am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900