单击链接按钮时,文本框将消失

时间:2013-12-11 18:35:42

标签: c# asp.net

我有问题。我正在尝试制作一些面板,在这些面板中我想要一些链接按钮,当用户单击面板的链接按钮时,文本框将出现在该面板中,当我单击另一个链接按钮时该面板的文本框也看起来没有问题但是当我点击另一个链接按钮时,在前一个面板中创建的texbox会消失。

这是我的代码:

public partial class _Default : Page
    {
        Label myLabel1;
        Label myLabel2;
        protected void Page_Load(object sender, EventArgs e)
        {
            myLabel1 = new Label();
            myLabel2 = new Label();
            Panel1.Controls.Add(myLabel1);
            Panel2.Controls.Add(myLabel2);
            if (!Page.IsPostBack)
            {
                //Remove the session when first time page loads.
                Session.Remove("clicks");
                Session.Remove("clicks2");
            }

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            int rowCount = 0;
            //initialize a session.
            rowCount = Convert.ToInt32(Session["clicks"]);
            rowCount++;
            //In each button clic save the numbers into the session.
            Session["clicks"] = rowCount;
            //Create the textboxes and labels each time the button is clicked.
            for (int i = 0; i < rowCount; i++)
            {
                TextBox TxtBoxU = new TextBox();
                TxtBoxU.ID = "TextBoxU" + i.ToString();
                //Add the labels and textboxes to the Panel.
                Panel1.Controls.Add(TxtBoxU);

            }
            myLabel1.Text = rowCount + "";

        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            int rowCount2 = 0;
            //initialize a session.
            rowCount2 = Convert.ToInt32(Session["clicks2"]);
            rowCount2++;
            //In each button clic save the numbers into the session.
            Session["clicks2"] = rowCount2;
            //Create the textboxes and labels each time the button is clicked.
            for (int i = 0; i < rowCount2; i++)
            {

                TextBox TxtBoxU = new TextBox();
                TxtBoxU.ID = "TextBoxU" + i.ToString();
                //Add the labels and textboxes to the Panel.
                Panel2.Controls.Add(TxtBoxU);

            }
            myLabel2.Text = rowCount2 + "";
        }
    }

这是另一部分:

<form id="form1" runat="server">
    <p>
        Part I</p>
    <asp:Panel ID="Panel1" runat="server" Height="53px">
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Add to 1</asp:LinkButton>
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server" Height="51px">
        <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Add to 2</asp:LinkButton>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </asp:Panel>
</form>

3 个答案:

答案 0 :(得分:1)

由于文本框是动态创建的,当页面加载时,所有内容都需要重新构建,但是当您面板的特定链接按钮正在进行时,您发布的代码只会重新构建其中一个面板的文本框。点击。但是,您的代码只是再次构建一个面板,因为这就是链接按钮的单击事件处理程序告诉它的所有操作。

我建议将逻辑组合到构建构建的单个方法中,然后每个链接按钮单击事件处理程序可以更新自己的计数并将其传递给方法,如下所示:

private void BuildTextBoxes(int rowCount1, int rowCount2)
{
    for (int i = 0; i < rowCount; i++)
    {
        TextBox TxtBoxU = new TextBox();
        TxtBoxU.ID = "TextBoxU" + i.ToString();
        //Add the labels and textboxes to the Panel.
        Panel1.Controls.Add(TxtBoxU);
    }

    myLabel1.Text = rowCount + "";

    for (int i = 0; i < rowCount2; i++)
    {
        TextBox TxtBoxU = new TextBox();
        TxtBoxU.ID = "TextBoxU" + i.ToString();
        //Add the labels and textboxes to the Panel.
        Panel2.Controls.Add(TxtBoxU);
    }

    myLabel2.Text = rowCount2 + "";
}

现在,在链接按钮中单击事件处理程序,您需要更新一个计数,但将两者都传递给方法,如下所示:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    int rowCount = 0;
    //initialize a session.
    rowCount = Convert.ToInt32(Session["clicks"]);
    rowCount++;
    //In each button clic save the numbers into the session.
    Session["clicks"] = rowCount;

    BuildTextBoxes(rowCount, Convert.ToInt32(Session["clicks2"]));
}

protected void LinkButton2_Click(object sender, EventArgs e)
{
    int rowCount2 = 0;
    //initialize a session.
    rowCount2 = Convert.ToInt32(Session["clicks2"]);
    rowCount2++;
    //In each button clic save the numbers into the session.
    Session["clicks2"] = rowCount2;

    BuildTextBoxes(Convert.ToInt32(Session["clicks1"]), rowCount2));
}

现在,无论您是单击第一个还是第二个链接按钮,都将重新创建所有文本框;只有特定的链接按钮增加行数。

答案 1 :(得分:0)

当点击链接后回到服务器后,会生成一个全新的HTML页面并返回给客户端。在代码中的单击事件处理程序中,您将向aspx标记中的控件添加其他控件。然后将这些包含在返回给客户端的HTML中。但是服务器是无状态的,并且不会记住您在下次有来自客户端的请求时添加了这些服务器。每次有回发后,您都需要再次添加它们。

答案 2 :(得分:0)

也许尝试移动您的逻辑,将文本框添加到单独的方法中,然后在您的点击中使用适当的值调用它。

这样的事情:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    int rowCount = Convert.ToInt32(Session["clicks"]) + 1;
    Session["clicks"] = rowCount;
    int rowCount2 = Convert.ToInt32(Session["clicks2"]) + 1;
    Session["clicks2"] = rowCount2;
    AddTextBoxes(rowCount, Panel1, myLabel1);
    AddTextBoxes(rowCount2, Panel2, myLabel2);
}

protected void LinkButton2_Click(object sender, EventArgs e)
{
    int rowCount = Convert.ToInt32(Session["clicks"]) + 1;
    Session["clicks"] = rowCount;
    int rowCount2 = Convert.ToInt32(Session["clicks2"]) + 1;
    Session["clicks2"] = rowCount2;
    AddTextBoxes(rowCount, Panel1, myLabel1);
    AddTextBoxes(rowCount2, Panel2, myLabel2);
}

protected void AddTextBoxes(int numberToAdd, Panel panel, Label label)
{
    //In each button clic save the numbers into the session.
    numberToAdd = rowCount;
    //Create the textboxes and labels each time the button is clicked.
    for (int i = 0; i < rowCount; i++)
    {
        TextBox TxtBoxU = new TextBox();
        TxtBoxU.ID = "TextBoxU" + i.ToString();
        //Add the labels and textboxes to the Panel.
        panel.Controls.Add(TxtBoxU);

    }
    label.Text = rowCount + "";
}