Monday, March 26, 2012

UPDATEPANEL (FindControl?)

I have an update panel with a place holder in which I load a user control when the page loads.

The user control has a button. What I would like is for that button to load another control in the update panel.

I thought I could utilize FindControl, but so far I am having no luck.

It is so easy to load controls into an update panel when the button is not contained in that update panel. But what if I want to use the update panel as if it were a frame?

Any ideas?

I tested to load user controls dynamically through asp:UpdatePanel.If you load user controls dynamically in Page_Load event handler,the loaded user controls can keep in a web all the time.Otherwise they will fade when there are any events to?be triggered.
Here are my testing codes for you.
User control 1:
<%@. Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="Web_User_Controls_WebUserControl1" %>
<table>
<tr>
<td><asp:Label ID="lblUserName" runat="server" Text="User Name"></asp:Label></td>
<td><asp:TextBox ID="tbUserName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label></td>
<td><asp:TextBox ID="tbPassword" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="btnLogon" runat="server" Text="Logon" /></td>
<td><asp:Button ID="btnCancel" runat="server" Text="Cancel" /></td>
</tr>
</table
Web User Control 2 to Load Web User Control 1:
<%@. Control Language="C#" AutoEventWireup="true" CodeFile="WUCLoadControls0.ascx.cs" Inherits="Web_User_Controls_WUCLoadControls0" %>
<asp:Label ID="lblLoadUserControl" runat="server" Text="Load User Controls Dynamically" BackColor="#C0FFC0"></asp:Label>
<asp:PlaceHolder ID="PH" runat="server"></asp:PlaceHolder>
<asp:Button ID="btnLoadControls" runat="server" Text="Load Another User Control" OnClick="btnLoadControls_Click" />

protected void btnLoadControls_Click(object sender, EventArgs e)
{
PH.Controls.Add(LoadControl("~\\Web User Controls\\WebUserControl1.ascx"));
}

.ASMX HTML:
<div>
<asp:UpdatePanel ID="upnlWaterMarkExtender" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="phLoadUserControl" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Page_Load Event Handler:
PlaceHolder phLoadUserControl = upnlWaterMarkExtender.FindControl("phLoadUserControl") as PlaceHolder;
phLoadUserControl.Controls.Add(LoadControl("~\\Web User Controls\\WUCLoadControls0.ascx"));

As a result,I find that?FindControl in asp:UpdatePanel can work fine.
Wish this can help you.

Well, how about this.

ON default.aspx, I have an updatepanel. In the updatepanel is a user control:

<asp:UpdatePanel ID="upMsg" runat="server" UpdateMode="Always">
<ContentTemplate>
<quarem:messages ID="msgcenter" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>

Now, the user control simply contains a placeholder in which is loaded 1 of 2 user controls based on a session variable:

<asp:PlaceHolder ID="messagecenterplaceholder" runat="server" />

[code behind]
Page_Init
Select Case session("message")
Case "write"
(load write)
Case Else
(load read)
End Select

So, so far this is good. Everything works. The problem is when I actually load the read part.
There is a button that changes the session and then should update...

What I am trying is:

dim up as UpdatePanel = ctype(findcontrol("upMsg"), updatepanel)
up.update

But this does not work. It gives me the old "object reference not set to an instance of an object" error... so obviously FindControl is not finding the control.

Any suggestions?


I have the same problem. I can't find any dynamically added control to a placeholder (inside an UpdatePanel) with FindControl method.

No comments:

Post a Comment