A simple way to copy value of first control to the remaining controls in a ASP.NET Repeater.
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;
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" %>
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>

5 comments:
hi.. just dropping by here... have a nice day! http://kantahanan.blogspot.com/
nice~..................................................
Hi... Looking ways to market your blog? try this: http://bit.ly/instantvisitors
Man is not made for defeat. A mean can be destroyed but not defeated...................................................
乳交挑逗淫婦色情俱樂部成人色情成人影片視訊網愛聊天室免費色情網情色區火辣美女情色性愛成人影音聊天成人色情網站全裸美女圖片成人裸照sex辣妹裸體美女全裸圖成人區av女情色內容情色自拍貼圖成人色情網性愛論壇一對多性伴侶成人聊天室撫摸淫美成人論壇女人奶頭女生自慰影片台灣女優美女視訊一絲不掛一夜正妹成人影像巨乳大奶子情色性愛貼圖情色王國做愛視訊火辣情色台灣情色網情色聊天網性愛技巧淫娃情色成人巨乳辣妹性愛知識性經驗
Post a Comment
Thank you for your Comment!