如何更新GridView后,检查GridView字段内的一些复选框
本文关键字:GridView 字段 复选框 检查 何更新 更新 | 更新日期: 2023-09-27 18:17:06
我正在使用ASP.NET开发一个intranet web应用程序。现在,我需要为我的部门制作一份培训记录。这个部门由四个小组组成。为此,我按照以下方式开发了这个记录:我使用了一个Repeator,并在其中放入了GridView,因为我有三种不同类型的培训课程。我使用HiddenField来确定每个培训课程类型的ID。为了从数据库中检索数据,我使用了StoredProcedure。经过这么长时间的工作,一切都运转良好。
现在,我需要设置这些GridViews由管理员更新。既然我已经很多员工和很多课程在每个GridView,我认为最好的更新GridView的方法是将空白字段显示为复选框管理员想要更新一个员工的记录需要做的只是检查复选框,并单击更新按钮。能够显示空字段作为复选框,但现在我不知道如何将选中复选框的值发送到数据库
我正在寻找的场景是使系统检查每一个复选框,如果它之前已经被选中,保持它不变是多少。如果它是空的,现在刚刚检查,它应该被添加到数据库中员工的记录。
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommandType="StoredProcedure" SelectCommand="kbiReport">
<%--FilterExpression="[Division] like '{0}%' and [Organization] like '{1}%'">--%>
<%--<FilterParameters>
<asp:ControlParameter ControlID="ddlDivision" Name="Division"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="ddlOrganization" Name="Organization"
PropertyName="SelectedValue" Type="String" />
</FilterParameters>--%>
<SelectParameters>
<%--ControlParameter is linked to the HiddenField above to generate different GridView based on different values
of GroupID--%>
<asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True"
CellPadding="3"
DataSourceID="SqlDataSource1"
CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black"/>
<Columns>
<asp:CommandField ShowSelectButton="True" />
<%--<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>--%>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT DISTINCT GroupID FROM courses">
</asp:SqlDataSource>
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
<br /><br />
后台代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell c in e.Row.Cells)
{
// Check if the cell vlaue = Yes
// if it is Yes, the cell will be colored with Light Green
if (c.Text.Equals("Yes"))
{
c.BackColor = System.Drawing.Color.LightGreen;
}
}
}
// The following is for changing the color of headers in each GridView based on the value of the HiddenFild
// BTW, the value of the HiddenField is the value of the GroupID in Group Table in the Database
else if(e.Row.RowType == DataControlRowType.Header){
switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value)
{
case "1":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
break;
case "2":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
break;
case "3":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.Orange;
break;
}
}}
//For inserting checkboxes inside all courses buttons
protected void GridView1_DataBound(object sender, EventArgs e){
GridView GridView1 = (GridView)sender;
foreach (GridViewRow objRow in GridView1.Rows)
{
for (int i = 5; i < objRow.Cells.Count; i++){
CheckBox chkCheckBox = new CheckBox();
objRow.Cells[i].Controls.Add(chkCheckBox);
if (objRow.Cells[i].BackColor == System.Drawing.Color.LightGreen)
chkCheckBox.Checked = true;
}
}
}
//For updating the GridView (Save Button)
protected void btnSave_Click(object sender, EventArgs e)
{
}
更新:我修改了btnSave_Click按钮,如下所示:
//For updating the GridView (Save Button)
protected void btnSave_Click(object sender, EventArgs e)
{
GridView GridView1 = (GridView)sender;
// Are there checked boxes?
List<int> CheckBoxList = new List<int>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
int courseid = (int)GridView1.DataKeys[i][0];
CheckBox checkBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox");
if (checkBox.Checked)
{
CheckBoxList.Add(courseid);
}
}
}
但是我得到了以下错误:sys . webforms . pagerequestmanagerservererrorreexception:无法强制转换类型为"System.Web.UI.WebControls"的对象。按钮'键入'System.Web.UI.WebControls.GridView'
我不知道为什么我得到这个错误。有人能帮我一下吗?
你将需要改变这个工作为你需要的,但如果你想检查在GridView控件检查的项目,试试这个:
List<int> CheckBoxList = new List<int>();
for (int i = 0; i < GridView.Rows.Count; i++)
{
int yourColumnId = (int)GridView.DataKeys[i][0];
CheckBox cb = (CheckBox)GridView.Rows[i].FindControl("The name of your CheckBox");
if (cb.Checked)
{
CheckBoxList.Add(productId);
}
}
// Do something with CheckBoxList