0

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 = "";
}
2
  • 1
    Huh? I really don't understand what you're asking here. Commented Mar 2, 2011 at 3:16
  • I suspect you just want to get a control given then name of the control -- if this is the case, I would advise using a Dictionary<name,Control> or similar (although perhaps the general design is icky?) The Controls collection can be manually iterated -- but more ick. Commented Mar 2, 2011 at 5:41

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I know I'm a little late to the party but that last block just helped me out.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.