<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> <script> 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 = "<center>L <input type='text' value='0' size='1' id='intHeight" + intTextBox + "' name='intHeight" + intTextBox + "'/>" + "B <input type='text' value='0' size='1' id='intWidth" + intTextBox + "' name='intWidth" + intTextBox + "'/>" + "H <input type='text' value='0' size='1' id='intBreadth" + intTextBox + "' name='intBreadth" + intTextBox + "'/>" + "Pcs <input type='text' value='0' size='1' id='intPcs" + intTextBox + "' name='intPcs" + intTextBox + "'/></center>"; contentID.appendChild(newTBDiv); document.frm.txtMutiVolumetric.value = intTextBox; // document.getElementById('intHeight'+i).focus(); } //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= intTotal ^ * 1/3; // intTotal = document.frm.intHeight+i+.value } // opener.frm1.txtHeight.value = document.frm.intHeight1.value; // opener.frm1.txtWidth.value = document.frm.intWidth1.value; // opener.frm1.txtBreadth.value = document.frm.intBreadth1.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%>"); // document.frm.txtHeight.value = intTotal; //document.frm.txtLength.value = intTotal; // document.frm.txtBreadth.value = intTotal; txtTLength.value = intTotal; txtTBreadth.value = intTotal; txtTHeight.value = intTotal; // self.close(); } </script> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <asp:TextBox ID="txtAdd" runat="server"> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> <br /> <br /> <asp:Panel ID="pnlTextBoxes" runat="server"> <br /> <br /> <br /> <div id="content"> </div> <br /> <br /> <br /> <br /> <a href="java<!-- no -->script:addElement();"><font color="#000000">Add</font> </a>||<a href="java<!-- no -->script:removeElement();"><font color="#000000">Remove</font></a>||<a> href="java<!-- no -->script:sendInfo();"><font color="#000000">Calculate</font></a> <div id="Div1"> </div> <br /> <br /> <br /> L <asp:TextBox ID="txtTLength" runat="server" Width="85px" MaxLength="10" placeholder="L" ReadOnly="true"> x B <asp:TextBox ID="txtTBreadth" runat="server" Width="85px" MaxLength="10" placeholder="B" ReadOnly="true"> x H <asp:TextBox ID="txtTHeight" runat="server" Width="85px" MaxLength="10" placeholder="H" ReadOnly="true"> (In cm)
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 <br /> element. pnlTextBoxes.Controls.Add(new LiteralControl("<br />")); } } }
Quote:If you insert dynamic controls between existing controls, the dynamic control's view state information is inserted into the corresponding location of the view state structure. When the page is posted and the view state is loaded, the dynamic control does not yet exist; therefore, the extra information in view state does not correspond to the right control. The result is usually an error indicating an invalid cast. If you reinsert controls with each round trip, each generation of dynamically created controls will pick up property values from the view state of the preceding set of controls.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)