我有一个网格视图,其中包含一个复选框列和另一列信息。检查或取消选中后,我想根据复选框的内容更新数据库。但是,单击按钮后,似乎已删除Checkbox选中的属性值,并在onClick事件发生之前返回其原始状态,并且数据库永远不会更新。
ASPX
<div>
<asp:gridview runat="server" id="MeasuresGrid" autogeneratecolumns="False" cssclass="SelectionHeader" onrowdatabound="MeasuresGrid_OnRowDataBound" datakeynames="SubSectionId">
<AlternatingRowStyle BackColor="#cce1ec"></AlternatingRowStyle>
<Columns>
<asp:TemplateField HeaderText="Applicable Measures?" ItemStyle-Width="5%" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:CheckBox ID="MeasureChckBox" CommandName="CheckBox" Checked='<%# Eval("IsSelected") %>' CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Measure" DataField="SubSection">
</asp:BoundField>
</Columns>
</asp:gridview>
</div>
<asp:updatepanel runat="server">
<ContentTemplate>
<table style="width: 100%">
<tr>
<td style="width: 50%; text-align: left">
<asp:button id="btnCancel" runat="server" text="Cancel" cssclass="hrsaSkingreybtn" />
</td>
<td style="width: 50%; text-align: right">
<asp:button id="btnSave" runat="server" text="Save" cssclass="hrsaSkingreybtn" OnClick="btnSave_OnClick" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:updatepanel>
ASPX.cs
protected void MeasuresGrid_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var measureChckBox = (e.Row.FindControl("MeasureChckBox") as CheckBox);
foreach (var x in MeasurePrefixArray)
{
if (e.Row.Cells[1].Text.StartsWith(x))
{
if (measureChckBox != null)
{
measureChckBox.Checked = true;
measureChckBox.Enabled = false;
}
}
}
}
}
protected void btnSave_OnClick(object sender, EventArgs e)
{
foreach (GridViewRow row in MeasuresGrid.Rows)
{
try
{
//Retrieve isSelected value
var selectQuery =
@"select IsSelected from MHF.FlexMeasureSelection where RptInstanceId = @ReportInstanceId and SubSectionId = @SubSectionId";
var dataKey = MeasuresGrid.DataKeys[row.RowIndex];
if (dataKey != null)
{
if (dataKey.Value != null)
{
subSectionId = Convert.ToInt32(dataKey.Value);
}
}
if (conn.State == ConnectionState.Closed)
conn = DbAgent.selfRef().getConnection("default");
var command = new SqlCommand(selectQuery, conn)
{
CommandType = CommandType.Text
};
command.Parameters.AddWithValue("@ReportInstanceId", apSess.ReportInstanceID);
command.Parameters.AddWithValue("@SubSectionId", subSectionId);
var isSelected = Convert.ToInt32(command.ExecuteScalar());
var chk = (CheckBox) row.Cells[0].FindControl("MeasureChckBox");
var updateQuery =
@"update MHF.FlexMeasureSelection set IsSelected = @IsSelected where RptInstanceId = @ReportInstanceId and SubSectionId = @SubSectionId";
if (conn.State == ConnectionState.Closed)
conn = DbAgent.selfRef().getConnection("default");
var command1 = new SqlCommand(updateQuery, conn)
{
CommandType = CommandType.Text
};
command1.Parameters.AddWithValue("@RptInstanceId", apSess.ReportInstanceID);
command1.Parameters.AddWithValue("@SubSectionId", subSectionId);
if (chk != null)
{
//handle deletion from A330 Data Table and disable display from form
var checkedVal = 0;
if (isSelected == 1 && !chk.Checked)
{
//update FlexMeasureSelection Table
checkedVal = 0;
command1.Parameters.AddWithValue("@IsSelected", checkedVal);
command1.ExecuteNonQuery();
lbConfirmationMessage.Visible = true;
lbConfirmationMessage.Text = @"Selections have been saved";
}
else if (isSelected == 0 && chk.Checked) //enable display with form
{
//update FlexMeasureSelection Table
checkedVal = 1;
command1.Parameters.AddWithValue("@IsSelected", checkedVal);
command1.ExecuteNonQuery();
lbConfirmationMessage.Visible = true;
lbConfirmationMessage.Text = @"Selections have been saved";
}
conn.Close();
}
}
catch (Exception ex)
{
AppLogger.selfRef().log("Error in SelectionPage.aspx.cs btnSave_OnClick", AppLogger.logLevel.error);
var ex2 = new AppException(CORE.Utilities.AppException.ErrorCode.DB, ORHP.AppObjects.AppIni.selfRef().XmlErrorCodes, ex);
throw ex2;
}
}
MeasuresGrid.DataBind();
}