【问题标题】:Populate DropDownList in DataList in ASPX在 ASPX 中的 DataList 中填充 DropDownList
【发布时间】:2021-08-20 17:48:24
【问题描述】:

我正在一个测验页面中工作,用户可以访问并回答一些问题。我面临的问题是当我想显示问题的可能答案时。我想在下拉列表中显示选项。我已经在数据列表中有问题。对我该怎么做有什么想法吗?

这是它的外观: (在我放置的箭头中是我要显示选项的位置) This is what users see

这是我所拥有的:

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>

这就是我在 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();
        }
    }

这就是我使用 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;
    }

但我在 SelectedSectionsTitlesQuestions 方法中得到了这个 error

【问题讨论】:

    标签: c# asp.net asp.net-core-webapi


    【解决方案1】:

    您可以配置另一个 SqlDataSource 并将其绑定到您的下拉列表。

    <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

    【讨论】:

    • 我正在尝试正确知道。我从后面的代码中执行了 SqlDataSource,因为我需要所选测验的值,但出现以下错误:System.Web.HttpException: 'The DataSourceID of 'DropDownOptions' must be the ID of a control of type IDataSource .找不到 ID 为“dsOptions”的控件。'
    • 可以添加 asp: DropDownList runat =server DataSourceID ="dsOptions" />。SqlDataSource的使用可以参考官方文档:docs.microsoft.com/en-us/dotnet/api/…
    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多