472,958 Members | 2,371 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.

Inserting Controls Dynamically - with Validators Also Added Dynamically

I have successfully created functionality that mostly models what I'm trying
to do - which is dynamically insert controls into a user control (ascx), and
insert validation controls, also dynamically, for some of the inserted
controls.

The controls (e.g., textBoxes) get created correctly and viewstate is
maintained across postbacks, etc - BUT there is an issue with the validation
controls.

The validation controls, themselves are working - EXCEPT that
1. They do their job only during a Postback (i.e., the page must actually
post back in order for validation to occur); that is, client-side validation
is not happening (i.e., it's not preventing a postback when it should).

2. The validation summary control is apparently not working at all. It
should pop up a message box, but it does nothing.

My question is how to resolve/fix these two issues: I want the validation to
occur client-side (in addition to the default server-side validation), and I
want the validation summary control to work.

FWIW here is my code:
Hosting ASCX:
<%@ Control CodeBehind="ucForm1.ascx.cs" Language="c#"
AutoEventWireup="false" Inherits="MyAssembly.ucForm1" %>
<asp:PlaceHolder id="FormPlaceHolder" runat="server"></asp:PlaceHolder>
<table align="center"><tr><td><asp:Button id="btnSave"
EnableViewState="false" CommandName="btnSave" Visible="true" Width="100px"
runat="server" Text="Save"></asp:Button></td></tr></table>

Relevant Code Behind:
The method named InsertDynamicControls() is called from the OnInit() event
ucForm1.

private void InsertDynamicControls () {
Literal spacer = new Literal();
spacer.Text = "&nbsp;";

// This is where I have all the logic that inserts the FORM controls into
the Placeholder
string s = "<table border=2><tr><td>Yo</td><tr><td>";
s += "<asp:TextBox id=\"txtBox1\" EnableViewState=\"false\"
Width=\"300px\" runat=\"server\"></asp:TextBox>";
s += "</td></tr></tr><tr><td>Some Value</td></tr><tr><td>Other
Value</td></tr></table>";

System.Web.UI.Control c = ParseControl(s);

FormPlaceHolder.Controls.Add(c);

RequiredFieldValidator requiredFieldValidator = new
RequiredFieldValidator();
requiredFieldValidator.ControlToValidate = "txtBox1";
requiredFieldValidator.EnableClientScript = true;
requiredFieldValidator.Enabled = true;
requiredFieldValidator.ErrorMessage = "Yo - You gotta enter something
man!";
requiredFieldValidator.ID = "RequiredFieldValidator_Title";
requiredFieldValidator.Text = "*";
requiredFieldValidator.EnableClientScript = true;

FormPlaceHolder.Controls.Add(spacer);

FormPlaceHolder.Controls.Add(requiredFieldValidato r);

ValidationSummary validationSummary = new ValidationSummary();
validationSummary.DisplayMode =
System.Web.UI.WebControls.ValidationSummaryDisplay Mode.BulletList;
validationSummary.ShowMessageBox = true;
validationSummary.HeaderText = "These things must be fixed before you can
proceed:";
validationSummary.ShowSummary = false;
validationSummary.ID = "validSummary1";
validationSummary.EnableClientScript = true;

FormPlaceHolder.Controls.Add(spacer);
FormPlaceHolder.Controls.Add(validationSummary);
}

Thanks!
Nov 19 '05 #1
1 2017
The overall code looks fine, except you are adding one control (spacer) in
multiple places (which I thought was illegal). So I wonder if the
WebUIValidation.js script file simply isn't loading.

Take a look at this thread for several ideas:
http://forums.asp.net/739537/ShowPost.aspx

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Jeffrey Todd" <Me@Somewhere.net> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
I have successfully created functionality that mostly models what I'm
trying to do - which is dynamically insert controls into a user control
(ascx), and insert validation controls, also dynamically, for some of the
inserted controls.

The controls (e.g., textBoxes) get created correctly and viewstate is
maintained across postbacks, etc - BUT there is an issue with the
validation controls.

The validation controls, themselves are working - EXCEPT that
1. They do their job only during a Postback (i.e., the page must actually
post back in order for validation to occur); that is, client-side
validation is not happening (i.e., it's not preventing a postback when it
should).

2. The validation summary control is apparently not working at all. It
should pop up a message box, but it does nothing.

My question is how to resolve/fix these two issues: I want the validation
to occur client-side (in addition to the default server-side validation),
and I want the validation summary control to work.

FWIW here is my code:
Hosting ASCX:
<%@ Control CodeBehind="ucForm1.ascx.cs" Language="c#"
AutoEventWireup="false" Inherits="MyAssembly.ucForm1" %>
<asp:PlaceHolder id="FormPlaceHolder" runat="server"></asp:PlaceHolder>
<table align="center"><tr><td><asp:Button id="btnSave"
EnableViewState="false" CommandName="btnSave" Visible="true" Width="100px"
runat="server" Text="Save"></asp:Button></td></tr></table>

Relevant Code Behind:
The method named InsertDynamicControls() is called from the OnInit() event
ucForm1.

private void InsertDynamicControls () {
Literal spacer = new Literal();
spacer.Text = "&nbsp;";

// This is where I have all the logic that inserts the FORM controls into
the Placeholder
string s = "<table border=2><tr><td>Yo</td><tr><td>";
s += "<asp:TextBox id=\"txtBox1\" EnableViewState=\"false\"
Width=\"300px\" runat=\"server\"></asp:TextBox>";
s += "</td></tr></tr><tr><td>Some Value</td></tr><tr><td>Other
Value</td></tr></table>";

System.Web.UI.Control c = ParseControl(s);

FormPlaceHolder.Controls.Add(c);

RequiredFieldValidator requiredFieldValidator = new
RequiredFieldValidator();
requiredFieldValidator.ControlToValidate = "txtBox1";
requiredFieldValidator.EnableClientScript = true;
requiredFieldValidator.Enabled = true;
requiredFieldValidator.ErrorMessage = "Yo - You gotta enter something
man!";
requiredFieldValidator.ID = "RequiredFieldValidator_Title";
requiredFieldValidator.Text = "*";
requiredFieldValidator.EnableClientScript = true;

FormPlaceHolder.Controls.Add(spacer);

FormPlaceHolder.Controls.Add(requiredFieldValidato r);

ValidationSummary validationSummary = new ValidationSummary();
validationSummary.DisplayMode =
System.Web.UI.WebControls.ValidationSummaryDisplay Mode.BulletList;
validationSummary.ShowMessageBox = true;
validationSummary.HeaderText = "These things must be fixed before you can
proceed:";
validationSummary.ShowSummary = false;
validationSummary.ID = "validSummary1";
validationSummary.EnableClientScript = true;

FormPlaceHolder.Controls.Add(spacer);
FormPlaceHolder.Controls.Add(validationSummary);
}

Thanks!

Nov 19 '05 #2

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

Similar topics

10
by: Alphonse Giambrone | last post by:
I have a web form with 2 user controls on it (UC1 and UC2). Each control has a bound datagrid with textboxes in the footer to add a new row. There are also requiredfieldvalidators in each footer....
2
by: Christian H | last post by:
Based on the content in my database, I need to populate different form controls and validators, such as textbox, dropdownlist, requiredfieldvalidators , etc. Then I need to check if the form has...
1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
1
by: Jack | last post by:
Hi, I have a page with a repeater control that contains textboxes. I'm trying to create a validation class that is called by my page. A method in this class iterates through all the controls...
1
by: psparago | last post by:
I have developed a tab user control in which each tab is itself a user control and the tab selection control is a datalist. Each tabbed user control has zero or more validator controls on it. The...
1
by: Guadala Harry | last post by:
I will be creating a new ASPX that requires many controls to be inserted into the page at runtime. The controls include things like TextBox, RadioButtonList, CheckBoxList, DropDownList, etc....
5
by: Chris | last post by:
I have a page with mixture of static and dynamically added controls is there any way of controlling the order which they are added to the page. My submit button (statically added) appears before...
1
by: =?Utf-8?B?bWFya203NQ==?= | last post by:
I have a simple page i'm trying to do dynmaically.. i have a page called submitcomments.aspx with the .cs codebehind.. before i created everything in design view.. now i've ripped that out and...
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 :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
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.