473,322 Members | 1,755 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,322 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 2036
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: 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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.