简体   繁体   English

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

[英]Batch Insert from Repeater control using checkboxlist

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. 在搜索找到示例之后,我使用的是一个填充有SqlDataSource的转发器,并添加了一个带有静态值的checkboxlist ,当从转发器页脚中单击按钮时,我试图将这些静态值插入到Sql Server DB中。 It is supposed to only insert from rows that have a value for the checkboxlist for users that have been assigned a task. 对于分配了任务的用户,应该只从具有复选框列表值的行中插入。

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. 感谢您提供的所有帮助。

You need to cast AssignCB into a CheckBoxList. 您需要将AssignCB转换为CheckBoxList。 FindControl() just simply returns a System.Web.UI.Control . FindControl()只是返回System.Web.UI.Control

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM