使用复选框列表从Repeater控件批量插入

时间:2022-11-24 12:06:34

I appreciate any insight where I am going wrong in this process. Pretty new at this and have been beating my head against a wall on this one. Probably something simple.

我很欣赏在这个过程中我出错的任何见解。在这个相当新的一直在我的头撞在这一个墙上。可能很简单。

After searching to find examples, I am using a repeater that I have populated with a SqlDataSource and added a checkboxlist with static values that I am trying to get to insert into a Sql Server DB when a button is clicked from the repeater footer. It is supposed to only insert from rows that have a value for the checkboxlist for users that have been assigned a task.

在搜索查找示例之后,我使用了一个我用SqlDataSource填充的转发器,并添加了一个带有静态值的checkboxlist,当我从转发器页脚单击一个按钮时,我试图将其插入到Sql Server DB中。它应该只从具有复选框列表值的行中插入已分配任务的用户。

I am getting the following error

我收到以下错误

CS0266: Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.CheckBoxList'. An explicit conversion exists (are you missing a cast?)

If I comment out the if statement on the checkboxlist, the page will build without throwing an error, but the insert doesn't complete.

如果我在复选框列表中注释掉if语句,页面将构建而不会抛出错误,但插入操作无法完成。

I have researched the error, but don't quite get what exactly I am doing wrong when checking the checkboxlist value in the if statement to select the rows with values.

我已经研究了错误,但是在检查if语句中的checkboxlist值以选择具有值的行时,我并没有完全弄清楚我做错了什么。

Here is my code:

这是我的代码:

 <asp:Repeater ID="Repeater3" runat="server" DataSourceID="TaskAssignReviewSDS">
        <FooterTemplate>
            <asp:Button ID="Button1" runat="server" Text="Add Users" />
        </FooterTemplate>
        <ItemTemplate>
            <asp:Table ID="Table1" runat="server" Width="400px">
                <asp:TableRow>
                    <asp:TableCell Width="40%">
                        <asp:HiddenField ID="UserIdHidden" runat="server" Value='<%# Bind("UserId") %>' />
                        <asp:Label ID="UserNamelbl" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                    </asp:TableCell>
                    <asp:TableCell Width="60%">
                            <asp:CheckBoxList ID="AssignCB" RepeatDirection="Horizontal" runat="server">
                            <asp:ListItem Value="1">Assign</asp:ListItem>
                            <asp:ListItem Value="2">Review</asp:ListItem>
                        </asp:CheckBoxList>
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
        </ItemTemplate>
    </asp:Repeater>

And related on click codebehind:

点击代码隐藏相关:

 protected void Button1_Click(object sender, EventArgs e)
    {

        foreach (RepeaterItem ri in Repeater3.Items)
        {

            CheckBoxList cb = ri.FindControl("AssignCB");
            if (cb.value != null)
            {
                string TasksId = Request.QueryString["TasksId"].ToString();
                string UserId = ((HiddenField)ri.FindControl("UserIdHidden")).Value;
                string Assignation = ((TextBox)ri.FindControl("AssignCB")).Text;
                //do inserting process using above values.


                string insertSql = "INSERT INTO TaskUser(UserId,TasksId, Assignation) VALUES(@UserId, @TasksID,@Assignation) ";

                using (SqlConnection myConnection = new SqlConnection("Data Source={*MyserverIP*}\\SQLEXPRESS12;Initial Catalog=MyDBName;User ID=****;Password=*****"))
                using (SqlCommand myCommand = new SqlCommand(insertSql, myConnection))
                {
                    myConnection.Open();


                    myCommand.Parameters.AddWithValue("@TasksID", TasksId);
                    myCommand.Parameters.AddWithValue("@UserId", UserId);
                    myCommand.Parameters.AddWithValue("@Assignation", Assignation);
                    myCommand.ExecuteNonQuery();

                    myConnection.Close();
                }


            }

            }

    }

Thank you for any and all help.

感谢您的帮助。

1 个解决方案

#1


0  

You need to cast AssignCB into a CheckBoxList. FindControl() just simply returns a System.Web.UI.Control.

您需要将AssignCB转换为CheckBoxList。 FindControl()只是简单地返回一个System.Web.UI.Control。

CheckBoxList cb = (CheckBoxList)ri.FindControl("AssignCB");

#1


0  

You need to cast AssignCB into a CheckBoxList. FindControl() just simply returns a System.Web.UI.Control.

您需要将AssignCB转换为CheckBoxList。 FindControl()只是简单地返回一个System.Web.UI.Control。

CheckBoxList cb = (CheckBoxList)ri.FindControl("AssignCB");