动态文本框和动态标签

时间:2011-11-14 07:03:11

标签: c# asp.net .net textbox label

我创建了3个动态texbox。在运行时我想在该文本框中输入一些值。如果单击该按钮,则输入的文本框值应显示在一个动态标签控件中。

请帮我一些样品

3 个答案:

答案 0 :(得分:1)

使用Page.FindControl访问动态创建的控件

答案 1 :(得分:0)

你可以在客户端做这件事,假设你使用jquery:

<span name="display" id="display"></span>
<input name="text1" id="text1" />
<input name="text2" id="text2" />
<input name="text3" id="text3" />

<input type="button" id="button1" />

<script>
    $("#button1").click(function() { 
        $("#display").html($("#text1").val() + $("#text2").val() + $("#text3").val());
    });

</script>

答案 2 :(得分:0)

在asp.net中试用此代码

.CS文件

protected override void OnPreInit(EventArgs e)
{

    Label lbl = new Label();
    lbl.ID = "mylbl";
    lbl.ClientIDMode = System.Web.UI.ClientIDMode.Static;
    form1.Controls.Add(lbl);
    for (int i = 0; i < 3; i++)
    {
        TextBox txt = new TextBox();
        txt.ID = "txt" + i;
        form1.Controls.Add(txt);
    }

}



protected void Button1_Click(object sender, EventArgs e)
    {
        Label lbl = form1.FindControl("mylbl") as Label;
        lbl.Text = "";
        for (int i = 0; i < 3; i++)
        {
            TextBox txt = form1.FindControl("txt" + i) as TextBox;


            lbl.Text += txt.Text;
        }
    }

.aspx文件

 <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>

    </form>
相关问题