locked
.NET C# use a string variable to reference the control name RRS feed

  • Question

  • User-596225964 posted

    Hello friends

    I have several labels, the names of which are numbered, e.g. lblTest1, lblTest2, lblTest3, lblTest4 & lblTest5. I would like to be able to reference each one using a for loop and concatenating the string filename with the label number. E.g. something like:

    Label lblMyLabel = new Label();

    string strLabelName;

    for (int i = 1, i = 5, i++)

    {

         strLabelName = lblTest + i;

         lblMyLabel = this.Controls.Find(strLabelName, True);

         lblMyLabel.Text = "Test Text " + i;

    }

    Is this possible? Otherwise I will have to write out the statements individually, e.g. lblTest1.Text = "Test Text 1", lblTest2.Text = "Test Text 2" etc. which will be very laborious and untidy as I have a lot of controls I need to apply this to.

    Thank you for your help

    Kind regards

    Jubbs

    Wednesday, February 5, 2020 3:50 AM

Answers

  • User409696431 posted

    Label lblMyLabel = new Label();

    defines a new label, which it does not sound like you want to do.  If you want to find each of your existing labels with the naming sequence you describe, use your for loop with following corrections:

    for (int i = 1; i < 6; i++)
    {
         string strLabelName = "lblTest" + i;
         Label lblMyLabel = (Label)FindControl(strLabelName);
         lblMyLabel.Text = "Test Text " + i;
    }

    In C# "for", the initilizer, condition, and increment are separated by ;  not by a comma.

    The "for" condition must be a boolean that is true for all the for loops that get executed. Using i < 6 means i from the initialized 1 through i == 5 would all be less than 6 so all those loops would run and increment to the next one.  You had i = 5, which is an assignment operator, not a boolean test, and the corresponding boolean test, i == 5, would not be true for any i other than 5, so not true for i = 1, the loop would never run, the increment would never occur (so 5 is never reached), and the for loop would do nothing.

    If you are looking for Label controls, and you know their names, use FindControl.

    You may notice I'm creating strings by concatenating a string and an int, as you did in your post.  That works - no need for i.ToString().  The string concatination internally calls ToString on all the objects being concatenated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 6, 2020 8:17 AM

All replies

  • User475983607 posted

    The common solution, in ASP.NET Web Forms, is using a data bound control to render a collection of server controls.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.repeater?view=netframework-4.8

    Wednesday, February 5, 2020 11:36 AM
  • User288213138 posted

    Hi Jubbs,

    Is this possible? Otherwise I will have to write out the statements individually, e.g. lblTest1.Text = "Test Text 1", lblTest2.Text = "Test Text 2" etc. which will be very laborious and untidy as I have a lot of controls I need to apply this to.

    You can put the Label in panel control, then traverse the panel control, assigning the label in turn.

    The code for you as a reference:

    <asp:Panel ID="Panel1" runat="server">
                    <asp:Label ID="Label1" runat="server" Text="Label1"></asp:Label><br />
                    <asp:Label ID="Label2" runat="server" Text="Label2"></asp:Label><br />
                    <asp:Label ID="Label3" runat="server" Text="Label3"></asp:Label><br />
                    <asp:Label ID="Label4" runat="server" Text="Label4"></asp:Label><br />
                    <asp:Label ID="Label5" runat="server" Text="Label5"></asp:Label>
                </asp:Panel>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
     protected void Button1_Click(object sender, EventArgs e)
            {
                int i = 0;
                foreach (Control control in this.Panel1.Controls)
                {
                    if (control is Label)
                    {
                        i++;
                        (control as Label).Text = "Test Text"+i.ToString();
                    }
                }
            }

    Best regards,

    Sam

    Thursday, February 6, 2020 3:54 AM
  • User409696431 posted

    Label lblMyLabel = new Label();

    defines a new label, which it does not sound like you want to do.  If you want to find each of your existing labels with the naming sequence you describe, use your for loop with following corrections:

    for (int i = 1; i < 6; i++)
    {
         string strLabelName = "lblTest" + i;
         Label lblMyLabel = (Label)FindControl(strLabelName);
         lblMyLabel.Text = "Test Text " + i;
    }

    In C# "for", the initilizer, condition, and increment are separated by ;  not by a comma.

    The "for" condition must be a boolean that is true for all the for loops that get executed. Using i < 6 means i from the initialized 1 through i == 5 would all be less than 6 so all those loops would run and increment to the next one.  You had i = 5, which is an assignment operator, not a boolean test, and the corresponding boolean test, i == 5, would not be true for any i other than 5, so not true for i = 1, the loop would never run, the increment would never occur (so 5 is never reached), and the for loop would do nothing.

    If you are looking for Label controls, and you know their names, use FindControl.

    You may notice I'm creating strings by concatenating a string and an int, as you did in your post.  That works - no need for i.ToString().  The string concatination internally calls ToString on all the objects being concatenated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 6, 2020 8:17 AM
  • User-596225964 posted

    Hi Kathy.

    Thank you for the reply. This is exactly the solution I am looking for. Unfortunately I am getting the message that FindControl doesn't exist in the current context. Does it matter that my project is Winforms NOT ASP? I realise now that I posted in the wrong forum.

    Thank you for your help.

    Jubbs

    Thursday, February 6, 2020 12:19 PM
  • User753101303 posted

    Hi,

    Yes, try rather https://social.msdn.microsoft.com/Forums/en-US/home or https://stackoverflow.com/questions to find a forum for Windows desktop apps. This one is for web based applications which are working quite differently.

    Thursday, February 6, 2020 12:38 PM
  • User-596225964 posted

    Hi Kathy, your solution works for WinForms if I replace your FindControl line with:

    Label lblMyLabel = this.Controls.Find(strLabelName, true).FirstOrDefault() as Label;

    Thanks for your help

    Thursday, February 6, 2020 12:42 PM
  • User409696431 posted

    You did not post this in the Winforms forum (https://social.msdn.microsoft.com/Forums/en-US/home?forum=winforms) or specify that you were talking about a windows form.  I assumed you were using Web Forms.

    Glad you got it working.

    Thursday, February 6, 2020 7:25 PM