COPYING MULTIPLE ROWS IN FOOTER TEMPLATE ON SINGLE BUTTON CLICK

Prabhukiran345
Posted by Prabhukiran345 under C# category on | Points: 40 | Views : 2915
ASPX CODE:


<asp:Button Text="CopyNewRows" runat="server" ID="btnCopyRows" OnClick="btnCopyRows_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeId"
ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField HeaderText="NAME" SortExpression="EmployeeName">
<EditItemTemplate>
<asp:TextBox ID="txtEmpName" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hdnEmpId" Value='<%# Bind("EmployeeId") %>' />
<asp:Label ID="lblEmpName" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Panel runat="server" ID="pnlEmpName" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DESIGNATION" SortExpression="EmployeeDesignation">
<EditItemTemplate>
<asp:TextBox ID="txtEmpDesig" runat="server" Text='<%# Bind("EmployeeDesignation") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblEmpDesig" runat="server" Text='<%# Bind("EmployeeDesignation") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Panel runat="server" ID="pnlEmpDesig" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox Text="" runat="server" ID="chk" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>


CS CODE:

protected void btnCopyRows_Click(object sender, EventArgs e)
{
this.CopyFooterRows(); //Copying checked rows to the footer with web controls.
}
protected void CopyFooterRows()
{
ArrayList categoryIDList = new ArrayList();
GridViewRow gr = GridView1.FooterRow;
Panel pnlEmpName = (Panel)gr.FindControl("pnlEmpName");
Panel pnlEmpDesig = (Panel)gr.FindControl("pnlEmpDesig");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].FindControl("chk")).Checked)
{
categoryIDList.Add(((HiddenField)GridView1.Rows[i].FindControl("hdnEmpId")).Value);
TextBox lblEmpName = new TextBox();
lblEmpName.Text = ((Label)GridView1.Rows[i].FindControl("lblEmpName")).Text;
lblEmpName.ID = "lblEmpName" + i;
lblEmpName.Attributes.Add("readonly", "readonly");
lblEmpName.Attributes.Add("style", "background-color:#507CD1; font-weight:bold; Color:White;border:0");
lblEmpName.ClientIDMode = ClientIDMode.Static;
pnlEmpName.Controls.Add(lblEmpName);
pnlEmpName.Controls.Add(new LiteralControl("<br/>"));
TextBox lblEmpDesig = new TextBox();
lblEmpDesig.Text = ((Label)GridView1.Rows[i].FindControl("lblEmpDesig")).Text;
lblEmpDesig.ID = "lblEmpDesig" + i;
lblEmpDesig.Attributes.Add("readonly", "readonly");
lblEmpDesig.Attributes.Add("style", "background-color:#507CD1; font-weight:bold; Color:White;border:0");
lblEmpDesig.ClientIDMode = ClientIDMode.Static;
pnlEmpDesig.Controls.Add(lblEmpDesig);
pnlEmpDesig.Controls.Add(new LiteralControl("<br/>"));
}
}
}

Comments or Responses

Posted by: kgovindarao523-21772 on: 4/22/2013 Level:Bronze | Status: [Member] [MVP] | Points: 10
Useful code... good job

Login to post response