如何在c#.net中创建文本框运行时以及如何添加运行时生成tex框的值? [英] How to create text box runtime in c#.net and how to add values of runtime generate tex box ?

查看:81
本文介绍了如何在c#.net中创建文本框运行时以及如何添加运行时生成tex框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我无法添加运行时生成的文本框的值??

我已经通过javascript函数完成了它,但它的价值在简单的页面回发中丢失了我想做同样的事情通过c#代码,请帮助???



我尝试过:



 <%@       标题  = 主页   语言  =  C#    MasterPageFile   = 〜/ Site.master    AutoEventWireup   =  true  

CodeFile = Default.aspx.cs 继承 = _默认 %>

< < span class =code-leadattribute> asp:Content ID = HeaderContent runat = server ContentPlaceHolderID = HeadContent >
< script >

var intTextBox = 0 ;

// 添加文本框元素的功能

function addElement(){
intTextBox = intTextBox + 1 ;

var contentID = document.getElementById(' 含量);

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'mall ='1'id ='intWidth + intTextBox + 'name ='intWidth + intTextBox + '/> + H < input type ='text'value ='0'mode ='1'id ='intBreadth + intTextBox + 'name ='intBreadth + intTextBox + '/ gt ; + Pcs< input type ='text'value ='0'statory ='1 'id ='intPcs + intTextBox + 'name ='intPcs + intTextBox + '/>< / center>;

contentID.appendChild(newTBDiv);

document.frm.txtMutiVolumetric。 value = intTextBox;

// document.getElementById('intHeight'+ i).focus();
}



// 删除文本框元素的功能

函数removeElement(){
if (intTextBox!= 0 ){
var contentID = document.getElementById(' content');

contentID.removeChild(document.getElementById(' intData' + intTextBox) );

intTextBox = intTextBox - 1 ;

document.frm.txtMutiVolumetric。 value = intTextBox;
}
}


函数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 = 主要内容 >
< asp:TextBox ID = txtAdd runat = < span class =code-keyword> server
>
< asp:按钮 ID = Button1 runat = server 文字 = 按钮 OnClick = Button1_Click / >
< br / >
< br / >
< asp:Panel ID = pnlTextBoxes runat < span class =code-keyword> =
server >

< br / >
< br / >
< br / >
< div id = content >
< / div >
< br < span class =code-attribute>
/ >
< br / >
< br / >
< br / >
< a href = java<! - no - > script:addElement(); > < font 颜色 = #000000 > 添加< / font >
< / a > || < a href = java<! - no - > script:removeElement(); > < /温泉n> < font 颜色 = #000000 > 删除< / font > < / a < span class =code-keyword>> || < a >
href =java <! - no - > 脚本:sendInfo();> < font color = #000000 > 计算< / font > < / a >
< div id = Div1 >
< / div >
< br / &g t;
< br / >
< br / >
L < asp:TextBox ID = < span class =code-keyword> txtTLength
runat = server 宽度 = 85px MaxLength = 10 占位符 = < span class =code-keyword> L


< span class =code-attribute>
ReadOnly = true >
x B < asp:TextBox ID = txtTBreadth runat = server 宽度 = 85px MaxLength = 10

占位符 = B ReadOnly = true >
x H < asp:TextBox ID = txtTHeight runat = server 宽度 = 85px MaxLength = 10

占位符 < span class =code-keyword> =
H ReadOnly = true > (以厘米为单位)



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

public partial class _Default:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

}


< span class =code-keyword> protected void Button1_Click( object sender,EventArgs e )
{
int count = Convert.ToInt32(txtAdd.Text.Trim());

for int i = 0 ; i < count; i ++)
{
Label MyLabel = 标签();

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);



标签MyLabel2 = 标签();

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);



标签MyLabel3 = 标签();

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);



标签MyLabel4 = 标签();

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( ));

// 以HTML格式添加间隔< br />元素。
pnlTextBoxes.Controls.Add( new LiteralControl( < br />));
}
}
}

解决方案

您需要在页面加载中再次创建这些控件。

参考 - 动态Web服务器控件和查看状态 [< a href =https://msdn.microsoft.com/en-us/library/hbdfdyh7.aspxtarget =_ blanktitle =New Window> ^ ]

Quote:

如果在现有控件之间插入动态控件,则动态控件的视图状态信息将插入到视图状态结构的相应位置。发布页面并加载视图状态时,动态控件尚不存在;因此,视图状态中的额外信息不对应于正确的控制。结果通常是指示无效转换的错误。

如果每次往返重新插入控件,则每一代动态创建的控件都将从前一组控件的视图状态中获取属性值。


My problem is that I m unable to add the values of runtime generated textboxes ??
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 ???

What I have tried:

<%@ 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 /><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 />"));
        }
    }
}

解决方案

You need to create those controls again in the Page Load.
Refer - Dynamic Web Server Controls and View State[^]

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.


这篇关于如何在c#.net中创建文本框运行时以及如何添加运行时生成tex框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆