0

I'm having a very annoying issue and some help will be very appreciate.
i am creating dynamically text boxes, and with a button i want to create another text box and to save the text which inserted to the last text box. The thing is when the handler event for the button is called, the page load is called before, and re-creates the page again.
then i lose the dynamic text-boxes i created. I even tried to save the text-boxes or theirs id in a session but the problem i cant get the text in the last text box because the page load called before.

What can i do in order to save my text before the page load, or can i cancel the page load from creating my page again?

I added my code here:

int i = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["orderList"] = null;
        }
    }

    public void create()
    {
            List<Order> orderList = (List<Order>)Session["orderList"];    // get the order list
            TextBox1.Text = orderList[0].Number_Of_Items.ToString();
            TextBox2.Text = orderList[0].PricePerItem;

            for (i = 1; i < orderList.Count; i++)
            {
                Label lb = new Label();
                lb.ID = "lb" + i.ToString();
                lb.Text = "item" + (i + 1).ToString() + ":";
                Panel1.Controls.Add(lb);

                DropDownList ddl = new DropDownList();
                ddl.ID = "ddl" + i.ToString();
                ListItem li = new ListItem();
                li.Text = "12345";
                li.Value ="5";
                ddl.Items.Add(li);
                Panel1.Controls.Add(ddl);

                Label lb2 = new Label();
                lb2.ID = "lb2" + i.ToString();
                lb2.Text = "Amount:";
                Panel1.Controls.Add(lb2);

                TextBox tb = new TextBox();
                tb.ID = "tb" + i.ToString();
                tb.Width = 110;
                tb.Text = orderList[i].Number_Of_Items.ToString();
                Panel1.Controls.Add(tb);

                Label lb3 = new Label();
                lb3.ID = "lb3" + i.ToString();
                lb3.Text = "Price Per Item:";
                Panel1.Controls.Add(lb3);

                TextBox tb1 = new TextBox();
                tb1.ID = "tb" + i.ToString();
                tb1.Width = 110;
                tb1.Text = orderList[i].PricePerItem;
                Panel1.Controls.Add(tb1);

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

            // Adding another empty order 

            Label lbLast = new Label();
            lbLast.ID = "lbLast";
            lbLast.Text = "item" + (i + 1).ToString() + ":";
            Panel1.Controls.Add(lbLast);

            DropDownList ddlLast = new DropDownList();
            ddlLast.ID = "ddlLast";
            ListItem liLast = new ListItem();
            liLast.Text = "12345";
            liLast.Value = "5";
            ddlLast.Items.Add(liLast);
            Panel1.Controls.Add(ddlLast);

            Label lbLast1 = new Label();
            lbLast1.ID = "lbLast1";
            lbLast1.Text = "Amount:";
            Panel1.Controls.Add(lbLast1);

            TextBox tbLast = new TextBox();
            tbLast.ID = "tbLast" + (i + 1).ToString();
            tbLast.Width = 110;
            Panel1.Controls.Add(tbLast);

            Label lbLast2 = new Label();
            lbLast2.ID = "lbLast2";
            lbLast2.Text = "Price Per Item:";
            Panel1.Controls.Add(lbLast2);

            TextBox tbLast1 = new TextBox();
            tbLast1.ID = "tbLast1";
            tbLast1.Width = 110;
            Panel1.Controls.Add(tbLast1);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<Order> list = new List<Order>();
        Order order = new Order();

        if (Session["orderList"] == null)      // takes data from .aspx
        {
            order.Number_Of_Items = Int32.Parse(TextBox1.Text);
            order.PricePerItem = TextBox2.Text;

            list.Add(order);
            Session["orderList"] = list;
            List<Order> orderList = (List<Order>)Session["orderList"];
            create();
        }
        else    // takes data from last order
        {

            list = (List<Order>)Session["orderList"];
            order.Number_Of_Items = list.Last().Number_Of_Items;
            order.PricePerItem = list.Last().PricePerItem;
            list.Add(order);
            Session["orderList"] = list;
            create();
        }
    }


<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Item1:"></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server">
    <asp:ListItem Value="1">BBB</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label3" runat="server" Text="Amount:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="110px"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Text="Price Per Item:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="110px"></asp:TextBox>
<br /><br />

strong text

4

1 回答 1

0

创造(); 应该在每次回发时调用。这将重新创建所有动态创建的控件并维护其上的值。

这是将执行您正在尝试执行的操作的代码。

    int i = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Orders"] = 1;
        }
        else
        {
            create();
        }
    }

   public void create()
    {
        for (i = 0; i < (int)Session["Orders"]; i++)
        {
            Label lb = new Label();
            lb.ID = "lb" + i.ToString();
            lb.Text = "item" + (i + 1).ToString() + ":";
            Panel1.Controls.Add(lb);

            DropDownList ddl = new DropDownList();
            ddl.ID = "ddl" + i.ToString();
            ListItem li = new ListItem();
            li.Text = "12345";
            li.Value = "5";
            ddl.Items.Add(li);
            Panel1.Controls.Add(ddl);

            Label lb2 = new Label();
            lb2.ID = "lb2" + i.ToString();
            lb2.Text = "Amount:";
            Panel1.Controls.Add(lb2);

            TextBox tb = new TextBox();
            tb.ID = "tb" + i.ToString();
            tb.Width = 110;
            Panel1.Controls.Add(tb);

            Label lb3 = new Label();
            lb3.ID = "lb3" + i.ToString();
            lb3.Text = "Price Per Item:";
            Panel1.Controls.Add(lb3);

            TextBox tb1 = new TextBox();
            tb1.ID = "tb1" + i.ToString();
            tb1.Width = 110;
            Panel1.Controls.Add(tb1);

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

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
           Session["Orders"] = (int)Session["Orders"] + 1;
    }
于 2013-08-23T14:16:02.307 回答