Invoke a control to set a property
I have this exact issue
ASP.net can’t update page from event handler
开发者_StackOverflow中文版and it's been answered! My only problem is I don't really understand the solution. How does one Invoke the control when setting the property.
I have a label control but there doesn't seem to be an Invoke property/method on it.
I tried this...
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(Label1);
PropertyDescriptor myProperty = properties.Find("Text", false);
myProperty.SetValue(Label1, "my value");
but that seemed to be the same as
label1.text = "my value"
which didn't work
You need something like this:
delegate void UIDelegate(object component, object value);
if (this.save_button.InvokeRequired)
{
this.save_button.Invoke(new UIDelegate(TypeDescriptor.GetProperties(this.save_button).Find("Enabled", false).SetValue),
new object[] { this.save_button, true });
}
else
{
this.save_button.Enabled = true;
}
Normally you'd Invoke a control like this:
this.label1.Invoke(new MethodInvoker(delegate
{
this.label1.Test = "my value";
}));
Unfortunately there seems to be no Invoke method on a WebControls.Label.
One way around this is to write web method which returns a string in web service and set it to Label.Text, I found an example here.
精彩评论