我有一个后端,管理员将在其中存储一个值假设为 10。我想动态填充 10 下拉列表并将其保存到数据库中。
这是代码,但我已经对其进行了硬编码。
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
CodeBehind="student_project_choice.aspx.cs" Inherits="Final_Year_Project_Allocation_System.student_project_choice" %>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyContent" runat="server">
<div class="well">
<div>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<td>
Choice number
</td>
<td>
Project titles
</td>
<td>
Demand
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
1
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:UomDBConnectionString1 %>"
SelectCommand="SELECT * FROM [tbl_lecturer_project]"></asp:SqlDataSource>
</td>
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblDemand" runat="server" Text=""></asp:Label>
</ContentTemplate>
<Triggers><asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged"/></Triggers>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td>
2
</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
3
</td>
<td>
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
4
</td>
<td>
<asp:DropDownList ID="DropDownList4" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
5
</td>
<td>
<asp:DropDownList ID="DropDownList5" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
6
</td>
<td>
<asp:DropDownList ID="DropDownList6" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
7
</td>
<td>
<asp:DropDownList ID="DropDownList7" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
8
</td>
<td>
<asp:DropDownList ID="DropDownList8" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
9
</td>
<td>
<asp:DropDownList ID="DropDownList9" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td>
10
</td>
<td>
<asp:DropDownList ID="DropDownList10" runat="server"
DataSourceID="SqlDataSource1" DataTextField="projectTitle"
DataValueField="lecturerProjectId">
</asp:DropDownList>
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
<div class="control-group controls">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btn btn-primary" />
<input id="Reset1" type="reset" value="reset" class="btn btn-primary" />
</div>
</div>
</fieldset>
</asp:Content>
请给我一些想法,我该如何实现这一目标。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace Final_Year_Project_Allocation_System
{
public partial class student_project_choice : System.Web.UI.Page
{
private string connectionString = WebConfigurationManager.ConnectionStrings["UomDBConnectionString1"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string selected = DropDownList1.SelectedItem.Value;
string selectSQL = "select Count(choiceNumber) from tbl_student_project_choice where choiceProjectId = '" + selected + "' and choiceNumber='"+1+"'";
// Define the ADO.NET objects.
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand(selectSQL, con);
int result = (int)cmd.ExecuteScalar();
lblDemand.Text = result.ToString();
}
catch (Exception exce)
{
lblMessage.Text = exce.Message;
}
}
}
}