如何使用变量 C# 动态设置控件

发布于 2024-10-19 20:26:10 字数 517 浏览 1 评论 0原文

如何在运行时动态调用控件并设置其属性?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-10-26 20:26:10

请参阅此处了解如何提出好问题。

我假设您粘贴了错误的代码,因为它似乎与问题没有任何关系。另外,如果这是 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);
}

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);
}
穿透光 2024-10-26 20:26:10

尝试 FindControl("controlID") ,然后将此调用的结果转换为所需的控件类型并设置所需的属性。

<代码>
(SomeParentControl.FindControl("IDOfControlToFind") AS LinkBut​​ton).PostBackUrl = "~/someresource.aspx";

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文