在检查GridView字段中的一些复选框后如何更新GridView?

时间:2011-12-08 16:33:12

标签: c# asp.net

我正在使用ASP.NET开发Intranet Web应用程序。现在,我需要为我的部门制定培训记录。该部门由四个小组组成。为此,我开发了以下记录:我使用了一个Repeator,我在里面放了GridView,因为我有三种不同类型的培训课程。我使用HiddenField来确定每种培训课程类型的ID。为了从数据库中检索数据,我使用了StoredProcedure。在这项漫长的任务之后,一切都很顺利。

现在,我需要设置这些GridView以供管理员更新。因为我有 很多员工和每个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 &lt; e.Row.Cells.Count; i++)
                        e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
                    break;

                case "2":
                    for (int i = 5; i &lt; e.Row.Cells.Count; i++)
                        e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
                    break;

                case "3":
                    for (int i = 5; i &lt; 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 &lt; 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.PageRequestManagerServerErrorException:无法将'System.Web.UI.WebControls.Button'类型的对象强制转换为'System.Web.UI.WebControls.GridView'

我不知道为什么会出现这个错误。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

您需要更改此设置以满足您的需要,但如果您想检查在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