472,958 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Simple Page Template with Form Problem

42
I implemented a simple class inherited from Page to create a page
template. It simply wraps some trivial html around the inherited page,
and puts the inherited page into a form.

The problem I have run into is that the emitted html at the end of the
process is slightly different and doesn't work.

Please don't be put off by all the source code. All the guts are in this
first base class, and it doesn't do much. The rest is trivial
boilerplate, and the ugly stuff near the bottom is emitted code, and its
only there to highlight the problem, which has already been located.

Source for BaseClass
--------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Test
{
public class TestPageBase : System.Web.UI.Page
{
protected override void OnInit(System.EventArgs e)
{
BuildPage( GenerateHtmlForm() );
base.OnInit(e);
}

protected void BuildPage(HtmlForm form)
{
////////////////////////////////////////////////////////
// Build the page and include inherited page content

this.Controls.AddAt( 0, new LiteralControl(
@"<HTML><body>") );

this.Controls.Add(form);

this.Controls.Add( new LiteralControl( @"</body></HTML>"));
}

private HtmlForm GenerateHtmlForm()
{
HtmlForm form = new HtmlForm();
AddControlsFromDerivedPage(form);
return form;
}
private void AddControlsFromDerivedPage(HtmlForm form)
{
int count = this.Controls.Count;
for( int i = 0; i<count; ++i )
{
System.Web.UI.Control ctrl = this.Controls[0];
form.Controls.Add( ctrl );
this.Controls.Remove( ctrl );
}
}
}
}

The inherited page is dead simple, and is a simple
'requiredfieldvalidator' example:
It should display a textbox, and a button. If you press submit when the
textbox is empty it should show the message.
------
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
Inherits="AccelRate.test" %>

Name: <asp:textbox id=name runat="server"/>
<br>
<asp:button id=Button1 runat="server" Text="Submit"/>
<br>
<asp:requiredfieldvalidator id=Requiredfieldvalidator1 runat="server"
Text="The name field is required!" ControlToValidate="name"/>
-------
And the codebehind equally trivial, all boilerplate except that it
inherits from test page base.

-------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Test
{
/// <summary>
/// Summary description for page1.
/// </summary>
public class test : Test.TestPageBase
{
protected System.Web.UI.WebControls.TextBox name;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.RequiredFieldValidator
Requiredfieldvalidator1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web
Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

--------
If you manually created the page it would be simply:
<html>
<body>
<form>
Name: <asp:textbox id=name runat="server"/>
<br>
<asp:button id=Button1 runat="server" Text="Submit"/>
<br>
<asp:requiredfieldvalidator id=Requiredfieldvalidator1 runat="server"
Text="The name field is required!" ControlToValidate="name"/>
</form>
</body>
</html>

------
The problem I've encountered is that the requiredfieldvalidator in the
first example doesn't work but does in the 2nd. I have determined that
the emitted code is slightly different for the 2 examples:

The first example (which does not work) results in:
-----------------
<HTML>
<body>
<form name="_ctl0" method="post" action="test.aspx"
language="javascript" onsubmit="ValidatorOnSubmit();" id="_ctl0">
<input type="hidden" name="__VIEWSTATE"
value="dDwtOTAwMjE1Mjk7Oz6ozVxOm9z0Jb82dYC0l6HUS35 h6w==" />

<script language="javascript"
src="/aspnet_client/system_web/1_1_4322/WebUIValidation.js"></script>

Name: <input name="name" type="text" id="name" />
<br>
<input type="submit" name="Button1" value="Submit" id="Button1" />
<br>
<span id="Requiredfieldvalidator1" controltovalidate="name"
evaluationfunction="RequiredFieldValidatorEvaluate IsValid"
initialvalue="" style="color:Red;visibility:hidden;">The name field is
required!</span>

<script language="javascript">
<!--
var Page_Validators = new
Array(document.all["Requiredfieldvalidator1"]);
// -->
</script>
<script language="javascript">
<!--
var Page_ValidationActive = false;
if (typeof(clientInformation) != "undefined" &&
clientInformation.appName.indexOf("Explorer") != -1) {
if (typeof(Page_ValidationVer) == "undefined")
alert("Unable to find script library
'/aspnet_client/system_web/1_1_4322/WebUIValidation.js'. Try placing
this file manually, or reinstall by running 'aspnet_regiis -c'.");
else if (Page_ValidationVer != "125")
alert("This page uses an incorrect version of
WebUIValidation.js. The page expects version 125. The script library is
" + Page_ValidationVer + ".");
else
ValidatorOnLoad();
}

function ValidatorOnSubmit() {
if (Page_ValidationActive) {
ValidatorCommonOnSubmit();
}
}
// -->
</script>
</form>
</body>
</HTML>

-------
The 2nd example, which does work is identical, except for the:

<input type="submit" name="Button1" value="Submit" id="Button1" />

becomes:

<input type="submit" name="Button1" value="Submit" onclick="if
(typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
language="javascript" id="Button1" />

Obviously the missing onclick is responsible for the validator not
working in the first example!!!
My question is threefold: WHY is the onclick event missing, WHAT did I
do wrong, and above all HOW do I revise my template solution to get it
to work?

Thanks in advance

Nov 18 '05 #1
0 1835

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Droolboy | last post by:
I'm trying to build a fairly small (max. 10 different pages) site using php, and it's becoming obvious that I need some kind of model view separation. Having done a few searches, I've come...
1
by: Dick Rosser | last post by:
Greetings, I have scanned all the script repositories to no avail trying to find what would be, I thought, a fairly simple script. What I am trying to achieve is as follows, perhaps someone can...
0
by: David Rose | last post by:
Hello I know nothing of XSL but was wondering if it was possible to do the following: Given an XML template: <template> <register TagPrefix="whatever" TagName="Template"...
1
by: suzy | last post by:
hi, i have created a aspx page template by creating a template.cs file which inherits from the page class. In this file I override the OnInit event and create a template in the form of a...
2
by: John Boers | last post by:
I am trying to create a template for my website. But I have a problem, the system says BC30469: Reference to a non-shared member requires an object reference and points to the statement...
4
by: James Thomas | last post by:
Hello - I am attempting to override the Page control so it outputs a simple copyright message at the bottom of every page. The problem I'm running into is that it puts said message below the...
3
by: MDS | last post by:
I have quite a few of web pages in my project which would have similar appearance (like header, some images, color, footer etc). How do i create one base template web page and have all other...
3
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I'm trying to follow the instructions on http://www.asp.net/learn/videos/view.aspx?tabid=63&id=75 for a simple AJAX demo. However, my code updates all 3 labels even though only 1 of them is...
2
by: JRough | last post by:
I have this code that switches templates depending on if the user fills in a form with a request. The request asks for the $mark & $number. If that request gets input then it displays a list...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.