RGX
RGX

Reputation: 13

Use string variable content as label ID to update label.Text, I get error - 'string' does not contain a definition for 'Text'

C# newbie so be gentle! Here is the code creating a string using the arguments from a button to match a label id so I can update the labels text.

string[] commandArgs = e.CommandArgument.ToString().Split(new char[] {','});                    //Convert the buttons arguments to server/service variables
    string strServerName = commandArgs[0];
    string strServiceName = commandArgs[1];     
    string strLabelID = String.Format(strServerName + "_" + strServiceName + "_" + "Status");   //assign the strLabelID to the format: "servername_servicename_Status" for updating the label text

This works when used directly as the Label ID name is "serverx_spooler_Status"...

serverx_spooler_Status.Text = String.Format(strServiceName);    //update label text

This fails even though the value of "strLabelID" is "serverx_spooler_Status"...

strLabelID.Text = String.Format(strServiceName);    //update label text

Thank you Derek for the direction to search into! The solution was this...

 // Find control on page.
    Control myControl1 = FindControl(strLabelID);
    Label myLabel1 = (Label)myControl1;
    myLabel1.Text = "Updated Label Text!";

Upvotes: 1

Views: 15141

Answers (4)

Derek
Derek

Reputation: 8628

        string service = "winmgmt";
        string server = "DFS5600";
        string labelText = string.Format("{0}_{1)_Status", server, service);

        foreach (Control ctr in this.Controls)
        {
            if (ctr is Label)
            {
                if (ctr.Name == labelText)
                {
                    ctr.Text = "Hello Label";
                }
            }
        }

Upvotes: 1

Derek
Derek

Reputation: 8628

I think this may help.

What you will need to do is loop through all labels in your project until you find a match like this :-

string strLabelID = String.Format("{0}_{1}_Status",strServerName,strServiceName);

foreach ( Control ctr in this.Controls) {

if (ctr is Label) { if (ctr.Name == strLabelID) { //Do what ever in here } } }

Upvotes: 0

Derek
Derek

Reputation: 8628

I think this is what you are looking for :-

Label.Text = String.Format("{0}_{1}_Status",strServerName,strServiceName);

That should work.

Or you could say :-

string strLabelID = String.Format("{0}_{1}_Status",strServerName,strServiceName);

label1.Text = strLabelID;

Not quite sure what you mean. Hope this helps.

Upvotes: 0

Attila
Attila

Reputation: 28772

The type of serverx_spooler_Status is possibly a Label (not shown in question) that has a Text field, so serverx_spooler_Status.Text is valid.

The type of strLabelID is string (first inclusion), which does not have a Text field, so access to strLabelID.Text is invalid

try:

strLabelID = String.Format(strServiceName);

This will change the value of strLabelID to that of strServiceName (essentially same as: strLabelID = strServiceName;)

If you actually want to update a label, you will need an object of type Label, where you can access the Text field and update that (just lie you are doing with serverx_spooler_Status). Your code inclusions do not show if you have any other label objects you could use.

Upvotes: 0

Related Questions