I have done it through javascript function but it's value get lost on simple page postback i want to do the same through c# code please help ???
Following are codes i have used
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
var intTextBox = 0;
//FUNCTION TO ADD TEXT BOX ELEMENT
function addElement() {
intTextBox = intTextBox + 1;
var contentID = document.getElementById('content');
var newTBDiv = document.createElement('div');
newTBDiv.setAttribute('id', 'intData' + intTextBox);
newTBDiv.innerHTML = "L " + "B " + "H " + "Pcs ";
contentID.appendChild(newTBDiv);
document.frm.txtMutiVolumetric.value = intTextBox;
}
//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeElement() {
if (intTextBox != 0) {
var contentID = document.getElementById('content');
contentID.removeChild(document.getElementById('intData' + intTextBox));
intTextBox = intTextBox - 1;
document.frm.txtMutiVolumetric.value = intTextBox;
}
}
function sendInfo() {
var intTotal;
intTotal = 0;
for (i = 1; i <= intTextBox; i++) {
intTotal = intTotal + parseFloat(document.getElementById('intHeight' + i).value) * parseFloat(document.getElementById('intPcs' + i).value) * parseFloat(document.getElementById('intWidth' + i).value) * parseFloat(document.getElementById('intBreadth' + i).value);
}
intTotal = Math.pow(intTotal, 1 / 3);
var txtTLength = document.getElementById("<%=txtTLength.ClientID%>");
var txtTBreadth = document.getElementById("<%=txtTBreadth.ClientID%>");
var txtTHeight = document.getElementById("<%=txtTHeight.ClientID%>");
txtTLength.value = intTotal;
txtTBreadth.value = intTotal;
txtTHeight.value = intTotal;
}
||Remove||
href="javascript:sendInfo();">Calculate
L
ReadOnly="true">
x B
placeholder="B" ReadOnly="true">
x H
placeholder="H" ReadOnly="true"> (In cm)
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int count = Convert.ToInt32(txtAdd.Text.Trim());
for (int i = 0; i < count; i++)
{
Label MyLabel = new Label();
TextBox text1 = new TextBox();
MyLabel.ID = "MyLabel" + i + 1;
text1.ID = "Text" + i + 1;
text1.Text = "0";
MyLabel.Text = "L";
pnlTextBoxes.Controls.Add(new LiteralControl(" "));
pnlTextBoxes.Controls.Add(MyLabel);
pnlTextBoxes.Controls.Add(text1);
Label MyLabel2 = new Label();
TextBox text2 = new TextBox();
MyLabel2.ID = "MyLabel" + i + 2;
text2.ID = "Text" + i + 2;
text2.Text = "0";
MyLabel2.Text = "B";
pnlTextBoxes.Controls.Add(new LiteralControl(" "));
pnlTextBoxes.Controls.Add(MyLabel2);
pnlTextBoxes.Controls.Add(text2);
Label MyLabel3 = new Label();
TextBox text3 = new TextBox();
MyLabel3.ID = "MyLabel" + i + 3;
text3.ID = "Text" + i + 3;
text3.Text = "0";
MyLabel3.Text = "H";
pnlTextBoxes.Controls.Add(new LiteralControl(" "));
pnlTextBoxes.Controls.Add(MyLabel3);
pnlTextBoxes.Controls.Add(text3);
Label MyLabel4 = new Label();
TextBox text4 = new TextBox();
MyLabel4.ID = "MyLabel" + i + 4;
text4.ID = "Text" + i + 4;
text4.Text = "0";
MyLabel4.Text = "Pcs";
pnlTextBoxes.Controls.Add(new LiteralControl(" "));
pnlTextBoxes.Controls.Add(MyLabel4);
pnlTextBoxes.Controls.Add(text4);
pnlTextBoxes.Controls.Add(new LiteralControl(" "));
//Add a spacer in the form of an HTML
element.
element.
pnlTextBoxes.Controls.Add(new LiteralControl("
"));
"));
}
}
}
5 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
ali tuncerPosted Feb 18, 2016, 1:35 AM
Dynamically created controls disappear after post back, you will need to use ViewState..
Rohit kumarPosted Feb 20, 2016, 12:56 AM
Rohit kumarPosted Feb 20, 2016, 12:55 AM
Rohit kumarPosted Feb 20, 2016, 12:50 AM
Shubham KumarPosted Feb 17, 2016, 11:58 PM