I have a gridview inside which there are 4-5 checkboxes. In that gridview, I have 10 rows too. So, Now what i want is.
IF I check, 2 checkboxes and don't check remaining checkboxes. how to save the value of the checked checkboxes value as Y and unchecked checkboxes value as N into the database.
Below is the HTML of the checkboxes here:-
<cc2:Grid ID="GrdRights" runat="server" FolderStyle="../Styles/Grid/style_12"
AllowSorting="False" AutoGenerateColumns="False" AllowColumnResizing="true" AllowAddingRecords="false"
AllowMultiRecordSelection="true" OnRowDataBound="GrdRights_RowDataBound" ViewStateMode="Enabled"
PageSize="100">
<ClientSideEvents OnClientSelect="FunMonthList" />
<ScrollingSettings ScrollHeight="400px" />
<Columns>
<cc2:Column ID="Column1" DataField="MKEY" ShowHeader="false" HeaderText="Select" ReadOnly="true"
Width="5%" runat="server">
<TemplateSettings TemplateId="TemplateWithCheckbox" />
</cc2:Column>
<cc2:Column ID="Column2" DataField="parent_menu" HeaderText="MENU" Visible="true"
ReadOnly="true" Width="10%" runat="server">
</cc2:Column>
<cc2:Column ID="Column4" DataField="child_menu_mkey" Visible="false" ReadOnly="true"
Width="10%" runat="server">
</cc2:Column>
<cc2:Column ID="Column3" DataField="child_menu" HeaderText="SUB MENU" runat="server"
Visible="true" Width="30%">
</cc2:Column>
<%--<cc2:Column ID="Column5" DataField="MKEY" ShowHeader="false" HeaderText="Select" ReadOnly="true" Width="5%" runat="server">
<TemplateSettings TemplateId="TemplateWithCheckbox" />
</cc2:Column>--%>
<cc2:CheckBoxColumn Id="chkAdd" DataField="ADD_FLAG" ShowHeader="true" HeaderText="Add"
Width="5%" runat="server" Align="right">
<TemplateSettings TemplateId="TemplateWithChecAdd" />
</cc2:CheckBoxColumn>
<cc2:CheckBoxColumn Id="chkEdit" DataField="MODIFY_FLAG" ShowHeader="true" HeaderText="Edit"
Width="5%" runat="server" Align="right">
<TemplateSettings TemplateId="TemplateWithChecEdit" />
</cc2:CheckBoxColumn>
<cc2:CheckBoxColumn Id="ChkView" DataField="VIEW_FLAG" ShowHeader="true" HeaderText="View"
Width="6%" runat="server" Align="right">
<TemplateSettings TemplateId="TemplateWithChecView" />
</cc2:CheckBoxColumn>
<cc2:CheckBoxColumn Id="ChkDelete" DataField="DEL_FLAG" ShowHeader="true" HeaderText="Delete"
Width="8%" runat="server" Align="right">
<TemplateSettings TemplateId="TemplateWithChecDelete" />
</cc2:CheckBoxColumn>
</Columns>
<Templates>
<cc2:GridTemplate ID="TemplateWithChecAdd">
<Template>
<asp:CheckBox runat="server" ID="ChkIDAdd" ToolTip="<%# Container.Value %>" />
</Template>
</cc2:GridTemplate>
<cc2:GridTemplate ID="TemplateWithChecView">
<Template>
<asp:CheckBox runat="server" ID="ChkIDView" ToolTip="<%# Container.Value %>" />
</Template>
</cc2:GridTemplate>
<cc2:GridTemplate ID="TemplateWithChecEdit">
<Template>
<asp:CheckBox runat="server" ID="ChkIDEdit" ToolTip="<%# Container.Value %>" />
</Template>
</cc2:GridTemplate>
<cc2:GridTemplate ID="TemplateWithChecDelete">
<Template>
<asp:CheckBox runat="server" ID="ChkIDDelete" ToolTip="<%# Container.Value %>" />
</Template>
</cc2:GridTemplate>
</Templates>
</cc2:Grid>
The below is the code which I tried but It didn't worked. After checking, i was getting checkbox.checked = true but in database the value was not getting stored.
public bool Save()
{
try
{
for (int i = 0; i < GrdRights.RowsInViewState.Count; i++)
{
GridDataControlFieldCell chkcell = GrdRights.RowsInViewState[i].Cells[0] as GridDataControlFieldCell;
GridDataControlFieldCell chkcellAdd = GrdRights.RowsInViewState[i].Cells[4] as GridDataControlFieldCell;
GridDataControlFieldCell chkcellEdit = GrdRights.RowsInViewState[i].Cells[5] as GridDataControlFieldCell;
GridDataControlFieldCell chkcellView = GrdRights.RowsInViewState[i].Cells[6] as GridDataControlFieldCell;
GridDataControlFieldCell chkcellDel = GrdRights.RowsInViewState[i].Cells[7] as GridDataControlFieldCell;
CheckBox chk = chkcell.FindControl("ChkID") as CheckBox;
CheckBox chkAddcheck = chkcellAdd.FindControl("ChkIDAdd") as CheckBox;
CheckBox chkEditcheck = chkcellEdit.FindControl("ChkIDEdit") as CheckBox;
CheckBox chkViewcheck = chkcellView.FindControl("ChkIDView") as CheckBox;
CheckBox chkDelcheck = chkcellDel.FindControl("ChkIDDelete") as CheckBox;
System.Text.StringBuilder StrPubBldg = new System.Text.StringBuilder();
XmlWriter xw = XmlWriter.Create(StrPubBldg);
xw.WriteStartElement("DocumentElement");
xw.WriteStartElement("WMS_Menu_Rights_Test");
if (GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim().ToString() == "0")
{
xw.WriteElementString("MENU_MKEY", GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("MKEY")].Text.Trim());
}
else
{
xw.WriteElementString("MENU_MKEY", GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim());
}
xw.WriteElementString("USER_ID", Hid_Selected_user.Value);
xw.WriteElementString("Delete_Flag", "N");
foreach (GridRow row in GrdRights.Rows)
{
xw.WriteElementString("ADD_FLAG", chkAddcheck.Checked == true ? "Y" : "N"); // how to save these values in the database
xw.WriteElementString("MODIFY_FLAG", chkEditcheck.Checked == true ? "Y" : "N");
xw.WriteElementString("VIEW_FLAG", chkViewcheck.Checked == true ? "Y" : "N");
xw.WriteElementString("DEL_FLAG", chkDelcheck.Checked == true ? "Y" : "N");
}
xw.WriteElementString("CREATION_DATE", System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
xw.WriteEndElement();
xw.WriteEndElement();
xw.Close();
MainEnqMkey = InsertUpdateDelete.InsertUpdateDeleteCls.InsertUpdateDelete_sql(strMode, Convert.ToInt16(iMKey), "WMS_Menu_Rights_Test", "MKEY", "MUR", StrPubBldg.ToString());
}
ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "alert('Rights assigned successfully.');", true);
}
catch (Exception)
{
throw;
}
return false;
}
Any suggestions on how to save this ?