Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8617889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:58:36+00:00 2026-06-12T05:58:36+00:00

The task is to count the number of clicks in a webform. Here is

  • 0

The task is to count the number of clicks in a webform.

Here is the code.

public partial class _default : System.Web.UI.Page
    {
        int count = 1;
        private void Page_Load(object sender, System.EventArgs e)
        {
            Button btn = new Button();
            btn.Text = "Click Me";
            btn.Click  += btn_Click;
            Label lbl = new Label();
            form1.Controls.Add(btn);
            form1.Controls.Add(lbl);


        }

        protected void btn_Click(object sender, EventArgs e)
        {
            count++;
            //Label1.Text = count.ToString();

        }
    }

The problem is that lbl is not recognised inside btn_Click() … I can’t figure it out… can anyone suggest?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:58:38+00:00Added an answer on June 12, 2026 at 5:58 am

    You need to declare lbl outside the page_load method than it will be available to you in button click like this.

    Label lbl ;
    int count = 1;
    
    private void Page_Load(object sender, System.EventArgs e)
    {
        Button btn = new Button();
        btn.Text = "Click Me";
        btn.Click  += btn_Click;
        lbl = new Label();
        form1.Controls.Add(btn);
        form1.Controls.Add(lbl);
    }
    
    protected void btn_Click(object sender, EventArgs e)
    {
        count++;
        if(lbl !=null)
          lbl.Text = count.ToString();            
    }
    

    Because if you create label in the page_load scope of the variable is limited to that method only. So either you declare the label outside the method like as I did or you can give id to label and than search it using FindControl method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(query updated for cdhowie comments) Here's the situation. I want to count the number
Ok, so my task was to modify this code to count both upper and
Task: To provide facility to upgrade the system remotely or add new features. What
Task at hand — I have three versions of some code, developed by different
Basically my task is to make the counter on a micro controller board count
The following function is giving me an out of bounds exception... public void count(){
I am trying to use the parallel task library to kick off a number
I have the following model: class Task(Model): solvers = ManyToManyField(User, related_name='slvrs') Now I want
I need to count the number of strokes of Chinese characters (UTF-8) in iOS,
I want a list of counts for some of my data (count the number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.