How to dynamically set a control using variables C#
How do you dynamically call a control and set i开发者_运维技巧t 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);
}
try FindControl("controlID")
and then cast the result of this call to the required control type and set the needed property.
(SomeParentControl.FindControl("IDOfControlToFind") AS LinkButton).PostBackUrl = "~/someresource.aspx";
精彩评论