Showing posts with label loads. Show all posts
Showing posts with label loads. Show all posts

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.

Saturday, March 24, 2012

UpdatePanel and __doPostBack

My app loads one of several dynamically generated reports, implemented as user Controls, (*.ascx files) into an Update Panel. I am trying to do this without a page postback. However, client side code needs to validate several things before issuing the asynchronous request. In IE, I was using an ASP.Net custom Validator control hooked to a client side script
(GenerateReport(sender, e) {...}
to control this, (usinge.valid = true/false; to control whether the asynchronous request to update the content in the UpdatePanel was sent or not, but I have discovered that Firefox does not implement ASP.Net Validators properly.

So now I am trying to accomplish the same functionality simply using a client side script that calls __doPostBack(eventTarget, eventArgs);
But I can't seem to get the __doPostBack to call the Microsoft AJAX asynhcronous call, instead of doing a complete page postback.

How do I do that? Or is there a better way to do this.

Another option I have considered is to put the user control into a web service, that somehow just returns the innerHTML of the user5 control rendering process, but I can;t seem to figure that out either..

ideas anyone?

I've been fighting the same problem the last couple of days. Here is what I just finally came up with...

I write the __doPostBack on the server side and insert it into an onClick event on a control.

String sOnClick

="__doPostBack('" +this.UpdatePanel1.UniqueID +"','eventarg1$eventarg2');"; // the Update Panel's UniqueID will call the asynchronous request and pass the event argument string

e.Row.Attributes.Add("onClick",sOnClick); // I'm adding this to a gridview, I'm guessing this will work elsewhere too.

Then in the Page_Load I parse the event arguments and call the appropriate function

StringsEventArguments =this.Request.Params["__EVENTARGUMENT"];if(sEventArguments !=null)

{

Int32iDelimiter =sEventArguments.IndexOf('$');

StringsArgument =sEventArguments.Substring(iDelimiter + 1);

if (sEventArguments.StartsWith("eventarg1")) EventFunction(sArgument);

}

I hope this helps.

G


You canuse __doPostBack() to refresh an UpdatePanel, if you target the UpdatePanel itself.