如何通过单击按钮更新位于Repeater内部的GridView [英] How to update GridView which is inside Repeater by Clicking Button

查看:70
本文介绍了如何通过单击按钮更新位于Repeater内部的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cross-Tab表,该表是在Repeater内的GridView中实现的.此Cross-Tab GridView由两个主键组成,这两个主键分别属于数据库中的两个表.用户名,CourseID.要更新GridView,我将复选框放在空文件中,当管理员检查它们并单击保存"按钮时,将更新GridView.

生成CrossTab报表的GridView背后的SqlDataSource使用一个StoredProcedure,它还返回CourseID,将它绑定到复选框.因为我的课程在GridView列中,而员工在GridView行中.

这是我的ASP.NET代码:

I have a Cross-Tab table which I implemented in a GridView inside a Repeater. This Cross-Tab GridView consists of two primary keys that belong to two tables in the database; Username, CourseID. To update the GridView, I put checkboxes inside the empty fileds, when the admin checks them and click on the Save button, the GridView will be updated.

The SqlDataSource behind this GridView which generates the CrossTab report uses a StoredProcedure which also returns the CourseID which I bound it to checkbox. Becuase I have the courses in the GridView Columns and employees in the GridView Rows.

This is my ASP.NET Code:

<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="[DivisionName] like ''{0}%''">
                        
                    <FilterParameters>
                        <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName" 
                                                 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>



后面的代码是:



The code-behind is:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Specialized;

public partial class KPIReportwithRepeatorFilter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }


    //This function is for checking each cell in each row. 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
	    if (e.Row.RowType == DataControlRowType.DataRow)
	    {
            for (int i = 5; i < e.Row.Cells.Count; i++)
            {
                string yes = e.Row.Cells[i].Text.Split(new string[] {", " },StringSplitOptions.RemoveEmptyEntries)[1];
                string courseid = e.Row.Cells[i].Text.Split(new string[] {", " },StringSplitOptions.RemoveEmptyEntries)[0];
                
                CheckBox chkCheckBox = new CheckBox();
                chkCheckBox.ID = "chkCheckBoxId";
                chkCheckBox.SkinID = courseid;
                chkCheckBox.Checked = yes.Equals("Yes");
                e.Row.Cells[i].Controls.Add(chkCheckBox);

                // Check if the cell vlaue = Yes
                // if it is Yes, the cell will be colored with Light Green 
                if (yes.Equals("Yes"))
                {
                    e.Row.Cells[i].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)
        {
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                GridView GridView1 = (GridView)Repeater1.Items[i].FindControl("GridView1");
                //List CheckBoxList = new List();
                for (int j = 5; j < GridView1.Rows.Count; j++)
                {
                    CheckBox chkCheckBox = (CheckBox)GridView1.Rows[j].FindControl("chkCheckBoxId");
                    //int courseid = (int)GridView1.DataKeys[j][0];
                    if (chkCheckBox != null && chkCheckBox.Checked)
                    {
                        CheckBoxList.Add(ch);
                    }
                }
            }
        }

   
}




主要问题是 btnSave_Click() 现在,我想我需要插入包含以下内容的内容:
GridView.Row.Cells [4]中员工的用户名 复选框ID
chkCheckBox.SkinID内的课程ID


我不知道如何在最后一个方法中插入所有这些东西.

仅供参考,如果没有此方法,则代码正确%100,并且效果很好.




The main problem is with btnSave_Click() Now I think I need to insert something that contains the following:
the username of the employee which is in GridView.Row.Cells[4]
the checkbox id
the courseid which is inside chkCheckBox.SkinID


I don''t know how to insert all of these things in the last method.

FYI, without this method the code is %100 right and it works well.

推荐答案

ConnectionStrings:testConnectionString%> SelectCommandType ="StoredProcedure" SelectCommand ="kbiReport" FilterExpression ="[DivisionName]之类的"{0}%""> < FilterParameters> < asp:ControlParameter ControlID ="ddlDivision" Name ="DivisionName" PropertyName ="SelectedValue" Type ="String"/> <%-< asp:ControlParameter ControlID ="ddlOrganization" Name ="Organization" PropertyName ="SelectedValue" Type ="String"/>-%> </FilterParameters> < SelectParameters> <%-ControlParameter链接到上面的HiddenField,以基于不同的值生成不同的GridView 的GroupID-%> < asp:ControlParameter ControlID ="HiddenField1" Name ="GroupID" PropertyName ="Value"/> </SelectParameters> </asp:SqlDataSource> < asp:GridView ID ="GridView1" runat ="server" AllowSorting =真" CellPadding ="3" DataSourceID ="SqlDataSource1" CssClass ="mGrid" AlternatingRowStyle-CssClass ="alt" RowStyle-Horizo​​ntalAlign ="Center" OnRowDataBound ="GridView1_RowDataBound" OnDataBound ="GridView1_DataBound"> < AlternatingRowStyle BackColor ="White" ForeColor =#284775"/> < HeaderStyle Font-Bold ="true" ForeColor ="Black"/> <列> < asp:CommandField ShowSelectButton ="True"/> <%-< asp:TemplateField HeaderText ="Select"> < ItemTemplate> < asp:CheckBox ID ="chkSelect" runat ="server" AutoPostBack ="true" OnCheckedChanged ="chkSelect_CheckedChanged"/> </ItemTemplate> </asp:TemplateField>-%> </列> < EditRowStyle BackColor =#999999"/> < FooterStyle BackColor =#5D7B9D" Font-Bold ="True" ForeColor ="White"/> < PagerStyle BackColor =#284775" ForeColor ="White" Horizo​​ntalAlign ="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 =服务器" ConnectionString =<%
ConnectionStrings:testConnectionString %>" SelectCommandType="StoredProcedure" SelectCommand="kbiReport" FilterExpression="[DivisionName] like ''{0}%''"> <FilterParameters> <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName" 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 =从课程中选择DISTINCT GroupID"> </asp:SqlDataSource>
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT DISTINCT GroupID FROM courses"> </asp:SqlDataSource>



后面的代码是:



The code-behind is:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Specialized;

public partial class KPIReportwithRepeatorFilter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }


    //This function is for checking each cell in each row. 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
	    if (e.Row.RowType == DataControlRowType.DataRow)
	    {
            for (int i = 5; i < e.Row.Cells.Count; i++)
            {
                string yes = e.Row.Cells[i].Text.Split(new string[] {", " },StringSplitOptions.RemoveEmptyEntries)[1];
                string courseid = e.Row.Cells[i].Text.Split(new string[] {", " },StringSplitOptions.RemoveEmptyEntries)[0];
                
                CheckBox chkCheckBox = new CheckBox();
                chkCheckBox.ID = "chkCheckBoxId";
                chkCheckBox.SkinID = courseid;
                chkCheckBox.Checked = yes.Equals("Yes");
                e.Row.Cells[i].Controls.Add(chkCheckBox);

                // Check if the cell vlaue = Yes
                // if it is Yes, the cell will be colored with Light Green 
                if (yes.Equals("Yes"))
                {
                    e.Row.Cells[i].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)
        {
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                GridView GridView1 = (GridView)Repeater1.Items[i].FindControl("GridView1");
                //List CheckBoxList = new List();
                for (int j = 5; j < GridView1.Rows.Count; j++)
                {
                    CheckBox chkCheckBox = (CheckBox)GridView1.Rows[j].FindControl("chkCheckBoxId");
                    //int courseid = (int)GridView1.DataKeys[j][0];
                    if (chkCheckBox != null && chkCheckBox.Checked)
                    {
                        CheckBoxList.Add(ch);
                    }
                }
            }
        }

   
}




主要问题是 btnSave_Click() 现在,我想我需要插入包含以下内容的内容:
GridView.Row.Cells [4]中员工的用户名 复选框ID
chkCheckBox.SkinID内的课程ID


我不知道如何在最后一个方法中插入所有这些东西.

仅供参考,如果没有此方法,则代码正确为%100,效果很好.




The main problem is with btnSave_Click() Now I think I need to insert something that contains the following:
the username of the employee which is in GridView.Row.Cells[4]
the checkbox id
the courseid which is inside chkCheckBox.SkinID


I don''t know how to insert all of these things in the last method.

FYI, without this method the code is %100 right and it works well.


这篇关于如何通过单击按钮更新位于Repeater内部的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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