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