number display question

Discussion in 'ASP .Net' started by philip, Aug 14, 2006.

  1. philip

    philip Guest

    hello, i am confused about the following aspx page, when the Submit
    button is clicked, the page will append a number to Label1, but i am
    wondering why the first row displays twice, would u please tell me? how
    can i solve this problem? thanks.

    number: 0
    number: 0 <--- why display twice
    number: 1
    number: 2
    number: 3
    number: 4



    The aspx source:

    <script language="C#" runat="server">

    private int currentIndex;

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    ViewState["currentIndex"] = 0;
    }

    currentIndex = (int)ViewState["currentIndex"];

    if (currentIndex < 5)
    Label1.Text += "<br>number: " + currentIndex;

    lblCurrentIndex.Text = "current index: " + currentIndex +
    "<br>";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    ViewState["currentIndex"] = currentIndex + 1;
    }
    </script>

    <html>
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server"
    Width="205px"></asp:Label><br />
    <br />
    <asp:Label ID="lblCurrentIndex" runat="server"></asp:Label><br
    />
    <br />
    <asp:Button ID="btnSubmit" runat="server"
    OnClick="Button1_Click" Text="Submit" />

    </div>
    </form>
    </body>
    </html>
     
    philip, Aug 14, 2006
    #1
    1. Advertising

  2. PageLoad event fires before your Button1_Click event

    "philip" wrote:

    > hello, i am confused about the following aspx page, when the Submit
    > button is clicked, the page will append a number to Label1, but i am
    > wondering why the first row displays twice, would u please tell me? how
    > can i solve this problem? thanks.
    >
    > number: 0
    > number: 0 <--- why display twice
    > number: 1
    > number: 2
    > number: 3
    > number: 4
    >
    >
    >
    > The aspx source:
    >
    > <script language="C#" runat="server">
    >
    > private int currentIndex;
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!IsPostBack)
    > {
    > ViewState["currentIndex"] = 0;
    > }
    >
    > currentIndex = (int)ViewState["currentIndex"];
    >
    > if (currentIndex < 5)
    > Label1.Text += "<br>number: " + currentIndex;
    >
    > lblCurrentIndex.Text = "current index: " + currentIndex +
    > "<br>";
    > }
    >
    > protected void Button1_Click(object sender, EventArgs e)
    > {
    > ViewState["currentIndex"] = currentIndex + 1;
    > }
    > </script>
    >
    > <html>
    > <head runat="server">
    > <title>Untitled Page</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <asp:Label ID="Label1" runat="server"
    > Width="205px"></asp:Label><br />
    > <br />
    > <asp:Label ID="lblCurrentIndex" runat="server"></asp:Label><br
    > />
    > <br />
    > <asp:Button ID="btnSubmit" runat="server"
    > OnClick="Button1_Click" Text="Submit" />
    >
    > </div>
    > </form>
    > </body>
    > </html>
    >
    >
     
    =?Utf-8?B?TWlsbGVy?=, Aug 14, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Imran Aziz
    Replies:
    4
    Views:
    1,516
    Imran Aziz
    Aug 11, 2005
  2. Marc Twain

    Display and display model

    Marc Twain, Jan 13, 2004, in forum: Java
    Replies:
    1
    Views:
    382
    Matt Humphrey
    Jan 13, 2004
  3. SG
    Replies:
    6
    Views:
    30,413
    ricuzzo
    Jan 15, 2008
  4. blank101
    Replies:
    2
    Views:
    536
    blank101
    Feb 5, 2005
  5. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    How to: display page, post back, display again

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Jul 14, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,027
    Walter Wang [MSFT]
    Jul 14, 2006
Loading...

Share This Page