[英]How to dynamically set a control using variables C#
如何在運行時動態調用控件並設置其屬性?
// Declare and set queue servers
string[] queueservers = new string[] { "SERVER1", "SERVER2", "SERVER3", "SERVER4" };
int y;
for (y = 0; y <= queueservers.Length - 1; y++)
{
string queueanswer = GetMailQueueSize(queueservers[y]);
if (queueanswer == "alarm")
{
phxQueueImg + queueservers + .ImageUrl = "~/images/Small-Down.gif";
}
else
{
phxQueueImg + queueservers + .ImageUrl = "~/images/Small-Up.gif";
}
queueanswer = "";
}
看到這里問好問題。
我假設您粘貼了錯誤的代碼,因為它似乎與afaik問題無關。 另外,如果這是Winform,WPF或Web,還可以編輯您的問題和標簽?
在這里,我在運行時動態創建控件:
Textbox c = new Textbox();
設置其文字,例如
string s = "Please paste code that relates to your question";
c.Text = s;
或者在這里,我使用變量動態設置文本框控件屬性:
propertyInfo = c.GetType().GetProperty(property);
if (propertyInfo != null)
{
propertyInfo.SetValue(c, value, null);
}
嘗試使用FindControl("controlID")
,然后將此調用的結果FindControl("controlID")
轉換為所需的控件類型並設置所需的屬性。
(SomeParentControl.FindControl("IDOfControlToFind") AS LinkButton).PostBackUrl = "~/someresource.aspx";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.