Robert Martin
Robert Martin

Reputation: 1

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

Upvotes: 0

Views: 2230

Answers (2)

Numan
Numan

Reputation: 3948

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

Upvotes: 0

Jeremy Thompson
Jeremy Thompson

Reputation: 65702

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);
}

Upvotes: 1

Related Questions