I want to create a label that implements the IPostBackDataHandler, because I want to change the text with javascript. If I trigger a postback after that, than my text is gone.
The code that I already have is this:
public class CustomLabel : Label, IPostBackDataHandler
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Page != null)
Page.RegisterRequiresPostBack(this);
}
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
this.Text = postCollection[postDataKey];
return true;
}
public void RaisePostDataChangedEvent()
{
//throw new NotImplementedException();
}
}
It is not working at all, I don't get how I should see that the text is changed and PostCollection[postDataKey] is always null.
The
IPostBackDataHandler
interface is intended for inputs. Elements like spans and divs don't get stored in the request object. I would just implement the necessary ViewState management methods. Here's an example from a custom grid component that I developed: