更新 CheckBoxList 时对象类型 System.Collections.Generic.List 不存在映射

No mapping exists from object type System.Collections.Generic.List while updating CheckBoxList

我正在尝试从 GridView 更新 CheckBoxList。但是我遇到了上面提到的错误。

我在这个表格的代码中还有其他字段,如姓名、性别、年龄、部门,但为了方便大家,我已经删除了那些不相关的代码。

以下是 GridView 的代码

 <div>
            <asp:GridView ID="GridView1" class="table table-striped table-bordered" runat="server" Width="603px" DataKeyNames="Student_ID" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False" HorizontalAlign="Center" > <%--OnRowDataBound="GridView1_RowDataBound"--%>
                <Columns>
                    <asp:TemplateField HeaderText="Student ID">
                        <EditItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Eval("Student_ID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Student_ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Subjects">
                        <EditItemTemplate>
                         <asp:CheckBoxList ID="CheckBoxList1" runat="server"  RepeatDirection="Horizontal" SelectedValue='<%# Eval("SUbjects") %>' > <%--OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged--%>
                            <asp:ListItem Value="Physics">Physics</asp:ListItem>
                            <asp:ListItem Value="Chemistry">Chemistry</asp:ListItem>
                            <asp:ListItem Value="Biology">Biology</asp:ListItem>
                        </asp:CheckBoxList >
              
                           
                  
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:CheckBoxList ID="CheckBoxList2" runat="server"  RepeatDirection="Horizontal" SelectedValue='<%# Eval("SUbjects") %>' > <%--OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged--%>
                            <asp:ListItem Value="Physics">Physics</asp:ListItem>
                            <asp:ListItem Value="Chemistry">Chemistry</asp:ListItem>
                            <asp:ListItem Value="Biology">Biology</asp:ListItem>
                        </asp:CheckBoxList >
              
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField HeaderText="Delete" ShowDeleteButton="True"/>
                    <asp:CommandField HeaderText="Edit" ShowEditButton="True" ValidationGroup="update" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>

这是 GridView 行更新的代码

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {

     int studentid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
     CheckBoxList subjects = ((CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("CheckBoxList1")) as CheckBoxList;

 List <string>  studentsubjects = new List <string>();
 foreach (ListItem item in subjects.Items)
 {
    if (item.Selected)
    {
        studentsubjects.Add(item.Text);
    }
 }



     SqlConnection conn = new SqlConnection("Data Source=WINCTRL-0938L38; Database=dbUni; Integrated Security=true");
     conn.Open();
     SqlCommand cmd = new SqlCommand("StudentUpdate", conn);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.AddWithValue("@student_id ", studentid);
     cmd.Parameters.AddWithValue("@subjects ", studentsubjects);
     cmd.ExecuteNonQuery();
     GridView1.EditIndex = -1;
     FillGrid();
     conn.Close();
 }

正在更新复选框列表

No mapping exist occurs 因为你正在更新一个 List (studentsubjects),你必须把它转换成字符串或者使用一个 sublist 在下面的代码中已经是一个字符串:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int studentid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        CheckBoxList subjects = (CheckBoxList)GridView1.Rows[e.RowIndex].FindControl("CheckBoxList1");

        List<string> studentsubjects = new List<string>();

        string sublist = "";

        foreach (ListItem item in subjects.Items)
        {
            if (item.Selected)
            {
                studentsubjects.Add(item.Text);
            }
        }

        sublist = string.Join(",", studentsubjects); // add , inside subjects names

        SqlConnection conn = new SqlConnection("Data Source=WINCTRL-0938L38; Database=dbUni; Integrated Security=true");
        conn.Open();
        SqlCommand cmd = new SqlCommand("StudentUpdate", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@student_id ", studentid);
        cmd.Parameters.AddWithValue("@subjects ", sublist);
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        FillGrid();
        conn.Close();
    }

已编辑 2:checkboxlist 中删除此 属性 SelectedValue='<%# Eval("SUbjects") %>' 并添加一个 Label 如:

<EditItemTemplate>
    <asp:Label ID="lblSubjects" Visible="false" runat="server" Text='<%# Eval("Subjects") %>'></asp:Label>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">
        <%--OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged--%>
        <asp:ListItem Value="Physics">Physics</asp:ListItem>
        <asp:ListItem Value="Chemistry">Chemistry</asp:ListItem>
        <asp:ListItem Value="Biology">Biology</asp:ListItem>
    </asp:CheckBoxList>
</EditItemTemplate>

RowDataBound 事件: 从数据库绑定您的 checkboslist

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                CheckBoxList chklist = ((CheckBoxList)e.Row.FindControl("CheckBoxList1"));
                string subjects = ((Label)e.Row.FindControl("lblSubjects")).Text;

                List<string> studentsubjects = subjects.Split(',').ToList();

                foreach (string item in studentsubjects)
                {
                    if (item == "Physics")
                        chklist.Items.FindByText("Physics").Selected = true;
                    else if (item == "Chemistry")
                        chklist.Items.FindByText("Chemistry").Selected = true;
                    else
                        chklist.Items.FindByText("Biology").Selected = true;
                }
            }
        }
    }

注意:不要忘记在GrindView中添加OnRowDataBound事件<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >