Custom control viewstate lost in Datagrid

T

Thomas

I am trying to create a sort of toggle button custom control. What I
did is to inherit an ImageButton, add a Viewstate field to rememer its
own state. When the button is clicked it will toggle its image. It
works fine standalone, but once I put it into the header of a
datagrid, the toggle button cannot get its viewstate, which is always
empty. I get a feeling that I am missing something simple, but I
cannot find a solution after looked at many previous posts. I am
wondering if it is a MS bug or something. Thanks in advance.


public string mImageUrl
{
get
{
if (!Page.IsPostBack)
{
ViewState["_Text"] = "image/plus.gif";
}
else
{
ViewState["_Text"] =
((string)ViewState["_Text"]=="image/plus.gif")?"image/minus.gif":"image/plus.gif";
}
return (String)ViewState["_Text"];
}
set
{
ViewState["_Text"] = value;
}
}

protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
// Load State from the array of objects that was saved at ;
// SavedViewState.
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);
if (myState[1] != null)
mImageUrl = (string)myState[1];
}
}

protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object[] allStates = new object[2];
allStates[0] = baseState;
allStates[1] = mImageUrl;
//sUrl = (sUrl=="image/plus.gif")?"image/minus.gif":"image/plus.gif";
return allStates;
}
#endregion

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
output.WriteBeginTag("input");
output.WriteAttribute("type", "image");
string str = this.ImageUrl;
str = (str=="image/plus.gif")?"image/minus.gif":"image/plus.gif";
output.WriteAttribute("src", str);
this.ImageUrl = str;
output.WriteAttribute("Id", this.ClientID);
output.WriteAttribute("Name", this.UniqueID);
output.WriteAttribute("border", "0");
output.Write(HtmlTextWriter.SelfClosingTagEnd); // add the closing
'/>'
//output.WriteEndTag("input");
}
}
 
A

Abhijeet Dev

Check whether EnableViewState is set to true or not ...

I may be wrong here but as far as i know, if datagrid doesnt have its
viewstate enabled, all the child controls will loose viewstates.
(Please correct me, if i m wrong here)

Abhijeet dev
 
T

Thomas

Yes, you are right that the children control's viewstate will be
disabled. However I have viewstate of the datagrid enabled as default.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top