Wednesday, March 28, 2012

Accessing objects in an UpdatePanel

I'm trying to reference a textbox that is located in an updatepanel and was successfully doing so until I installed the latest version of the CTP (Sept 2006) and now I don't have programmatic access to the objects in the update panel...how can I get to this? Below is my same code that worked in the last version:

<divid="footer">
<atlas:UpdatePanelID="udpStatusBar"runat="server"Mode="Always">
<ContentTemplate>
<asp:TextBoxID="tbxStatusMessage"runat="server"Enabled="False"SkinID="StatusBar"Width="455px"></asp:TextBox>
<asp:TextBoxID="tbxStats"runat="server"Enabled="False"SkinID="StatusBar"></asp:TextBox>
<asp:TextBoxID="tbxDate"runat="server"Enabled="False"SkinID="StatusBar"></asp:TextBox>
</ContentTemplate>
</atlas:UpdatePanel>
</div>

ASPX.cs

privatevoid DisplayMessage(string s)
{
((TextBox)this.Parent.Page.Master.Controls[3].FindControl("tbxStatusMessage")).Text = s;
}

myost2921:

I'm trying to reference a textbox that is located in an updatepanel and was successfully doing so until I installed the latest version of the CTP (Sept 2006) and now I don't have programmatic access to the objects in the update panel...how can I get to this? Below is my same code that worked in the last version:

<divid="footer">
<atlas:UpdatePanelID="udpStatusBar"runat="server"Mode="Always">
<ContentTemplate>
<asp:TextBoxID="tbxStatusMessage"runat="server"Enabled="False"SkinID="StatusBar"Width="455px"></asp:TextBox>
<asp:TextBoxID="tbxStats"runat="server"Enabled="False"SkinID="StatusBar"></asp:TextBox>
<asp:TextBoxID="tbxDate"runat="server"Enabled="False"SkinID="StatusBar"></asp:TextBox>
</ContentTemplate>
</atlas:UpdatePanel>
</div>

ASPX.cs

privatevoid DisplayMessage(string s)
{
((TextBox)this.Parent.Page.Master.Controls[3].FindControl("tbxStatusMessage")).Text = s;
}

You should be able to access the control directly. This should work for your DisplayMessage function:

tbxStatusMessage.Text = s;

The problem is that my DisplayMessage is sitting in a UserControl that is loaded dynamically into the page. The UserControl knows nothing of the page until runtime.
The problem is that the DisplayMessage method is found in a UserControl which is being dynamically added to the page at runtime. I'm having to use the FindControl command because of that reason. Any more ideas?

What error are you getting? Have you tried just calling page.findcontrol()?

A quick example:

.aspx file:

<%@. Page Language="VB" AutoEventWireup="false" CodeFile="wizard_validation.aspx.vb" Inherits="wizard_validation" %><%@. Register Assembly="AtlasControlToolkit" Namespace="AtlasControlToolkit" TagPrefix="act" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <atlas:ScriptManager runat="server" ID="blah2" EnablePartialRendering="true"></atlas:ScriptManager> <atlas:UpdatePanel runat="server" ID="blah" Mode="conditional"> <ContentTemplate> <asp:Wizard ID="NewFormWizard" runat="server" Width="100%" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" CancelButtonText="Cancel" DisplayCancelButton="true" > <WizardSteps> <asp:TemplatedWizardStep ID="WizardStepGeneral" runat="server" Title="*General"> <ContentTemplate> <h1>Header!</h1> </ContentTemplate> </asp:TemplatedWizardStep> </WizardSteps> </asp:Wizard> </ContentTemplate> </atlas:UpdatePanel> </div> </form></body></html>

Code-behind (in VB, but I'm sure you can figure it out):

Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.LoadDim iAs Integer Dim tmpwizardAs Wizard tmpwizard = Page.FindControl("NewFormWizard")If tmpwizardIs Nothing Then' failure i = 0Else' success i = 1End If End Sub

It returns with a success on that sample page. However, this is not a dynamically added control, so if you could post an example if using "Page.FindControl" doesn't work, it'd be easier to help ya out.


I figured out the problem...turns out it was actually simplified even more in this release and the cause of the error was something completely different. Thanks Matt!

No comments:

Post a Comment