简体   繁体   中英

Invoke a control to set a property

I have this exact issue

ASP.net can't update page from event handler

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM