【问题标题】:Method of Passing Two values in CheckBoxList selections在 CheckBoxList 选择中传递两个值的方法
【发布时间】: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 将学生链接到模块。我希望这会有所帮助。

标签: c# asp.net webforms


【解决方案1】:

如果我正确理解了您的问题,您希望为 CheckBoxList 中列出的每个 module_name 存储 module_idcat_points 并选择您想要获取所选 module_idcat_points 的复选框.

开箱即用的CheckBoxList 有一个DataTextFieldDataValueField(表示要显示的文本和相应的值)。您可以扩展 CheckBoxList 控件以保存附加值。

然而,最简单的选择是从数据源返回带有分隔符的连接值并将其用作DataValueField。现在,当您处理时,您可以拆分所选值并获取单独的值(在 sem_1_cat_Click 事件下)

在您的情况下,cats_pointsmodule_id 可以组合成一个名为 mod_id_catpoints 的字段(例如,可以是 23**345,其中 ** 是分隔符)

在这种情况下,mod_id_catpoints 将是您的DataValueField

sem_1_cat_Click event 下,您可以将23**345 拆分为23345,其中23 将是module_id245 将是cat_points

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多