动态创建按钮单击文本框

动态创建按钮单击文本框

本文介绍了动态创建按钮单击文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个按钮,从而被点击时,它应该在一行显示3文本框。然而,每一次我点击提交按钮时,该文本框将显示2行(这是6文本框)。并随后在每一次点击增加2行。

反正是有,它可能只是增加文本框的一行在每一次点击?

下面是code盈:

 <表ID =form1的=服务器>
< D​​IV>    < ASP:按钮的ID =Button1的=服务器文本=按钮的onclick =的button1_Click/>
    < ASP:面板ID =Panel1的=服务器>
    < / ASP:面板>< / DIV>
< /表及GT;

code背后:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
       {
        //添加已pviously动态添加$ P $任何控件
         的for(int i = 0; I< TotalNumberAdded;我++)
        {
        AddControls第(i + 1);
        }    //附加事件处理程序的按钮
    Button1.Click + =新的EventHandler(的button1_Click);
}保护无效的button1_Click(对象发件人,EventArgs的发送)
{    //增加增加的号码,并添加新的标签和文本框
    TotalNumberAdded ++;
    AddControls(TotalNumberAdded);}私人无效AddControls(INT控制编号)
{        文本框TxtBoxU =新的TextBox();        文本框TxtBoxE =新的TextBox();        文本框TxtBoxY =新的TextBox();        标签lblU =新的Label();
        标签lblE =新的Label();
        标签lblY =新的Label();        TxtBoxU.ID =TextBoxU+控制编号;
        TxtBoxE.ID =TextBoxE+控制编号;
        TxtBoxY.ID =TextBoxY+控制编号;        lblU.ID =LabelU+控制编号;
        lblE.ID =LabelE+控制编号;
        lblY.ID =LabelY+ +控制编号;        lblU.Text =用户:;
        lblE.Text =电子邮件;
        lblY.Text =电话号码;        //标签和文本框添加到面板。
        Panel1.Controls.Add(lblU);
        Panel1.Controls.Add(TxtBoxU);
        Panel1.Controls.Add(TxtBoxU);        Panel1.Controls.Add(lblE);
        Panel1.Controls.Add(TxtBoxE);        Panel1.Controls.Add(lblY);
        Panel1.Controls.Add(TxtBoxY);
        Panel1.Controls.Add(新LiteralControl(< BR>中));   }保护INT TotalNumberAdded
{
    {返回(INT)(的ViewState [TotalNumberAdded] ?? 0); }
    集合{的ViewState [TotalNumberAdded] =值; }
}


解决方案

您的问题是在你的页面加载,becouse你的单击事件 declaretion是多余的,您要连接的事件处理程序当你被HTML assinging,如果你把点击的情况下,这是火灾2次。

删除

  Button1.Click + =新的EventHandler(的button1_Click);

和你的页面加载会是这样的:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
   {
    //添加已pviously动态添加$ P $任何控件
     的for(int i = 0; I< TotalNumberAdded;我++)
    {
      AddControls第(i + 1);
    }  }

我希望帮助。

P.S:我在我的第一个测试对不起,我没有测试权

I have created a button whereby when it is clicked, it should display 3 textboxes in a row. However, every time i click the submit button, the textbox would display 2 rows(which is 6 textboxes). And subsequently increase 2 rows at every click.

Is there anyway that it could just increase one row of textbox at every click??

Here is the code infront:

<form id="form1" runat="server">
<div>

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>

</div>
</form>

Code Behind:

         protected void Page_Load(object sender, EventArgs e)
       {
        // Add any controls that have been previously added dynamically
         for (int i = 0; i < TotalNumberAdded;  i++)
        {
        AddControls(i + 1);
        }

    // Attach the event handler to the button
    Button1.Click += new EventHandler(Button1_Click);
}

protected void Button1_Click(object sender, EventArgs e)
{

    // Increase the number added and add the new label and textbox
    TotalNumberAdded++;
    AddControls(TotalNumberAdded);

}

private void AddControls(int controlNumber)
{

        TextBox TxtBoxU = new TextBox();

        TextBox TxtBoxE = new TextBox();

        TextBox TxtBoxY = new TextBox();

        Label lblU = new Label();
        Label lblE = new Label();
        Label lblY = new Label();

        TxtBoxU.ID = "TextBoxU" + controlNumber;
        TxtBoxE.ID = "TextBoxE" + controlNumber;
        TxtBoxY.ID = "TextBoxY" + controlNumber;

        lblU.ID = "LabelU" + controlNumber;
        lblE.ID = "LabelE" + controlNumber;
        lblY.ID = "LabelY" + +controlNumber;

        lblU.Text = "User : ";
        lblE.Text = "E-Mail : ";
        lblY.Text = "Phone number : ";

        //Add the labels and textboxes to the Panel.
        Panel1.Controls.Add(lblU);
        Panel1.Controls.Add(TxtBoxU);
        Panel1.Controls.Add(TxtBoxU);

        Panel1.Controls.Add(lblE);
        Panel1.Controls.Add(TxtBoxE);

        Panel1.Controls.Add(lblY);
        Panel1.Controls.Add(TxtBoxY);
        Panel1.Controls.Add(new LiteralControl("<br>"));

   }

protected int TotalNumberAdded
{
    get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
    set { ViewState["TotalNumberAdded"] = value; }
}
解决方案

Your problem is in your page load, becouse your click event declaretion is redundant, you are attaching the EventHandler when you are assinging by HTML, And If you push click the event, It is fires 2 time.

Remove

Button1.Click += new EventHandler(Button1_Click);

And you page load will looks like:

   protected void Page_Load(object sender, EventArgs e)
   {
    // Add any controls that have been previously added dynamically
     for (int i = 0; i < TotalNumberAdded;  i++)
    {
      AddControls(i + 1);
    }

  }

I hope that help.

P.S: I'm sorry in my first test I did not test right.

这篇关于动态创建按钮单击文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 14:05