A very simple example to copy value of first control to the remaining controls in Repeater on clicking the control which is placed in the Header Template of Repeater. You can download a complete example of it.
CS File:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class CopyValues : System.Web.UI.Page
{
// It will contain the concatenated string of all textbox controls' ClientIDs, which will be used at client side
public string controlID = string.Empty;
// It is the reference to the Image which we have placed in the header template of Repeater which will copy values of first Textbox to all remaining Textboxes
Image imgCopyPaste = null;
///<summary>
/// Bind Datasouce to the Repeater
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
rptItems.DataSource = CreateDatasouce();
rptItems.DataBind();
}
///<summary>
/// Datasource which we will bind to the Repeater Control
///</summary>
///<returns></returns>
private ICollection CreateDatasouce()
{
// Create sample data for the Repeater control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("ItemName", typeof(String)));
dt.Columns.Add(new DataColumn("Qty", typeof(Int32)));
// Populate the table with sample values.
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = "item " + i.ToString();
dr[1] = i;
dt.Rows.Add(dr);
}
return dt.DefaultView;
}
protected void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
// Get the Image which we have placed in the Header Template
imgCopyPaste = e.Item.FindControl("imgCopyQty") as Image;
}
else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Get each Textbox control's ClientID and build a concatenated string
controlID = controlID + e.Item.FindControl("txtCustomQty").ClientID + "|";
}
else if (e.Item.ItemType == ListItemType.Footer)
{
// Add the onclick event and pass the concatenated string of controls' ClientIDs
imgCopyPaste.Attributes.Add("onclick", "CopyQuantity('" + controlID.Substring(0, controlID.Length - 1) + "')");
}
}
}
ASPX File:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CopyValues.aspx.cs" Inherits="CopyValues" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<style type="text/css">
</style>
<script type="text/javascript" language="javascript">
function CopyQuantity(controlID){
var controlIdArray=controlID.split("|");
if(document.getElementById(controlIdArray[0]).value=='')
{
alert('Please enter any quantity in the first item to paste in remaining items! ')
return;
}
for(var i=1; i<controlIdArray.length; i++)
{
document.getElementById(controlIdArray[i]).value=document.getElementById(controlIdArray[0]).value;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="rptItems" runat="server" OnItemDataBound="rptItems_ItemDataBound">
<HeaderTemplate>
<table style="border-right: lightseagreen 1px solid; border-top: lightseagreen 1px solid;
border-left: lightseagreen 1px solid; border-bottom: lightseagreen 1px solid">
<tr style="font-weight: bold; color: white; height: 21px; background-color: lightseagreen">
<td>
<strong>Item Name </strong>
</td>
<td>
<strong>Quantity </strong>
</td>
<td>
<strong>Value</strong><asp:Image ID="imgCopyQty" runat="server" Visible="true" ImageUrl="~/Images/copy_paste.png"
ToolTip="Copy Quantity"></asp:Image></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("ItemName") %>
</td>
<td>
<%# Eval("Qty") %>
</td>
<td>
<asp:TextBox ID="txtCustomQty" runat="server" Width="30px"></asp:TextBox></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>