J
Jeffrey Todd
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
laceHolder id="FormPlaceHolder" runat="server"></asp
laceHolder>
<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 = " ";
// 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(requiredFieldValidator);
ValidationSummary validationSummary = new ValidationSummary();
validationSummary.DisplayMode =
System.Web.UI.WebControls.ValidationSummaryDisplayMode.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!
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
<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 = " ";
// 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(requiredFieldValidator);
ValidationSummary validationSummary = new ValidationSummary();
validationSummary.DisplayMode =
System.Web.UI.WebControls.ValidationSummaryDisplayMode.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!