Hi.

I have a custom control that inherits from TextBox and implements ICallbackEventHandler.
The control is kind of an extended ajax autocomplete/suggestions one.

Aside from the Text property of the Textbox I have extended the control with a Value property.
The idea is that a Hiddenfield control is assigned a value by javascript upon selected a autocomplete suggestion on the client side, which can then be read server side on postback.

I add the "extra" HiddenField by overriding the control's OnInit event

private HiddenField hf = new HiddenField();
protected override void OnInit(EventArgs e) {
 hf.ID = base.UniqueID + "_hf";
 this.Controls.Add(hf);
 base.OnInit(e);
}

The control also extends a Value property

public string Value {
 get { return hf.Value; }
 set { hf.Value = value; }
}

And in the control's Render method (aside from all the other stuff) I do

protected override void Render(System.Web.UI.HtmlTextWriter writer) {
... 
 base.Render(writer);
 hf.RenderControl(writer);
...
}

And then my problem:

On postbacks I am able to retrieve the control's Value property.
That is...for example, from a buttons click event I am able get the controls Value (assigned by javascript client side). No problem.

But when I try to do the exact same with on a webform with a master page the control's Value property is never populated on postback.
It's almost like the viewstate isn't restored or something.

Anywayz; I'm puzzled as to why the things works fine on a "normal" webform but fails when using a master page.
Why the difference.
Everything else works fine.

Any help or ideas would be greatly appreciated.

Recommended Answers

All 3 Replies

Hi, and thanks for your reply.

And no...I didn't read through the articles you mention.
This is my first go at a server control, so I probably should have.
I'm gonna start with the sections on state management.

Hrmm...

It seems overriding the SaveViewState method and manually retrieving the HiddenField's value does the trick.

protected override object SaveViewState() {
	this.hf.Value = ((HiddenField)base.Controls[0]).Value;
	object baseState = base.SaveViewState();
	return baseState;
}

Now I only need to understand why the above is necessary... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.