简体   繁体   中英

How to dynamically set a control using variables C#

How do you dynamically call a control and set it property at runtime?

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

See here about asking good questions .

I'm going to assume you pasted the wrong code since it doesn't seem to have anything to do with the question afaik. Plus could edit your question and tag if this is winform, wpf or web?

Here I dynamically create the control at runtime:

Textbox c = new Textbox();

Set its text, eg

string s = "Please paste code that relates to your question";
c.Text = s;

Or here I dynamically set my textbox controls property using variables:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM