(Bassel Tabbara [MSFT])
1/3/2003 7:12:00 PM
Hello Joseph,
This problem might be caused by the user control having the same id. After
a postback, controls are not added in the same order after the postback,
they can get ID'd differently.
To avoid this issue, you need to explicitly set the ID property of the user
controls. For example, in the declaration below you can add the following
statements:
myUserControls1.id= "id1"
myUSerControls2.id = "id2"
Thanks,
Bassel Tabbara
Microsoft, ASP.NET
This posting is provided "AS IS", with no warranties, and confers no rights.
--------------------
| From: "Joseph" <joseph@bluefield.com.hk>
| Subject: System.InvalidCastException error when loading dynamic user web
control
| Date: Fri, 3 Jan 2003 19:05:46 +0800
| Lines: 58
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2720.3000
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
| Message-ID: <e14qKgxsCHA.1624@TK2MSFTNGP10>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: ntserver.bluefield.com.hk 203.85.151.129
| Path: cpmsftngxa06!TK2MSFTNGP08!TK2MSFTNGP10
| Xref: cpmsftngxa06
microsoft.public.dotnet.framework.aspnet.webcontrols:8183
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| On my aspx page I have a radio button with 2 selections. Clicking each
| selection will dynamically add a user control to a panel control on the
same
| page using the following lines of code:
|
| myUserControl myUserControls1; // my user control 1 to be created
| dynamically
| myUserControl myUserControls2; // my user control 2 to be created
| dynamically
|
| private void Page_Load(object sender, System.EventArgs e)
| {
| if (ListDisplay.SelectedItem.Value == "selection1")
| {
| plhControl1.Controls.Add(myUserControl1);
| }
| else if (ListDisplay.SelectedItem.Value == "selection2")
| {
| plhControl1.Controls.Add(myUserControl2);
| }
| }
| myUserControl1 was loaded successfully since it is loaded when the page
was
| first loaded. I got the following error when I click on the second
selection
| in the radio button to load the myUserControl2 using the Add() method
above:
|
| Stack Trace:
|
| [InvalidCastException: Specified cast is not valid.]
| System.Web.UI.StateBag.LoadViewState(Object state)
| System.Web.UI.Control.LoadViewState(Object savedState)
| System.Web.UI.HtmlControls.HtmlContainerControl.LoadViewState(Object
| savedState)
| System.Web.UI.Control.LoadViewStateRecursive(Object savedState)
| System.Web.UI.Control.LoadViewStateRecursive(Object savedState)
| System.Web.UI.Control.LoadViewStateRecursive(Object savedState)
| System.Web.UI.Control.AddedControl(Control control, Int32 index)
| System.Web.UI.ControlCollection.Add(Control child)
| MyProject.MyForm.Page_Load(Object sender, EventArgs e) in
| c:\inetpub\wwwroot\MyProject\MyForm.aspx.cs:90
| System.Web.UI.Control.OnLoad(EventArgs e)
| System.Web.UI.Control.LoadRecursive()
| System.Web.UI.Page.ProcessRequestMain()
|
|
| Does it matter if same name are used to name the controls (e.g. textbox,
| checkbox) contained in both user controls? I used distinct name to name
the
| controls contained in both user controls and set the enableViewState
| property to false but still get this error?
|
| Anybody got a hint here? This error happened inconsistently. It was
| appearing initially and then disappeared for quite some long time and
after
| I put a HTML div control on myUserControl1, the error occurred again.
|
| Thanks, Joseph
|
|
|
|
|
|