选中GridView字段内的某些复选框后如何更新GridView [英] How to update the GridView after checking some checkbox inside the GridView Fields

查看:64
本文介绍了选中GridView字段内的某些复选框后如何更新GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用ASP.NET开发Intranet Web应用程序.现在,我需要为我的部门制定培训记录.该部门由四个小组组成.为此,我开发了以下记录:我使用了Repeater,并在其中放入了GridView,因为我有三种不同类型的培训课程.我使用HiddenField来确定每种培训课程类型的ID.为了从数据库中检索数据,我使用了StoredProcedure.这项艰巨的任务完成后,一切工作都很好.

现在,我需要设置这些GridView,以便由管理员进行更新.所以既然我有
每个GridView中都有很多员工和很多课程,我认为最好的
更新GridView的一种方法是将空白字段显示为复选框,并且在
管理员希望更新其中一名员工的记录,所有他的记录
要做的只是检查复选框,然后单击更新按钮. 我可以
能够将空白字段显示为复选框,但现在我不知道如何
将选中复选框的值发布到数据库中.


我正在寻找的方案是使系统检查每个
每个GridView中的复选框,如果之前已被选中,则保持不变
是.如果它是空的并且现在才被检查,则应将其添加到
员工记录中的数据库.

Hello everybody,

I am developing an intranet web application using ASP.NET. Now, I need to develop a training record for my department. The department consists of the four groups. For that, I developed this record as following: I used a Repeator and inside it I put GridView since I have three different types of training courses. And I used a HiddenField to determine the ID for each training course type. For retrieving the data from the database, I used the StoredProcedure. Everything works fine and well after this long task.

Now, I need to set these GridViews for updating by the admin. So since I have
a lot of employees and a lot of courses in each GridView, I think the best
way to update the GridView is to show the blank fields as checkboxes and when
the Admin wants to update the record of one of the employees, all what he
needs to do is just checking the checkboxes and click update button. I could
be able to show the empty fields as checkboxes but now I don''t know how to
post the value of checking the checkbox to the database.


The scenario that I am looking for it is to make the system check every
checkbox in each GridView, if it is already checked before, leave it as it
is. If it was empty and it is just checked now, it should be added to the
database in the record of the employee.

<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 />



背后的代码:



The Code-Behind:

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)
        {
            
        }

推荐答案

ConnectionStrings:testConnectionString%>" SelectCommandType StoredProcedure" SelectCommand =" >> <%- FilterExpression ="[Division]如'{0}%'和[Organization]如'{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链接到上面的HiddenField,以基于不同的值生成不同的GridView 的GroupID -%> < asp:ControlParameter ControlID =" 名称 GroupID " PropertyName 值" / < /SelectParameters > < /asp:SqlDataSource > < asp:GridView ID =" runat 服务器" AllowSorting =" 真实" CellPadding =" 3" DataSourceID =" SqlDataSource1" CssClass =" mGrid" AlternatingRowStyle-CssClass =" =" OnRowDataBound =" GridView1_RowDataBound" OnDataBound =" > < AlternatingRowStyle =" 白色" ForeColor >#284775" / > < HeaderStyle ForeColor 黑色" > < > < asp:CommandField ShowSelectButton =" / <%- < asp:TemplateField HeaderText ="Select"> < ItemTemplate> < asp:CheckBox ID ="chkSelect" runat ="server" AutoPostBack ="true" OnCheckedChanged ="chkSelect_CheckedChanged"/> </ItemTemplate> </asp:TemplateField> -%> < /列 > < EditRowStyle =" #999999" / < FooterStyle =" #5D7B9D" 字体粗体 True" ForeColor =" 白色" / > < PagerStyle =" #284775" ForeColor 白色" Horizo​​ntalAlign =" 居中" / < RowStyle =" #F7F6F3" ForeColor #333333 " / < SelectedRowStyle =" #E2DED6" 字体粗体 True" ForeColor =" #333333" / > < SortedAscendingCellStyle =" #E9E7E2" / < SortedAscendingHeaderStyle =" #506C8C" / < SortedDescendingCellStyle =" #FFFDF8" / < SortedDescendingHeaderStyle =" #6F8DAE" / < /asp:GridView > < /ItemTemplate > < /asp:Repeater > < asp:SqlDataSource ID =" runat 服务器" 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 =" > < /asp:SqlDataSource > < asp:Button ID =" runat 服务器" OnClick btnSave_Click" 文本 保存" / > < br > br / >
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT DISTINCT GroupID FROM courses"> </asp:SqlDataSource> <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" /> <br /><br />



背后的代码:



The Code-Behind:

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)
        {
            
        }


我想知道哪一个简单,

1.保存到数据库时,请始终仔细检查并确保在将复选框保存到数据库之前,是否刚刚将其打勾.



2.保存到数据库时,将其全部删除并保存所有已检查的数据吗?

>>仍然取决于我不知道的业务需求...随时添加评论.谢谢. :)
I wonder which one is easy,

1. when saving to the database, always double check and make sure that if the checkbox was just newly ticked as check before saving it to the database?

or

2. when saving to the database, remove all and save all data that was check?

>> still depends on business requirements which i don''t know... feel free to add comments. thanks. :)


这篇关于选中GridView字段内的某些复选框后如何更新GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆