Tags: ajax, aspnet, composite, control, inside, maskededit, maskededitextender, masterpage, net

MaskedEdit inside Composite control on Masterpage

On .Net » ASP.NET AJAX

18,065 words with 2 Comments; publish: Sun, 06 Jan 2008 04:32:00 GMT; (100187.50, « »)

Hi!

I'm trying to get a MaskedEditExtender to work inside a composite control which I want to use inside a Masterpage. It works if I put my composite control directly on an simple ASPX page, but if I use a masterpage on the ASPX page, the MaskedEditExtender control inside my composite control fails to find the TargetControlID.

This is my composite control:

using System;

using System.Collections.Generic;

using System.Text;

using System.Web.UI.WebControls;

using AjaxControlToolkit;

namespace WAF.Presentation.Web {

publicclassMaskedEditTest :CompositeControl {

privateMaskedEditExtender maskedEditExtender;

privateMaskedEditValidator maskedEditValidator;

privateTextBox txtTimeString;

protectedoverridevoid Render(System.Web.UI.HtmlTextWriter writer) {

writer.Write("Textbox: ");

txtTimeString.RenderControl(writer);

writer.WriteBreak();

maskedEditExtender.RenderControl(writer);

maskedEditValidator.RenderControl(writer);

}

protectedoverridevoid OnInit(EventArgs e) {

EnsureChildControls();

base.OnInit(e);

}

protectedoverridevoid CreateChildControls() {

txtTimeString =newTextBox();

txtTimeString.ID =this.UniqueID +"_Textbox";

this.Controls.Add(txtTimeString);

maskedEditExtender =newMaskedEditExtender();

maskedEditExtender.TargetControlID = txtTimeString.ID;

maskedEditExtender.ID =this.UniqueID +"_maskedEditExtender";

maskedEditExtender.Mask ="99:99";

maskedEditExtender.MessageValidatorTip =true;

maskedEditExtender.OnFocusCssClass ="TimeOfDayMaskedEditFocus";

maskedEditExtender.OnFocusCssNegative ="TimeOfDayMaskedEdtError";

maskedEditExtender.MaskType =MaskedEditType.Time;

maskedEditExtender.DisplayMoney =MaskedEditShowSymbol.None;

maskedEditExtender.AcceptNegative =MaskedEditShowSymbol.None;

this.Controls.Add(maskedEditExtender);

maskedEditValidator =newMaskedEditValidator();

maskedEditValidator.ControlExtender = maskedEditExtender.ID;

maskedEditValidator.ControlToValidate = txtTimeString.ID;

maskedEditValidator.IsValidEmpty =false;

maskedEditValidator.EmptyValueMessage ="Date is required";

maskedEditValidator.InvalidValueMessage ="Date is invalid";

maskedEditValidator.Display =ValidatorDisplay.Dynamic;

maskedEditValidator.TooltipMessage ="Input a Date";

this.Controls.Add(maskedEditValidator);

base.CreateChildControls();

}

}

}

My ASPX page without a masterpage (this works);

<%

.net-ajax.todaysummary.com.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

<%.net-ajax.todaysummary.com.RegisterAssembly="Controls"Namespace="WAF.Presentation.Web"TagPrefix="cc1" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<asp:ScriptManagerID="ScriptManager1"runat="server"/>

<div>

<cc1:maskededittestid="MaskedEditTest1"runat="server"></cc1:maskededittest>

</div>

</form>

</body>

</html>

If I use a simple masterpage, it fails with the following message "Object reference not set to an instance of an object." on the following line of code in the OnLoad method in MaskedEditExtender.cs from the toolkit:

((TextBox)this.FindControl(TargetControlID)).MaxLength = 0;

My aspx page looks like this with the masterpage:

<%

.net-ajax.todaysummary.com.PageLanguage="C#"MasterPageFile="~/MasterPage.master"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

<%.net-ajax.todaysummary.com.RegisterAssembly="Controls"Namespace="WAF.Presentation.Web"TagPrefix="cc1" %>

<

asp:ContentContentPlaceHolderID="ContentPlaceHolder1"ID="content1"runat="server">

<asp:ScriptManagerID="ScriptManager1"runat="server"/>

<div>

<cc1:maskededittestid="MaskedEditTest1"runat="server"></cc1:maskededittest>

</div>

</asp:Content>My masterpage:

<%

.net-ajax.todaysummary.com.MasterLanguage="C#"AutoEventWireup="true"CodeFile="MasterPage.master.cs"Inherits="MasterPage" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:contentplaceholderid="ContentPlaceHolder1"runat="server">

</asp:contentplaceholder>

</div>

</form>

</body>

</html>

Any suggestions on what might be happening, and how I can solve this?

All Comments

Leave a comment...

  • 2 Comments
    • Hi vlangber,

      The workaround now is to comment this line.

      This piece of code guarantees that the Maxlength property is equal the zero, but if you guarantee Maxlength is equal the zero you can to comment this code.

      Thanks,

      Fernando Cerqueira

      #1; Sun, 06 Jan 2008 04:33:00 GMT
    • Sorry vlangber,

      I answered without investigating the context.

      + "to _maskedEditExtender";

      [ ]'s

      #2; Sun, 06 Jan 2008 04:34:00 GMT