简体   繁体   中英

Populate DropDownList in DataList in ASPX

I'm working in a Quiz Page where users can access and answers some questions. The problem I'm facing is when I want to display the possible answers of the questions. I want to display the options in a dropdownlist. I already have the questions in a datalist. Are there any ideas of how can I do it?

This is how it looks: (In the arrows that I placed is where I want to display the options) This is what users see

Here is what I have:

A FormQuiz.aspx:

 <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" href="#"> <img src="logo.png" alt="" class="auto-style1" />&nbsp; Quiz </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:Label ID="Label2" runat="server" CssClass="section"></asp:Label> <asp:DataList ID="DataList1" runat="server" CssClass="datalist"> <ItemTemplate> <div style="margin-top: 2%"> <asp:Label ID="Label2" runat="server" Text='<%#Eval("Description") %>'></asp:Label> </div> <asp:DropDownList ID="DropDownOptions" runat="server" Width="200px" AppendDataBoundItems="true" AutoPostBack="true" DataSourceID="dsOptions" DataTextField="Options" DataValueField="Id"> <asp:ListItem></asp:ListItem> </asp:DropDownList> </ItemTemplate> </asp:DataList> </asp:Panel> </div> </form> </body>

This is how I get the questions in FormQuiz.aspx.cs:

        public void SelectedSectionsTitlesQuestions(int id)
    {
        using (SqlConnection con = new SqlConnection(connectionstring))
        {
            con.Open();
            string querySection = @"SELECT Mant.Sections.Name 
                                    FROM Mant.Quizzes
                                    INNER JOIN Mant.Questions ON Mant.Quizzes.Id = Mant.Questions.Quiz_Id
                                    INNER JOIN Mant.Sections ON Mant.Questions.Section_Id = Mant.Sections.Id
                                    WHERE Mant.Quizzes.Id =" + id;
            string queryQuestion = @"SELECT Mant.Questions.Description
                                     FROM Mant.Quizzes
                                     INNER JOIN Mant.Questions ON Mant.Quizzes.Id = Mant.Questions.Quiz_Id
                                     WHERE Mant.Quizzes.Id =" + id;

            SqlDataAdapter ad2 = new SqlDataAdapter(querySection, con);
            DataSet ds2 = new DataSet();

            ad2.Fill(ds2);

            Label lblsection = (Label)FindControl("Label2");
            lblsection.Text = ds2.Tables[0].Rows[0]["Name"].ToString(); ;
            DataList datalistQuestions = (DataList)FindControl("DataList1");

            SqlDataAdapter ad = new SqlDataAdapter(queryQuestion, con);

            DataSet ds = new DataSet();
            ad.Fill(ds);
            datalistQuestions.DataSource = ds;
            datalistQuestions.DataBind();
            FillDropdown();
           
            con.Close();
        }
    }

This is how I populate the dropdown with a SqlDataSource:

        public void FillDropdown()
    {
        using (SqlConnection con = new SqlConnection(@"Server =hncrsap-sql01; Database=HEDS;User Id = sa; Password=Lear2005; MultipleActiveResultSets=true;"))
        {
            con.Open();
            List<int> Types = new List<int>(TypeQuestionsId());

                for(int x=0; x < Types.Count(); x++)
                {
                int TQuestions_Id = Types[x];
                string queryTypeQuestion = @"SELECT TypeQuestions_Id
                                                FROM Mant.Questions
                                                WHERE Quiz_Id=" + id_Quiz +
                                                "AND TypeQuestions_Id=" + TQuestions_Id;

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

                myreader = SelectCommand.ExecuteReader();
                while (myreader.Read())
                {

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

                    SqlDataSource dsOptions = new SqlDataSource();
                    dsOptions.ID = "dsOptions";
                    this.Page.Controls.Add(dsOptions);
                    dsOptions.ConnectionString = "Server =hncrsap-sql01; Database=HEDS;User Id = sa; Password=Lear2005; MultipleActiveResultSets=true;";
                    dsOptions.SelectCommand = querySelectOptions;
                }
                }

            con.Close();
        }
       // return values;
    }

But I got this error in the SelectedSectionsTitlesQuestions method

You can configure another SqlDataSource and bind it to your drop-down list.

<asp:DropDownList ID="ddlEmployees" runat="server" DataSourceID="SqlDataSource1"
    DataTextField="EmployeeName" DataValueField="EmployeeID" AppendDataBoundItems="true">
    <asp:ListItem Text="Please select" Value="" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DB_9EF896_weddingConnectionString %%>"
    SelectCommand="SELECT (FirstName + ' ' + LastName) AS EmployeeName, EmployeeID FROM Employees">
</asp:SqlDataSource>

Result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM