REFERENCING OTHER CONTROLS FROM ANOTHER METHOD...??

Last post 05-11-2008 5:34 AM by BasharKokash. 2 replies.

Sort Posts:

  • REFERENCING OTHER CONTROLS FROM ANOTHER METHOD...??

    05-08-2008, 2:54 PM
    • Loading...
    • codecorey
    • Joined on 03-22-2008, 10:27 PM
    • Posts 21

     Hello,

     in my app I have a method that creates a table (code below), and in this very same method you'll notice that I dynamically create a Label control, a few of them actually.  How can i reference these labels from/in another method?  For example ....I want lblTally1.Text = "text" ...but from another method other than it's origin method....     ??? 

     

     

     public static void TallyTable()
    {

    TableRow rowTitle = new TableRow();
    TableRow row1 = new TableRow();
    TableRow row2 = new TableRow();
    TableRow row3 = new TableRow();
    TableRow row4 = new TableRow();
    TableRow row5 = new TableRow();

    // row and cell schema TableCell cellTitle1 = new TableCell(); TableCell cellTitle2 = new TableCell();
    TableCell cell1 = new TableCell(); TableCell cell2 = new TableCell();
    TableCell cell3 = new TableCell(); TableCell cell4 = new TableCell();
    TableCell cell5 = new TableCell(); TableCell cell6 = new TableCell();
    TableCell cell7 = new TableCell(); TableCell cell8 = new TableCell();
    TableCell cell9 = new TableCell(); TableCell cell10 = new TableCell();

    cellTitle1.Text = "Radio Scores";
    cellTitle1.Font.Size = 10;
    cellTitle1.Font.Name = "Verdana";
    cellTitle1.BackColor = Color.LightGray;

    cellTitle2.Text = "Tally";
    cellTitle2.Font.Size = 10;
    cellTitle2.Font.Name = "Verdana";
    cellTitle2.BackColor = Color.LightGray;

    rowTitle.Cells.Add(cellTitle1); rowTitle.Cells.Add(cellTitle2);
    row1.Cells.Add(cell1); row1.Cells.Add(cell2);
    row2.Cells.Add(cell3); row2.Cells.Add(cell4);
    row3.Cells.Add(cell5); row3.Cells.Add(cell6);
    row4.Cells.Add(cell7); row4.Cells.Add(cell8);
    row5.Cells.Add(cell9); row5.Cells.Add(cell10);

    Table tblTally = new Table();
    tblTally.Rows.Add(rowTitle);
    tblTally.Rows.Add(row1);
    tblTally.Rows.Add(row2);
    tblTally.Rows.Add(row3);
    tblTally.Rows.Add(row4);
    tblTally.Rows.Add(row5);

    #region Generice labels on left column of table...nothing important Label lbl1 = new Label();
    lbl1.Text = "1 out of 5";
    lbl1.Font.Name = "verdana";
    lbl1.Font.Size = 10;
    cell1.Controls.Add(lbl1);

    Label lbl2 = new Label();
    lbl2.Text = "2 out of 5";
    lbl2.Font.Name = "verdana";
    lbl2.Font.Size = 10;
    cell3.Controls.Add(lbl2);

    Label lbl3 = new Label();
    lbl3.Text = "3 out of 5";
    lbl3.Font.Name = "verdana";
    lbl3.Font.Size = 10;
    cell5.Controls.Add(lbl3);

    Label lbl4 = new Label();
    lbl4.Text = "4 out of 5";
    lbl4.Font.Name = "verdana";
    lbl4.Font.Size = 10;
    cell7.Controls.Add(lbl4);

    Label lbl5 = new Label();
    lbl5.Text = "5 out of 5";
    lbl5.Font.Name = "verdana";
    lbl5.Font.Size = 10;
    cell9.Controls.Add(lbl5);
    #endregion // Where the tally for each 1-5 scores will appear...
    // ..then assigned to the corret cell in the table.
    Label lblTally1 = new Label();
    lblTally1.Font.Name = "verdana";
    lblTally1.Font.Size = 10;
    lblTally1.BackColor = Color.Aqua;
    cell2.Controls.Add(lblTally1);

    Label lblTally2 = new Label();
    lblTally2.Font.Name = "verdana";
    lblTally2.Font.Size = 10;
    lblTally2.BackColor = Color.Aqua;
    cell4.Controls.Add(lblTally1);

    Label lblTally3 = new Label();
    lblTally3.Font.Name = "verdana";
    lblTally3.Font.Size = 10;
    lblTally3.BackColor = Color.Aqua;
    cell4.Controls.Add(lblTally1);

    Label lblTally4 = new Label();
    lblTally4.Font.Name = "verdana";
    lblTally4.Font.Size = 10;
    lblTally4.BackColor = Color.Aqua;
    cell6.Controls.Add(lblTally1);

    Label lblTally5 = new Label();
    lblTally5.Font.Name = "verdana";
    lblTally5.Font.Size = 10;
    lblTally5.BackColor = Color.Aqua;
    cell8.Controls.Add(lblTally1);


    }
     
     
  • Re: REFERENCING OTHER CONTROLS FROM ANOTHER METHOD...??

    05-08-2008, 4:57 PM
    • Loading...
    • che3358
    • Joined on 09-25-2003, 10:23 AM
    • Cleveland, OH
    • Posts 700

    Hopefully I understand your question correctly. You maybe look for something like

    private System.Web.UI.WebControls.Label lblControl;public System.Web.UI.WebControls.Label LblControl

    {

      get { return lblControl; }

      set { lblControl = value; }

    }

    put this code on the top of your class, then in your currect method, set the lable propertities to LblControl. In your another method, you just put TallyTable()
    , you will get your lable.

  • Re: REFERENCING OTHER CONTROLS FROM ANOTHER METHOD...??

    05-11-2008, 5:34 AM
    Answer

    Hi,

    I think yes you can do that but just you have to do some modifications as follows:

    After you've built your table you should add it to the page, and That's why your method should not be static in order to access the page's properties, you can add your table as follows:

    this.form.controls.add(tblTally);

    and to access the label in that table the table you have to use the findControl() method, this involves that the table should be assigned a name, and also the label when building the table, like this:

    tblTally.ID = "tbl";
    lblTally1.ID ="lbl1";

    Now to access them form another method:

    protected void Method2()
    {

    Table tbl = ((Table)Form.FindControl("tbl"));// this will find the table
    Label lbl = ((Label)tbl.FindControl("lblTally1"));//this will find the label inside the form

    }

    Bashar Kokash, Blog
Page 1 of 1 (3 items)