使用字符串变量内容作为标签ID来更新标签.文本,出现错误-' string'不包含' Text'的定义 [英] Use string variable content as label ID to update label.Text, I get error - 'string' does not contain a definition for 'Text'

查看:59
本文介绍了使用字符串变量内容作为标签ID来更新标签.文本,出现错误-' string'不包含' Text'的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#新手,请保持温柔!这是使用按钮的参数来匹配标签ID的字符串创建代码的代码,因此我可以更新标签文本.

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

直接用于标签ID名称为"serverx_spooler_Status"时,此方法有效...

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

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

即使"strLabelID"的值为"serverx_spooler_Status",此操作也会失败...

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

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

谢谢德里克(Derek)的搜索方向!解决的办法是这样...

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!";

推荐答案

        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";
                }
            }
        }

这篇关于使用字符串变量内容作为标签ID来更新标签.文本,出现错误-' string'不包含' Text'的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆