[英]Populate DropDownList in DataList in ASPX
我正在一個測驗頁面中工作,用戶可以訪問並回答一些問題。 我面臨的問題是當我想顯示問題的可能答案時。 我想在下拉列表中顯示選項。 我已經在數據列表中有問題。 有什么想法我該怎么做?
這是它的外觀:(在我放置的箭頭中是我想要顯示選項的位置)這是用戶看到的
這是我所擁有的:
一個 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" /> 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 方法中遇到了這個錯誤
您可以配置另一個 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.