471,066 Members | 1,181 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 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 1936
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Alphonse Giambrone | last post: by
1 post views Thread by Robert Howells | last post: by
4 posts views Thread by Bas Groeneveld | last post: by
1 post views Thread by Jack | last post: by
1 post views Thread by psparago | last post: by
1 post views Thread by Guadala Harry | last post: by
5 posts views Thread by Chris | last post: by
1 post views Thread by =?Utf-8?B?bWFya203NQ==?= | last post: by
reply views Thread by leo001 | last post: by

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.