我正在开发一个使用ASP.NET的Intranet Web应用程序。现在,我需要为我的部门开发培训记录。该部门由四个小组组成。为此,我开发了这个记录如下:我使用了一个Repeator,并在里面放置了GridView,因为我有三种不同类型的培训课程。我使用HiddenField来确定每个培训课程类型的ID。为了从数据库中检索数据,我使用了StoredProcedure。一切工作正常,很好,经过这么长的任务。如何检查GridView字段内的某些复选框后更新GridView?
现在,我需要设置这些GridViews更新由管理员。因此,由于我在每个GridView中都有很多员工和很多课程,所以我认为最好的 更新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)
{
}
UPDATE: 我修改了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.PageRequestManagerServerErrorException:无法转换类型为“System.Web.UI.WebControls.Button”的对象以键入“System.Web .UI.WebControls.GridView'
我不知道为什么我得到这个错误。任何人都可以帮助我吗?
你能不能动态地添加一个字段到GridView这将显示更新管理 – MethodMan
没有必要的。所有我想要的是识别检查复选框,并更新数据库中的记录 –
哦..好吧,那么你可以做一个foreach循环,并检查复选框,并从那里做一些额外的编码,如果有复选框被检查让我寄给你一个样本 – MethodMan