如果在gridview中选中复选框,则将日期添加到数据库

时间:2017-10-15 13:12:13

标签: asp.net gridview checkbox

我有一个订购系统,显示已收到的订单以及未完成的订单。如果收到订单并且然后选中了复选框,我想在收到订单时将日期添加到数据库中。显然,我只想将日期添加到已检查的行中。提前谢谢。

相关HTML如下:

                            <asp:GridView ID="gvOpenOrders" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="4" ForeColor="#333333"
                                GridLines="None" DataKeyNames="PurchaseOrderID" AllowPaging="True" AllowSorting="True"
                                OnSorting="gvOpenOrders_Sorting" EnableViewState="true" OnPageIndexChanging="OnPageIndexChanging" PageSize="15" ViewStateMode="Enabled">
                                <AlternatingRowStyle BackColor="White" />
                                <Columns>
                                    <asp:TemplateField HeaderText="Is Received" SortExpression="IsReceived">
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkRcvd" runat="server" AutoPostBack="true" Checked='<%# Bind("IsReceived") %>' Enabled='<%# Eval("IsReceived") = False %>' OnCheckedChanged="chkRcvd_CheckedChanged" />
                                        </ItemTemplate>
                                    </asp:TemplateField>

我正在更新数据库时单击“更新已接收”按钮,请参见下文:

Protected Sub btnUpdateReceived_Click(sender As Object, e As EventArgs) Handles btnUpdateReceived.Click

    con.Open()
    For Each row As GridViewRow In gvOpenOrders.Rows
        Dim purchaseOrderID As Integer = Convert.ToInt32(gvOpenOrders.DataKeys(row.RowIndex).Values(0))
        Dim isReceived As Boolean = TryCast(row.FindControl("chkRcvd"), CheckBox).Checked

        cmd.CommandText = "usp_UpdatePurchaseOrder"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Clear()
        cmd.Parameters.AddWithValue("@OrderID", purchaseOrderID)
        cmd.Parameters.AddWithValue("@IsReceived", isReceived)
        cmd.ExecuteNonQuery()
    Next
    con.Close()
    Response.Redirect(Request.Url.AbsoluteUri)

End Sub

我知道有一个checkbox_CheckedChanged事件,但我不确定如何使用它。

3 个答案:

答案 0 :(得分:0)

您可以在CheckedChanged事件中添加此代码:

CheckBox chkSelect = (CheckBox)sender;

if (chkSelect.Checked)
{
   string date = DateTime.Today.ToString("dd-MM-yyyy");
}

希望可以将其转换为VB的代码。

答案 1 :(得分:0)

DELIMITER //
CREATE DEFINER=`mikrof`@`%` PROCEDURE `savingRelated`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
SELECT SUM(IF(`created_at` < '2017-09-30', 1, 0)) AS MembersInLastMonth,
     SUM(IF(`created_at` between '2017-10-01'AND '2017-10-15', 1, 0)) AS MembersInCurrentMonth,
     SUM(IF(`created_at` <= '2017-10-30', 1, 0)) AS gg,
     SUM(IF(((`IsActive`=0) && (`created_at` between '2017-10-01'AND '2017-10-15')), 1, 0)) AS MembersCancelInCurrentMonth FROM members;
END//
DELIMITER ;

答案 2 :(得分:0)

我认为只有一个修改可以找到您的解决方案。 试试这个:

string strDate=stirng.Empty;
foreach (GridViewRow rw in grid1.Rows)
      {
          CheckBox chkBx = (CheckBox)rw.FindControl("Check");
          if (chkBx != null && chkBx.Checked)
          {              
              strDate = ((Label)rw.FindControl("lblDate")).Text; 
         }
      }