Problem when converting int to string

Asked

Viewed 533 times

2

I’m trying to do a click counter in ASP.NET webforms and c#, and I need to convert the number int click-through to a string in label. Only this does not work, the page is displayed but the number of clicks (a 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 it doesn’t do is display the one of clicks, the rest appears.

  • Try changing the line that puts value on label for Label1.Text = "clicks aqui"; to see if the problem is the same conversion.

  • @Moustache label doesn’t look the same

  • @And I’m gonna show you how I do it?

  • give more details, we’re not seeing your code work, you have to explain it right. But if you have changed and nothing appears, your problem is not the conversion, so change the definition of your problem or the question needs to be closed because it is not possible to play it. For me the button is not running. Put something in the method to indicate that you passed there. Go thrashing to see if you enter this method. There is no missing one onClick="btn_Click"?"`

  • @mustache what else you need?

3 answers

2


Your problem is not the conversion, you just forgot to link the event onClick to your button. Add this to the button 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 in a global variable will not work. I would change the event onClick to take 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();
}

Obs.: Thus, the value of Label1.Text should be 0 when loading the page, if Label1.Text is different from 0, it would be better to do

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();
}
  • Okay, perfect, but my accountant doesn’t come out of 1

  • Remains the same.

  • I edited the answer.

  • Nope, gave an Exception

  • Who Exception? Conversion error? If so, look at the answer.

  • When I press the button, http://imgur.com/jeevPPpappears

  • It worked, corrected

Show 2 more comments

2

From what I understand the problem is another. The method is not being called because there is no event for it in the definition of the page, would be 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>

I put in the Github for future reference.

2

That way you will not succeed in storing the value of each increment, you can play 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();
            }

        }
    }
}

Observing: in your code failed to link the event from the button to the code, it’s simple go to the design mode and double-click the button.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.