Problem converting int to string

2

I'm trying to do a click counter in ASP.NET webforms and C #, and I need to convert the% number of clicks to a string in the label . But this does not work, the page is displayed but the number of clicks (the label ) does not appear.

Code:

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

namespace Teste
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        int clicks = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_Click(object sender, EventArgs e)
        {
            clicks++;
            Label1.Text = clicks.ToString();
        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Teste.WebForm2" %>
  <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">

  <head runat="server">
    <title>Clickount</title>
    <link rel="stylesheet" href="bootstrap.css" />
  </head>

  <body>
    <form runat="server">
      <center>
        <h1>Click counter</h1>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />&nbsp;
        <asp:Button ID="btn" runat="server" Text="Add" CssClass="btn btn-primary"></asp:Button>
    </form>
  </body>

  </html>

The only thing you do not do is display the number of clicks, the rest appears.

    
asked by anonymous 12.10.2015 / 22:39

3 answers

2

Your problem is not conversion, you just forgot to bind the onClick event to your button. Add this in the button's declaration

OnClick="btn_Click"

Putting in the code

<asp:Button ID="btn" runat="server" Text="Add" OnClick="btn_Click" CssClass="btn btn-primary"></asp:Button>

I have no experience with WebForms , but I think saving the value of clicks to a global variable will not work. I would change the event onClick to get the existing value of label .

protected void btn_Click(object sender, EventArgs e)
{
    int click = Convert.ToInt32(Label1.Text); //Se não tiveres certeza do valor de Label1.Text, use o TryParse()
    click++;
    Label1.Text = click.ToString();
}

Note: Thus, the value of Label1.Text must be 0 when loading the page, if Label1.Text is different from 0,

protected void btn_Click(object sender, EventArgs e)
{
    int click;
    int.TryParse(Label1.Text, out click); // Se a conversão falhar, click recebe 0
    click++;
    Label1.Text = click.ToString();
}
    
12.10.2015 / 22:54
2

From what I understand the problem is another. The method is not being called because there is no event for it in the page definition, it would look like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Teste.WebForm2" %>
  <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">

  <head runat="server">
    <title>Clickount</title>
    <link rel="stylesheet" href="bootstrap.css" />
  </head>

  <body>
    <form runat="server">
      <center>
        <h1>Click counter</h1>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />&nbsp;
        <asp:Button ID="btn" runat="server" Text="Add" OnClick="btn_Click" CssClass="btn btn-primary"></asp:Button>
    </form>
  </body>

  </html>
    
12.10.2015 / 22:55
2

In this way you will not succeed in saving the value of each increment, you can throw the information directly inside the Label in this specific case:

<h1>Click counter</h1>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="btn" runat="server" 
        Text="Add" CssClass="btn btn-primary" 
        OnClick="btn_Click">
</asp:Button>

Code:

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

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Label1.Text = "0";
            }
        }
        protected void btn_Click(object sender, EventArgs e)
        {
            int quant;
            if (int.TryParse(Label1.Text, out quant))
            {
                quant++;
                Label1.Text = quant.ToString();
            }

        }
    }
}

Note: In your code, the button event has to be linked to the code, it goes straight to design mode and double-clicks the button.

    
12.10.2015 / 22:57