Monday, March 26, 2012

UpdatePanel + Composite COntrol + ViewState

I have a composite control that relies on the ViewState for some properties. however I am getting very strange behaviors.

If I set the following in webform1.aspx:

MyControl.XMLFileName = "MyFile.xml";

and inside my composite control I have:

public string XMLFileName
{
get
{
return ViewState["XMLfileName"] == null? "" : ViewState["XMLfileName"].ToString();
}
set { ViewState["XMLfileName"] = value; }
}

Wherever I am in my application: ViewState["XMLfileName"] is null.

If I go to the composite control and break inside of it, XMLFileName is "" (Viewstate["XMLfileName"] null). However, when I go to Webform1.aspx, and I look at the XMLFileName property it's set to the correct string.

How can my webform see the property of the composite control (from outside), but when I'm inside my composite control, I can't get the property?

Here's the aspx:

<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" EnableViewState="true">
</asp:ScriptManager>

....
<asp:UpdatePanel ID="upTest" runat="server" UpdateMode="conditional" EnableViewState="true">
<ContentTemplate>
<cw:Form ID="frmSample" runat="server" EnableViewState="true" />
</ContentTemplate>
</asp:UpdatePanel>

Hey,

ViewState is loaded at particular times, and that may be the problem you are experiencing. When do you look at viewstate to check the property value?


What happens is:
on the ASPX.CS Page:
-Set a few properties
-Call Control.LoadForm


inside my COmposite controls LoadForm function:
public void LoadForm()
{
...This is where I access the properties.
}

=========
in the ASPX.CS page the properties are not null/"", they are the right values. Inside the LoadForm() method, they are empty (the viewstate["value"] is null.


I also tried to set hte VIewState properly in the ASPX.CS page.

However, whenever I do: this.ViewState, ViewState, ViewState[...] or access the ViewState in any way from my CompositeControl I get:

'(What I did)' threw an exception of type 'System.ArgumentNullException' System.Web.UI.StateBag {System.ArgumentNullException}

I'm not sure how to get around this.

I need to set values in my control that I will not know at design time, only at run time. And those values need to stick seeing the control posts back to itself many times.


Hey,

OK, you set a few properties, and LoadForm is your own method, that does what? And, right after you set a value, you still can't get the property value in debugging? If after post back, make sure your LoadForm data doesn't occur too early, before viewstate is loaded. And, when referencing ViewState, if the key isn't present in the viewstate, then an exception will raise; always do null checks with viewstate data.


All of this is happening off of a button click.

protected void btnLoadForm_Click(object sender, EventArgs e)
{
LoadData();
}

protected override void LoadData()
{
frmSample.FormType = FormManagement.Entities.FormTypes.ItemDetail;
frmSample.XMLFileName = ddlSampleForms.SelectedValue;
Trace.Write("**Start rendering Form Control");
frmSample.LoadForm(false);
Trace.Write("**End Rendering Form Control");
}

When does the viewstate get loaded? Before OnInit()? And is it OnInit() of the Page, or OnInit() of the control?



Hey,

After Init, so this should work... http://www.15seconds.com/issue/020102.htm

So does the code actually work, but you can't debug it?


I changed the order the way the data was loaded in the Form.

I was always trying to load the data on the OnInit() method of the composite control - and no viewstate is loaded then.

I got it working now. :D

No comments:

Post a Comment