button click evert error and multiple lablebox text value get

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yogarajan
    New Member
    • Apr 2007
    • 115

    button click evert error and multiple lablebox text value get

    Hi Friend

    this is my code
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                TableRow tr = new TableRow();
                
                TableCell td1 = new TableCell();
                Label lab1 = new Label();
                lab1.ID = "count" + i;
                lab1.Text = i.ToString();
                td1.Controls.Add(lab1);
    
                TableCell td2 = new TableCell();
                Button but1 = new Button();
                but1.Text = "increase";
                but1.Click +=new EventHandler(increasebuttonclick(i));
                td1.Controls.Add(but1);
    
                TableCell td3 = new TableCell();
                Button but2 = new Button();
                but2.Text = "Decrease";
                but2.Click += new EventHandler(decreasebuttonclick(i));
                td3.Controls.Add(but2);
    
                tr.Cells.Add(td1);
                tr.Cells.Add(td2);
                tr.Cells.Add(td3);
    
                Table1.Rows.Add(tr);
    
            }
        }
    
        private object decreasebuttonclick(int i)
        {
            //throw new Exception("The method or operation is not implemented.");
            "count" + i.Text = Convert.ToString("count" + i.Text - 1);
        }
    
        private object increasebuttonclick(int i)
        {
            //throw new Exception("The method or operation is not implemented.");
            "count" + i.Text = Convert.ToString("count" + i.Text + 1);
        }

    here i want

    1. button click event with my parameter (ex. increase buttonclick(str ing i)
    2. i want increase my label box value

    Pls guide me
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Huh? You didn't write the Page_Load portion did you? Anyone who could have written
    Code:
    Label lab1 = new Label();
     lab1.ID = "count" + i;
    lab1.Text = i.ToString();
    Would not have written
    Code:
    private object increasebuttonclick(int i)
        {
            //throw new Exception("The method or operation is not implemented.");
            "count" + i.Text = Convert.ToString("count" + i.Text + 1);
        }
    It's good to take someone else's code and tear it apart in an effort to understand how it works. It's a great way to learn more advanced techniques.

    Let's take it a piece at a time.

    The left side of an equal sign has to be an object, like a variable.
    Label Lab1 = for example. Lab1 is a new instance of a Label object.
    "count" + i.Text = This is not a variable or object so you can't set it equal to anything.

    i.Text + 1 You can't perform math on a string. You might as well be trying to do this "Chair" + 5
    also, 'i' is an int. int's don't have a .Text property. Notice when you are typing this in Visual Studio that as soon as you get to the period ( i. ) Visual Studio gives you a drop down menu of things you can pick from. This is a feature called Intellisense. The dropdown is all the properties within i. There are only 6 items in this menu and .Text is not one of them. So you are being told that there is no .Text property to an int.


    Declaring all your controls (tables, labels and so on) inside the Page_Load method means you have no way to reference them outside the Page_Load. This is why when you create a form in designer all of that takes place in the Form1.Designer. cs file. The controls are created with form wide scope, if that makes sense. If you make them in Page_Load then the references such as "Lab1" exist only within that method. The controls exist, but its a serious pain to try to reference them for things like.... Lab1.Text = "Bob";

    I suggest you start with a simpler project. Just make a form in designer with a label and 2 two buttons: One for add, one for subtract. And work on manipulating the label text from there.

    Then, once you have worked that out... try adding a new control programmaticall y (outside of any method) so it can be seen through-out the form, and try manipulating that.

    A little something to get you started
    Code:
            int newvalue = i++; //Increase the value of i;
            string status = "Count: " + newvalue.ToString();

    Comment

    Working...