Showing posts with label containing. Show all posts
Showing posts with label containing. Show all posts

Wednesday, March 28, 2012

Extended controls may not be registered before Pre-Render

hi all,

i get the following error message when trying to load a usercontrol containing a modalpopupextender:
"extended controls may not be registered before pre-render"

no line specified, no more info about what's wrong, and almost everything is done with click 'n' drag in design mode (vwd 2005).

default.aspx and its codebehind:http://www.aspsidan.se/code/default.asp?c=9359

editcontent.ascx and its codebehind:http://www.aspsidan.se/code/default.asp?c=9361

the error message:http://www.aspsidan.se/code/default.asp?c=9360

editcontent.ascx is, as you can see, within a loginview, and it's not until i log on that i get the problem. i guess this error has something to do with the page life cycle, but i have no clue of how to get around it...

(i posted the code already in another forum, so i refer to those pages. i hope they're available even if you're not logged on. i've tried when i was offline and it worked, so it shouldn't be a problem...)

Hi lycken,

It is a common issue. You should add base.OnPreRender at the beginning ofvoid OnPreRender(EventArgs e){};

For more , pleaseread this. If it doesn't work, please feel free to let me know.

Best regards,

Jonathan


hi jonathan,

thanks a lot for the reply! it really does save my day that there is a solution to this.

however, neither your answer nor the article you linked me to helped me all the way. i understand i have to call the base.OnPreRender routine, but i can't figure out where in my code to put the call. i am coding in vb, so if you could explain it with vb examples instead of c# it would help me ;)

thanks again for your help!

-tomas


Hi Lycken,

Would you please give us a tiny and workable repro? So you can benefit from it directly. Thanks for your understanding.

Best regards,

Jonathan


hi again jonathan,

i provided all my source code in the first post - unless the links are not working. it's pasted below anyway - excuse the lenghtyness... and again - thanks for helping out!

the db is designed as follows:

Texts
******
TextID | int (identifier, primary key)
TextName | varchar(15)
Text | varchar(max)

Default.aspx

1<%@. Page Language="VB" MasterPageFile="~/site.master" AutoEventWireup="false" CodeFile="Default.aspx.vb"Inherits="_Default" title="Untitled Page" Theme="SMK" Trace="true" %>
2<%@. RegisterAssembly="AjaxControlToolkit"Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
3<%@. Register TagPrefix="list" TagName="Concerts" src="~/konsertprogram/Concerts.ascx" %>
4<%@. Register TagPrefix="ucl" TagName="EditContent" src="~/admin/EditContent.ascx" %>
5
6<asp:Content ID="Content1" ContentPlaceHolderID="Left" Runat="Server">
7 <asp:Image ID="LeftPaneImage" runat="server" ImageUrl="~/image/bredvid_konserter.jpg" />
8</asp:Content>
9<asp:Content ID="Content2" ContentPlaceHolderID="Content" Runat="Server">
10 <asp:Label ID="TextLabel" runat="server" />
11 <div style="text-align:right; width:100%;">
12 <asp:LoginView ID="LoginView1" runat="server">
13 <RoleGroups>
14 <asp:RoleGroup Roles="Admin">
15 <ContentTemplate>
16 <ucl:EditContent ID="EditContentUserControl" runat="server" TextToEdit="hem" />
17 </ContentTemplate>
18 </asp:RoleGroup>
19 </RoleGroups>
20 </asp:LoginView>
21 </div>
22
23 <div class="row">
24 <div style="float:left;">
25 <h1>N?sta konsert:</h1>
26 <list:Concerts ID="Concerts" runat="server" ItemsToShow="1" />
27 </div>
28
29 <div style="float:left;">
30 <h1>En till ruta</h1>
31 <p>med lite info</p>
32 </div>
33 </div>
34
35</asp:Content>

Default.aspx.vb

1Imports System.Configuration
2Imports System.Data.SqlClient
3Imports System.Data.SqlDbType
4Imports ErrorHandling
5
6PartialClass _Default
7Inherits System.Web.UI.Page
8
9Dim objConnAs New SqlConnection(ConfigurationManager.ConnectionStrings("SMK").ConnectionString)
10
11Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load
12Dim objCmdAs New SqlCommand("SELECT [Text] FROM [Texts] WHERE [TextName] ='hem'", objConn)
13
14Try
15 objConn.Open()
16 TextLabel.Text = StringFunctions.FormatText(objCmd.ExecuteScalar)
17Catch exAs Exception
18 Err.Log(ex)
19Finally
20 If objConn IsNotNothing Then objConn.Close()
21End Try
22 End Sub
23End Class

EditContent.ascx

1<%@. Control Language="VB" AutoEventWireup="false" CodeFile="EditContent.ascx.vb"Inherits="admin_EditContent" %>
2<%@. RegisterAssembly="AjaxControlToolkit"Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
3<asp:ScriptManager ID="ScriptManager1" runat="server">
4</asp:ScriptManager>
5<asp:LinkButton ID="EditText" runat="server" CssClass="textr">Redigera texten</asp:LinkButton>
6<asp:Panel ID="ModalPanel" runat="server" CssClass="modalpu">
7 <asp:Button ID="btnBold" runat="server" Font-Bold="True" Text="F" Width="30px" />
8 <asp:Button ID="btnItalic" runat="server" Font-Italic="True" Text="K" Width="30px" />
9 <asp:Button ID="btnUnderline" runat="server" Font-Underline="True" Text="U" Width="30px" />
10 <asp:Button ID="btnHeader" runat="server" Text="Rubrik" Width="80px" />
11 <asp:Button ID="btnText" runat="server" Text="Text" Width="80px" /><br />
12 <asp:TextBox ID="tbContent" runat="server" Height="400px" Width="430px" TextMode="MultiLine" /><br />
13 <asp:Button ID="OKButton" runat="server" Font-Bold="True" Text="Skicka" Width="90px" />
14 <asp:Button ID="CancelButton" runat="server" Text="Avbryt" Width="90px" /><br />
15 <asp:Label ID="lblStatus" runat="server" />
16</asp:Panel>
17<cc1:ModalPopupExtender ID="ModalPopupExtender" runat="server"
18 TargetControlID="EditText"
19 PopupControlID="ModalPanel"
20 x="0"
21 Y="0"
22 DropShadow="true"
23 RepositionMode="none"
24 OkControlID="OKButton"
25 OnOkScript="__doPostBack('ctl00$Content$LoginView1$EditContentUserControl$OKButton','')"
26 CancelControlID="CancelButton"
27 BackgroundCssClass="modalbg" />
EditContent.ascx.vb
1Imports System.Configuration
2Imports System.Data.SqlClient
3Imports System.Data.SqlDbType
4Imports ErrorHandling
5
6PartialClass admin_EditContent
7Inherits System.Web.UI.UserControl
8
9Dim objConnAs New SqlConnection(ConfigurationManager.ConnectionStrings("SMK").ConnectionString)
10Private _textToEditAs String
11
12 Public WriteOnly Property TextToEdit()As String
13 Set(ByVal valueAs String)
14 _textToEdit = value
15End Set
16 End Property
17
18 Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load
19Dim objCmdAs New SqlCommand("SELECT [Text] FROM [Texts] WHERE [TextName] = @.TextName", objConn)
20
21Dim objParamAs New SqlParameter("@.TextName", VarChar)
22 objParam.Value = _textToEdit
23 objCmd.Parameters.Add(objParam)
24
25Try
26 objConn.Open()
27 tbContent.Text = objCmd.ExecuteScalar
28Catch exAs Exception
29 Err.Log(ex)
30Finally
31 If objConn IsNotNothing Then objConn.Close()
32End Try
33 End Sub
34
35 Protected Sub OKButton_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles OKButton.Click
36Dim objCmdAs New SqlCommand("UPDATE Texts SET [Text] = @.Text WHERE [TextName] = @.TextName", objConn)
37
38Dim objParamAs New SqlParameter("@.Text", VarChar)
39 objParam.Value = tbContent.Text
40 objCmd.Parameters.Add(objParam)
41
42 objParam =New SqlParameter("@.TextName", VarChar)
43 objParam.Value = _textToEdit
44 objCmd.Parameters.Add(objParam)
45
46Try
47 objConn.Open()
48 objCmd.ExecuteNonQuery()
49 lblStatus.Text ="Uppdaterat!"
50Catch exAs Exception
51 Err.Log(ex)
52Finally
53 If objConn IsNotNothing Then objConn.Close()
54End Try
55
56 objConn =Nothing
57 objCmd =Nothing
58 End Sub
59End Class



Hi Luken,

Would you please add this code to EditContent.ascx.vb?

protected override void OnPreRender(EventArgs e)
{
// add base.OnPreRender(e); at the beginning of the method.
base.OnPreRender(e);

// codes to handle with your controls.
...
}

I have copied your code to my project and did some modifications(comment and rename work only) , it works well on my machine. Also where's your master page? If it doesn't work, would you please create aworkable sample and give us the links so that we can download your project. Thanks

Best regards,

Jonathan


i'd love to add it to editcontent.ascx.vb, but i don't know how to re-write it to vb. i get syntax errors on "base" when i try. what is it called with vb syntax?


Hi Lycken,

For example,

C# code

protected override void OnPreRender(EventArgs e)
{
// add base.OnPreRender(e); at the beginning of the method.
base.OnPreRender(e);

// codes to handle with your controls.
...
}

Convert to VB Code,

Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs) ' add base.OnPreRender(e); at the beginning of the method. MyBase.OnPreRender(e) ' codes to handle with your controls.

End Sub

Here is anuseful tool.

I hope this help.

Best regards,

Jonathan


thanks a lot! it's working fine now - or at least this part of the solution is... i have a couple of other things that aren't doing so well at the moment, but i'll work on those for yet a while before i post here.

again, thanks!

Saturday, March 24, 2012

Exception when using public attributes of user control containing UpdatePanel

I have a page that utilizes a user control. This control is compiled in using a web deployment project. The project is not set to allow the precompiled site to be updatable.

This user control has a number of attributes that set/access child control properties.

My problem is that within my user control, no child control within the UpdatePanel is instantiated when the page attributes are being processed. When I try to declare attributes for my user control within the .aspx, I get a null reference exception. The generated code that sets these attributes appears to run too early. If I set the attributes programmatically at Page_Init or Page_Load, everything works okay.

On error, the flow of execution is this:

1) Page's Page_Init

2) Exception (attempt to set attribute of null control)

(Not called: Control's Page_PreInit, Control's Page_Init)

I have tried calling EnsureChildControls() for both the page and the user control, but it does not make any difference.

It looks something like this:

ASPX file:

<asp:Content ID="thePage" ContentPlaceHolderID="MainContentPlaceHolder" Runat="Server"> <MyControls:theControl id="WebLinksControl" runat="server" CssClass="Content_FullPage" ListTitleText="my text" /></asp:Content>
ASCX file:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional" > <ContentTemplate> <div class="ListTitle"> <asp:Label ID="lblListTitle" runat="server" Text="Replace Me"> </asp:Label> </div></ContentTemplate></asp:UpdatePanel>

The accessor:

/// <summary> /// Get/set the text of the label: List title /// </summary>public string ListTitleText {get {// Retrieve object propertyreturn lblListTitle.Text; }set {// Override null with empty stringif (value ==null)value = String.Empty;// Set object property lblListTitle.Text =value; } }

I'm currently experiencing this problem myself. Has anyone been able to solve this?

Me too. Has anyone been able to resolve this?

Edmund


Hi, I'm having the same issue. Anyone found a solution yet?

As you mentioned, I can get around it by setting the attributes programmatically but this isn't really desirable


Try this solution it worked for me:

Create a private variable to temporarily store the value of the property. Override the render method of the user control to assign the property to the textbox.

Private _maxLengthAsInteger

PublicProperty MaxLength()AsInteger
Get
Return txtValue.MaxLength
EndGet
Set(ByVal valueAsInteger)
_maxLength = value
End Set
EndPropertyOverridesSub Render(ByVal writerAs System.Web.UI.HtmlTextWriter)
txtValue.MaxLength = _maxLength
MyBase.Render(writer)
EndSub

Good luck, Hanan Schwartzberg
--------
Custom programming and web design
http://www.lionsden.co.il

Has a solution been found to this problem?

As for the temporary variable in the property suggestion, wouldn't that have a problem for any property accessed before the render occurred ie the set value is in the temp but the get is pointing to the actual child control. Seems like this causes an out of sync issue with the property values until the render method occurs.

Is there any way to ensure that the child controls exist before a property tries to get set without having to change a usercontrol into a custom control?


I believe you are correct about the problem with loading the temporary values into the controls being in the render method. The solution is, in my example, to move

txtValue.MaxLength = _maxLength

into the Page.Load method.

Hanan

Wednesday, March 21, 2012

Events are firing at the wrong time, I dont know how to stop them

Hi,

Here are the files for a very simple project containing some user controls.

http://www.wmscanner.com/Happiness.zip

If you open the fileUserControls/StrikeDistance.ascx.cs you will be able to quickly see three event handlers that I have marked with comments.

My problem is that these event handlers are being fired on every single postback even when I don't want them to fire.

The controls contain an AJAX SliderExtension, and I want the event handlers to fire ONLY after a user has adjusted one of the sliders. Instead, the events are firing willy nilly, especially on the first couple of postbacks.

I have made the attached project very simple, to isolate the problem, so it should be pretty easy for someone expert in control lifecycles to step through and see where my code is "improveable" :)

Many, many thanks in advance, i've been working on this one problem an entire day now :)

Ben

OK, I found the problem and it was my fault. I was setting the sliderextender's value to 0.1 when it's "Decimals" property was set to zero. The slider extender was resetting the text of its bound control, and that was firing the unwanted events.

Escape Key Behaviour

Hey,

One of my users just discovered the following:

If you hit ESC twice when on a page containing an update panel with a control inside, if you click on anything else that does a postback, you'll get aSystem.ArgumentException: Invalid postback or callback argument. Event validation is enabled using [...] error.

I would like to know more about the default behaviour of the ESC key. And might be interested in hooking up to the event to fire something else when that happens.

Note: I am still using the July CTP (haven't had time / fond it necessary to upgrade yet, so maybe this issue no longer exists now).

Thanks,

Jason

Okay just upgraded to 1.0 RTM and I'm no longer gettingpostback or callback argument exceptions. However, if I do it during a postback, I do see that my updateprogress stays open. In my old one, it didn't seem to have any effect.

Are there any references to the escape key behaviour?

Jason