TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Rohit kumar
1.6k
98
5.9k
asp.net
Feb 17 2016 11:23 PM
My problem is that I m unable to add the values of runtime generated textboxes in
asp.net
??
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" %>
<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;
}
//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;
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="txtAdd" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<br />
<asp:Panel ID="pnlTextBoxes" runat="server">
</asp:Panel>
<br />
<br />
<div id="content">
</div>
<br />
<br />
<a href="javascript:addElement();"><strong><font color="#000000">Add</font> </strong>
</a>||<a href="javascript:removeElement();"><strong><font color="#000000">Remove</font></strong></a>||<a
href="javascript:sendInfo();"><strong><font color="#000000">Calculate</font></strong></a>
<div id="Div1">
</div>
<br />
<br />
L <asp:TextBox ID="txtTLength" runat="server" Width="85px" MaxLength="10" placeholder="L"
ReadOnly="true"></asp:TextBox>
x B <asp:TextBox ID="txtTBreadth" runat="server" Width="85px" MaxLength="10"
placeholder="B" ReadOnly="true"></asp:TextBox>
x H <asp:TextBox ID="txtTHeight" runat="server" Width="85px" MaxLength="10"
placeholder="H" ReadOnly="true"></asp:TextBox> (In cm)
</asp:Content>
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 <br /> element.
pnlTextBoxes.Controls.Add(new LiteralControl("<br />"));
}
}
}
Reply
Answers (
5
)
How to get specific string on an html?
SharePoint Not working properly