C#ASP.NET复选框在数据库中无法更新

时间:2015-11-04 13:38:19

标签: c# asp.net listview checkbox webforms

似乎无法将我的复选框从前端代码连接到我的后端代码。我想只更新数据库中的复选框。

这里是SQL源的前端代码和Listview的结尾

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
 SelectCommand="SELECT Create_Project.ProjectID, Create_Project.ProjectName, Create_Project.ProjectStartDate, Create_Project.ProjectEndDate, Offers.OfferID, Offers.UserID, Offers.Accept, Offers.Decline 
                FROM Create_Project 
                INNER JOIN Offers ON Create_Project.ProjectID = Offers.ProjectID 
                WHERE (Offers.UserID = @UserID) 
                ORDER BY Create_Project.ProjectStartDate DESC">
                <SelectParameters>
                    <asp:QueryStringParameter Name="UserID" QueryStringField="searchID" />
                </SelectParameters>
            </asp:SqlDataSource>


            <asp:ListView ID="ListView1" runat="server" DataKeyNames="ProjectID,OfferID" DataSourceID="SqlDataSource1">
                <AlternatingItemTemplate>
                </AlternatingItemTemplate>
                <EditItemTemplate>
                </EditItemTemplate>
                <EmptyDataTemplate>
                    <table runat="server" style="">
                        <tr>
                            <td>No data was returned.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <InsertItemTemplate>
                </InsertItemTemplate>
                <ItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Label ID="ProjectIDLabel" runat="server" Text='<%# Eval("ProjectID") %>' />
                        </td>
                        <td>
                            <asp:Label ID="ProjectNameLabel" runat="server" Text='<%# Eval("ProjectName") %>' />
                        </td>
                        <td>
                            <asp:Label ID="ProjectStartDateLabel" runat="server" Text='<%# Eval("ProjectStartDate") %>' />
                        </td>
                        <td>
                            <asp:Label ID="ProjectEndDateLabel" runat="server" Text='<%# Eval("ProjectEndDate") %>' />
                        </td>
                        <td>
                            <asp:Label ID="OfferIDLabel" runat="server" Text='<%# Eval("OfferID") %>' />
                        </td>
                        <td>
                            <asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
                        </td>
                        <td>
                            <asp:CheckBox ID="AcceptCheckBox" runat="server" Text="Accept"/>
                        </td>
                        <td>
                            <asp:CheckBox ID="DeclineCheckBox" runat="server" Text="Decline"/>
                        </td>
                        <td>
                        <asp:Button ID="Button1"  runat="server" Text="Save" class="btn btn-success" OnClick="Button1_Click" />
                        </td>
                    </tr>
                </ItemTemplate>
                <LayoutTemplate>
                    <table runat="server">
                        <tr runat="server">
                            <td runat="server">
                                <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                    <tr runat="server" style="">
                                        <th runat="server">ProjectID</th>
                                        <th runat="server">ProjectName</th>
                                        <th runat="server">ProjectStartDate</th>
                                        <th runat="server">ProjectEndDate</th>
                                        <th runat="server">OfferID</th>
                                        <th runat="server">UserID</th>
                                        <th runat="server">Accept</th>
                                        <th runat="server">Decline</th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr runat="server">
                            <td runat="server" style=""></td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <SelectedItemTemplate>
                </SelectedItemTemplate>
            </asp:ListView>

后端

protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            conn.Open();
            {
                bool Accept = false;
                bool Decline = false;

                foreach (ListViewDataItem item in Listview1.Items)
                {
                    CheckBox findcheckbox = (CheckBox)item.FindControl("AcceptCheckbox");

                    if (findcheckbox.Checked)
                    {
                        Accept = true;
                        Decline = false;
                    }
                    else
                    {
                        Accept = false;
                        Decline = true;
                    }
                }
                string selectString = "UPDATE Offers SET (Accept, Decline VALUES ('" + Accept + "', '" + Decline + "')";
                SqlCommand usersql = new SqlCommand(selectString, conn);
                usersql.ExecuteNonQuery();
                Response.Write("<script type='text/javascript'>");
                Response.Write("alert('You have made your decision');");
                Response.Write("document.location.href='Offers.aspx';");
                Response.Write("</script>");


            }
        }

2 个答案:

答案 0 :(得分:1)

首先你应该考虑使用SqlParameters而不是将你的参数放在一个字符串中来查询以避免sql注入。

其次,UPDATE不遵循INTO,只是UPDATE OFFERS SET ACCEPT = @YOURVALUE, DECLINE = @YOUROTHERVALUE,您应该考虑使用WHERE子句,否则,它会更新您的所有记录。

答案 1 :(得分:1)

您需要以这种方式使用 findcontrol

 CheckBox findcheckbox = (CheckBox)Listview1.FindControl("AcceptCheckbox");

现在在检查条件时,您需要使用findcheckbox

if(findcheckbox.Checked)
{
//your code
}

<强> EDIT1:

基于评论的输入这里是Button_click代码

    protected void Button1_Click(object sender, EventArgs e) {
     try
     {


         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
         conn.Open();
         {
             bool Accept = false;
             bool Decline = false;
             foreach (ListViewDataItem item in ListView1.Items)
             {
                 CheckBox findcheckbox = (CheckBox)item.FindControl("AcceptCheckbox");

                 if (findcheckbox.Checked)
                 {
                     Accept = true;
                     Decline = false;
                 }
                 else
                 {
                     Accept = false;
                     Decline = true;

                 }
             }
             string selectString = "UPDATE Offers SET (Accept, Decline VALUES ('" + Accept + "', '" + Decline + "')";
             SqlCommand usersql = new SqlCommand(selectString, conn);
             usersql.ExecuteNonQuery();
             Response.Write("<script type='text/javascript'>");
             Response.Write("alert('You have made your decision');");
             Response.Write("document.location.href='Offers.aspx';");
             Response.Write("</script>");


         }
     }
     catch(Exception ex)
     {
         throw ex;
     }
    }