473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1864

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.