【发布时间】:2015-11-18 22:52:42
【问题描述】:
我在学生门户网站上有一个注册向导,学生可以在其中选择每个学年的模块。他们可以选择模块,每个模块都带有一定数量的分数,学生被限制在一定数量的分数上,具体取决于他们的课程。我有下面的 asp.net 代码,其中包含该学年所有可用模块的CheckBoxListmodule_choice。
<asp:View ID="semester_1_view" runat="server">
<p>CATS points Required:</p>
<asp:DropDownList ID="cat_points_total" runat="server" DataSourceID="semester"
DataTextField="cats_points_total" DataValueField="cats_points_total"
Visible="true"></asp:DropDownList>
<asp:CheckBoxList ID="module_choice" runat="server" DataSourceID="semester"
DataTextField="module_name" DataValueField="cats_points"></asp:CheckBoxList>
<asp:Label ID="sem_1_fb" runat="server" Text=""></asp:Label>
<asp:Button ID="sem_1_cat" runat="server" Text="Test" OnClick="sem_1_cat_Click" />
<asp:Button CommandName="NextView" ID="btnnext2" runat="server" Text="Next"
OnClick="btnnext2_Click" />
<asp:Button ID="Button1" runat="server" CommandName="PrevView" Text="Previous"
CssClass="submitbtn" />
</asp:View>
目前这只通过cats_points我还想要一些方法来传递所选项目的module_id。
然后我有以下 C#,它计算所选猫的总点数,并提供反馈,选择的总分是否等于所需的总分。
protected void sem_1_cat_Click(object sender, EventArgs e)
{
int catselected = 0;
for (int i = 0; i < module_choice.Items.Count; i++)
{
if (module_choice.Items[i].Selected)
{
string value = module_choice.Items[i].Value;
catselected += int.Parse(value);
}
}
int cattotal = int.Parse(cat_points_total.SelectedValue);
int catcalc = cattotal - catselected;
int zero = 0;
if (catcalc != zero)
{
sem_1_fb.Visible = true;
sem_1_fb.Text = "<b> Not Enough Points</b> cattotal =" + cattotal + " catselected =" + catselected + " catcalc =" + catcalc +".";
}
else
{
sem_1_fb.Visible = true;
sem_1_fb.Text = "<b>Point Criteria Met</b> cattotal =" + cattotal + " catselected =" + catselected + " catcalc =" + catcalc +".";
}
}
我希望能够检查总数是否正确,如果它是从每个选择中插入选定的 module_id 到数据库中,但显然目前我没有 @987654328 @信息这样做只有cats_points;
int count = module_choice.Items.Count;
for (int i = 0; i < count; i++)
{
if (moduleselect.Items[i].Selected)
{
string value = module_choice.Items[i].Value;
String query = "INSERT INTO students_vs_modules (user_id, module_id)
VALUES (@user_id, @module_id)";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@user_id", user);
myCommand.Parameters.AddWithValue("@module_id", value);
myCommand.ExecuteNonQuery();
}
}
任何帮助将不胜感激,因为我是 C# 的新手
编辑
这是我用来填充module_choice CheckBoxList 的数据源
<asp:SqlDataSource ID="semester" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT modules.module_id, modules.module_name, modules.module_tutor,
staff_records.f_name, staff_records.l_name, modules.compulsory, modules.year,
modules.cats_points, modules.description, courses.course_name,
student_records.user_id, courses.cats_points_total
FROM modules
INNER JOIN courses_vs_modules ON modules.module_id = courses_vs_modules.module_id
INNER JOIN staff_records ON modules.module_tutor = staff_records.user_id
INNER JOIN courses ON courses_vs_modules.course_id = courses.course_id
INNER JOIN student_records ON courses.course_id = student_records.course_id
WHERE (student_records.user_id = @user_id)"
InsertCommand="INSERT INTO [students_vs_modules]([user_id], [module_id])
VALUES (@user_id, @module_id)">
<InsertParameters>
<asp:Parameter Name="user_id" Type="Int32" />
<asp:Parameter Name="module_id" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter QueryStringField="user_id" Name="user_id" Type="Int32"></asp:QueryStringParameter>
</SelectParameters>
</asp:SqlDataSource>
【问题讨论】:
-
你是如何填充 module_choice 复选框列表的?
-
@DinoMyte 我已经用我用来填充
module_choice复选框列表的数据源更新了我的问题。 -
谢谢。上下文中的 module_id 是什么?
-
@DinoMyte 在值中?将有多个选择,因此会有多个值,它们都是从 2000 开始的 4 位整数,并且每次插入都会自动递增。还是您的意思是 module_id 的用途?因为它是
modules表中的唯一主键,也用于通过查找表链接其他表,例如students_vs_modules将学生链接到模块。我希望这会有所帮助。