1

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

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”时有效...

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

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

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

谢谢德里克的搜索方向!解决方案是这样...

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

4 回答 4

1
        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";
                }
            }
        }
于 2012-04-13T08:48:16.487 回答
0

我认为这可能会有所帮助。

您需要做的是遍历项目中的所有标签,直到找到这样的匹配项:-

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

foreach(在 this.Controls 中控制 ctr){

if (ctr is Label) { if (ctr.Name == strLabelID) { //在这里做任何事情 } } }

于 2012-04-13T08:35:28.037 回答
0

的类型serverx_spooler_Status可能是Label具有Text字段的(未在问题中显示),因此serverx_spooler_Status.Text是有效的。

的类型strLabelIDstring(第一个包含),它没有Text字段,所以访问strLabelID.Text无效

尝试:

strLabelID = String.Format(strServiceName);

这会将 的值更改strLabelIDstrServiceName(基本上与:strLabelID = strServiceName;)的值相同

如果您真的想更新标签,您将需要一个类型为 的对象Label,您可以在其中访问该Text字段并更新该字段(只是说谎您正在使用serverx_spooler_Status)。如果您有任何其他可以使用的标签对象,则不会显示您的代码包含。

于 2012-04-12T15:12:59.773 回答
0

我认为这就是您要寻找的东西:-

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

那应该行得通。

或者你可以说:-

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

label1.Text = strLabelID;

不太清楚你的意思。希望这可以帮助。

于 2012-04-12T15:39:20.780 回答