简体   繁体   中英

asp.net button click event occurs only once

I am very new to ASP.NET but I have background in C and C#. I am trying to make web application which will be connected with my database. I did that and that is OK. The problem occurred when I added buttons for moving forward and backward through the database... It was working fine but only for one click! Then I thought that probably I have bug in the code and I decided to do it in easy way.

I created new web form with just one button and one label, but behavior is still the same- after first click event is not fired again. Any help?

My code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DummyTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
    int pom = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Convert.ToString(pom);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        pom++;
        Label1.Text = Convert.ToString(pom);
    }
    }
  }

and source

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
 Inherits="DummyTest.WebForm1" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
 <title></title>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>

    Dummy Test
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />

</div>
</form>
</body>

Your Webform class gets recreated on every request - that's just how it works. You need to persist the field pom in some other way. You could add it to the session state, but that affects scalability of the application as you then hold state on the server.

It is already persisted by the label control, so you could do something like this in your click event:

var pom = Int.Parse(Label1.Text);
pom++;
Label1.Text = pom.ToString();

However, this will not always be the case for something you want to persist. In these cases I would add a hidden field to the html which holds it. My WebForms is a little rusty, but in your markup:

<asp:HiddenField id="pom" value="0" />

then pull the value out and increment in the click event as I've done above.

Whenever you click your int pom = 0 get initialized to zero and then you set value that is why it looks it calling once. either make it static or better to try:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "" + (Convert.ToInt32(Label1.Text) + 1);
}

You could try moving your initialisation into an IsPostBack check, so reloading the page does not re-intitialise the variable to 0. The IsPostBack value indicates whether the page is being rendered for the first time or is being loaded in response to a postback and a button click event triggers a postback.

Additional
The IsPostBack fires at a specific stage in the page lifecycle

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
         int pom = 0;
    }
    Label1.Text = Convert.ToString(pom);
}

To some extent it depends what the purpose of the pom variable is. If it is tracking clicks per user then the above should help. However if you're tracking clicks across all users then you should consider something else (probably using the global.asax)

It is working now! Here is code for Up and Down.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DummyTest
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        static int pom;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
               pom = 0;
               Label1.Text = Convert.ToString(pom);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           var pom = Int32.Parse(Label1.Text);
           pom++;
           Label1.Text = Convert.ToString(pom);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
           var pom = Int32.Parse(Label1.Text);
           pom--;
           Label1.Text = Convert.ToString(pom);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM