User Control Code
AddMore.ascx
<%@ Control Language="c#" Inherits="Ind.UC.AddMore" CodeFile="AddMore.ascx.cs" %>
<table cellpadding="0" cellspacing="0" width="100%" align="center" id="tblAddMore">
<tr>
<td align="right">
<input class="button" id="btnAdd" type="button" value="Add Row" runat="server">
<input class="button" id="btnDelete" type="button" value="Delete Row" runat="server">
td>
tr>
<tr style="display:none">
<td align="right">
<input class="button" id="btnGet" type="button" value="Get Values" runat="server" />
<input type ="hidden" id ="hdnArray" />
td>
tr>
table>
<script language="javascript" src="js/CommonControls.js">script>
<script language="javascript" type="text/javascript">
function getObj(id) {
return document.getElementById(id);
}
function getAllValues(tableId)
{
var table = getObj(tableId);
var str="";
for(var i=1; i < style="">
{
str+="Row"+i+".value = ";
for(var j=0; j < style="">
{
for(var k=0; k <>
{
var childNode = table.rows[i].cells[j].childNodes[k];
if(childNode.type=="text")
str += "'"+childNode.value+"', ";
}
}
}
alert(str);
}
script>
AddMore.ascx.cs
namespace Ind.UC
{
using System;
using System.Web.UI.HtmlControls;
///
/// Summary description for AddMore.
///
public partial class AddMore : System.Web.UI.UserControl
{
//protected System.Web.UI.HtmlControls.HtmlInputButton btnDelete;
protected string clickEvent;
public string onclick {
get{return clickEvent;}
set{clickEvent = value;}
}
// private void Page_Load(object sender, System.EventArgs e) {
// // Put user code to initialize the page here
// }
#region Web Form Designer generated code
override protected void OnInit(EventArgs e) {
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InitializeComponent();
base.OnInit(e);
}
//Required method for Designer support - do not modify the contents of this method with the code editor.
private void InitializeComponent() {
//this.Load += new System.EventHandler(this.Page_Load);
//this.btnAddMore.Attributes.Add("onclick",this.clickEvent);
this.btnAdd.Attributes.Add("onclick", "javascript:addRowInATable('tblAddMorea')");
this.btnDelete.Attributes.Add("onclick", "javascript:deleteRowInATable('tblAddMorea')");
this.btnGet.Attributes.Add("onclick", "javascript:getAllValues()");
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
CommonControls.js
/*Commonly Used Functions*/
/*Created By : Ananda Thirumurugan.V */
/*Date : 15_JUNE_2007 */
var arrElements = new Array();
function callGrabFunction(tableID,hdnArray)
{
}
function getValuesInACell(cell) {
for(var i=0;i
var childNode = cell.childNodes[i];
getValuesOfAControl(childNode);
}
}
function getValuesOfAControl(control) {
if(control.type == "select-one") {
control.selectedIndex=0;
control.disabled=false;
}
else if(control.type == "text") {
alert(control.value);
//control.value = "";
//control.disabled=false;
//if(control.getAttribute("defaultRead")) {
// if(control.getAttribute("defaultRead") == 1)
// control.readOnly=true;
//}
}
else if(control.type == "textarea") {
control.value = "";
control.disabled=false;
}
else if(control.type == "checkbox") {
control.checked = false;
control.disabled=false;
}
else if(control.type == "select-multiple") {
for(var j=childNode.options.length-1;j>0;j--) {
if(control.options[j].selected == true)
control.options[j].selected == false;
//childNode.remove(j);
}
control.disabled=false;
}
}
//In
//function used to addrow in a table(Table id)
function addRowInATable(tableID) {
var tableObj = getId(tableID);
var rowObj = tableObj.insertRow(tableObj.rows.length);
addCellInARow(tableObj, rowObj);
assignRowClass(tableObj);
}
//function to add specific number of rows
function addSpecificRows(tblID, count) {
for(var i=0;i
addRowInATable(tblID);
//addRowInATable(tblID);
}
}
//In
//function adds a cell in a row(Table & row object)
function addCellInARow(tableObj, rowObj) {
for(var i=0; i
var cellObj = rowObj.insertCell(i);
cellObj.innerHTML = tableObj.rows[1].cells[i].innerHTML;
// alert(tableObj.rows[i].cells[0].innerHTML)
// alert(tableObj.rows[i].cells[i].innerHTML)
clearValuesInACell(cellObj);
}
}
//function calls corresponding function based on the control type
function correspondingClearCall(control) {
var type=returnControlType(control);
//alert(control.childNodes[0].tagName);
if(type=="DIV"){
}
else if(type="TABLE") {
dsarClearValuesInATable(control);
}
else if(type=="TR") {
dsarClearValuesInARow(control);
}
else if(type=="TD") {
clearValuesInACell(control);
}
else if(type.match(/INPUT|SELECT|TEXTAREA/gi)) {
clearValuesInAChildNode(control);
}
}
//
function returnControlType(control) {
return control.tagName;
}
/*//function
function clearValuesInADiv() {
var length = arguments.length;
for(var i=0;i
clearValuesInATable();
}
}*/
//In
//function clears value of a cell (function will not remove dropdown options)(cell).
function clearValuesInACell(cell) {
for(var i=0;i
var childNode = cell.childNodes[i];
clearValuesOfAControl(childNode);
}
}
//In
// function clears values of a control and also enables them
function clearValuesOfAControl(control) {
if(control.type == "select-one") {
control.selectedIndex=0;
control.disabled=false;
}
else if(control.type == "text") {
control.value = "";
control.disabled=false;
if(control.getAttribute("defaultRead")) {
if(control.getAttribute("defaultRead") == 1)
control.readOnly=true;
}
}
else if(control.type == "textarea") {
control.value = "";
control.disabled=false;
}
else if(control.type == "checkbox") {
control.checked = false;
control.disabled=false;
}
else if(control.type == "select-multiple") {
for(var j=childNode.options.length-1;j>0;j--) {
if(control.options[j].selected == true)
control.options[j].selected == false;
//childNode.remove(j);
}
control.disabled=false;
}
}
// function deletes a row in a table. Expects a table id as a parameter
function deleteRowInATable(tableId) {
var tableObj = getId(tableId)
for(var i=tableObj.rows.length-1;i>=1;i--) {
var length = tableObj.rows[i].cells[tableObj.rows[i].cells.length-1].childNodes.length;
for(var j=0;j
if(tableObj.rows[i].cells[tableObj.rows[i].cells.length-1].childNodes[j].type=="checkbox") {
if(tableObj.rows[i].cells[tableObj.rows[i].cells.length-1].childNodes[j].checked==true) {
var childNode=tableObj.rows[i].cells[tableObj.rows[i].cells.length-1].childNodes[j];
j=length;
deleteRowByControl(childNode);
}
}
}
}
}
//In
//function deletes the row containing the control.(control object)
function deleteRowByControl(control) {
var row=control.parentNode.parentNode;
var tableObj=row.parentNode;
if(row.rowIndex != 1) {
row.parentNode.deleteRow(row.rowIndex);
assignRowClass(tableObj);}
else {
alert("* This row cannot be deleted.");
control.checked=false;
return false;}
}
//In
//function applies alternating class to the rows in a table.(Table Object)
function assignRowClass(tableObj) {
for(var i=1;i
var rowObj = tableObj.rows[i];
if(rowObj.rowIndex%2==0)
rowObj.className="tablecontent2";
else
rowObj.className="tablecontent1";
}
}
//function returns necessary validation parameters
function returnValidationParameters(controlID) {
return controlID.valid.split("_");
}
//function validates only mandatory controls
function validateMandatoryControls(childNode) {
if((childNode.type == "text")||(childNode.type == "textarea")) {
if(trim(childNode.value)=="") {
alert(returnAlertMessage(childNode)+childNode.errname);
childNode.focus();
return false;}}
if(childNode.type == "select-one") {
if(trim(childNode.value)=="") {
alert(returnAlertMessage(childNode)+childNode.errname);
childNode.focus();
return false;}}
if(childNode.type == "select-multiple") {
if(!validateMutipleDropdown(childNode)) {
alert(returnAlertMessage(childNode)+childNode.errname);
childNode.focus();
return false;}}
return true;
}
//function returns true if any multiple dropdown option is selected (except Select)
function validateMutipleDropdown(control) {
for(var i=0;i
if(control.options[i].selected == true) {
if((control.options[i].value != "")||(control.options[i].value != "0"))
return true;}
}
return false;
}
//function enables divs(multiple, give as comma seperated(no ' or ""))//
function DSAREnableDivs() {
var length = arguments.length;
for(var i=0;i
arguments[i].style.display="block";}
}
//function disables divs(multiple, give as comma seperated(no ' or ""))//
function DSARDisableDivs() {
var length = arguments.length;
for(var i=0;i
arguments[i].style.display="none";}
}
//function removes spaces and returns the string
function trim(str) {
return str.toString().replace(/(^\s*|\s*$)/gi,"");
}
//
function showPopupPage(url) {
window.open(url,"","height=400,width=600,toolbar=no,minimize=no,status=no,memubar=no,location=no, scrollbars=yes");
}
//function returns object of any element / control, given the id.
function getId(controlID) {
return document.getElementById(controlID);
}
/*Commonly Used Functions*/
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Src="AddMore.ascx" TagName="AddMore" TagPrefix="uc1" %>
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 runat="server">
<title>Custom Controlstitle>
head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<table cellpadding="0" cellspacing="0" width="100%" align="center" id="tblAddMorea">
<tr>
<td>
Add and delete demo:
td>
tr>
<tr>
<td>
<input type="text" />
td>
<td>
<input type="text" />
td>
<td>
<input type="text" />
td>
<td>
<input type="checkbox" />
td>
tr>
<uc1:AddMore ID="AddMore1" runat="server" />
table>
div>
ContentTemplate>
asp:UpdatePanel>
<input class="button" id="btnGets" type="button" value="Get Valuess" runat="server"
onclick="javascript:getAllValues('tblAddMorea')" />
form>
body>
html>
Default2.aspx.cs
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;
using System.Runtime.InteropServices;
public partial class Default2 : System.Web.UI.Page
{
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd,String Text,String Caption,uint type);
protected void Page_Load(object sender, EventArgs e)
{
//MessageBox(new IntPtr(0), "Hello World", "Hello Dialog", 0);
}
}
Happy Coding!!!!
Comments