473,651 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="ucF orm1.ascx.cs" Language="c#"
AutoEventWireup ="false" Inherits="MyAss embly.ucForm1" %>
<asp:PlaceHolde r id="FormPlaceHo lder" runat="server"> </asp:PlaceHolder >
<table align="center"> <tr><td><asp:Bu tton id="btnSave"
EnableViewState ="false" CommandName="bt nSave" Visible="true" Width="100px"
runat="server" Text="Save"></asp:Button></td></tr></table>

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

private void InsertDynamicCo ntrols () {
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><t d>Yo</td><tr><td>";
s += "<asp:TextB ox id=\"txtBox1\" EnableViewState =\"false\"
Width=\"300px\" runat=\"server\ "></asp:TextBox>";
s += "</td></tr></tr><tr><td>Some Value</td></tr><tr><td>Othe r
Value</td></tr></table>";

System.Web.UI.C ontrol c = ParseControl(s) ;

FormPlaceHolder .Controls.Add(c );

RequiredFieldVa lidator requiredFieldVa lidator = new
RequiredFieldVa lidator();
requiredFieldVa lidator.Control ToValidate = "txtBox1";
requiredFieldVa lidator.EnableC lientScript = true;
requiredFieldVa lidator.Enabled = true;
requiredFieldVa lidator.ErrorMe ssage = "Yo - You gotta enter something
man!";
requiredFieldVa lidator.ID = "RequiredFieldV alidator_Title" ;
requiredFieldVa lidator.Text = "*";
requiredFieldVa lidator.EnableC lientScript = true;

FormPlaceHolder .Controls.Add(s pacer);

FormPlaceHolder .Controls.Add(r equiredFieldVal idator);

ValidationSumma ry validationSumma ry = new ValidationSumma ry();
validationSumma ry.DisplayMode =
System.Web.UI.W ebControls.Vali dationSummaryDi splayMode.Bulle tList;
validationSumma ry.ShowMessageB ox = true;
validationSumma ry.HeaderText = "These things must be fixed before you can
proceed:";
validationSumma ry.ShowSummary = false;
validationSumma ry.ID = "validSummary1" ;
validationSumma ry.EnableClient Script = true;

FormPlaceHolder .Controls.Add(s pacer);
FormPlaceHolder .Controls.Add(v alidationSummar y);
}

Thanks!
Nov 19 '05 #1
1 2054
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****@PeterBlu m.com
Creator of "Profession al Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Jeffrey Todd" <Me@Somewhere.n et> wrote in message
news:%2******** *******@TK2MSFT NGP15.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="ucF orm1.ascx.cs" Language="c#"
AutoEventWireup ="false" Inherits="MyAss embly.ucForm1" %>
<asp:PlaceHolde r id="FormPlaceHo lder" runat="server"> </asp:PlaceHolder >
<table align="center"> <tr><td><asp:Bu tton id="btnSave"
EnableViewState ="false" CommandName="bt nSave" Visible="true" Width="100px"
runat="server" Text="Save"></asp:Button></td></tr></table>

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

private void InsertDynamicCo ntrols () {
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><t d>Yo</td><tr><td>";
s += "<asp:TextB ox id=\"txtBox1\" EnableViewState =\"false\"
Width=\"300px\" runat=\"server\ "></asp:TextBox>";
s += "</td></tr></tr><tr><td>Some Value</td></tr><tr><td>Othe r
Value</td></tr></table>";

System.Web.UI.C ontrol c = ParseControl(s) ;

FormPlaceHolder .Controls.Add(c );

RequiredFieldVa lidator requiredFieldVa lidator = new
RequiredFieldVa lidator();
requiredFieldVa lidator.Control ToValidate = "txtBox1";
requiredFieldVa lidator.EnableC lientScript = true;
requiredFieldVa lidator.Enabled = true;
requiredFieldVa lidator.ErrorMe ssage = "Yo - You gotta enter something
man!";
requiredFieldVa lidator.ID = "RequiredFieldV alidator_Title" ;
requiredFieldVa lidator.Text = "*";
requiredFieldVa lidator.EnableC lientScript = true;

FormPlaceHolder .Controls.Add(s pacer);

FormPlaceHolder .Controls.Add(r equiredFieldVal idator);

ValidationSumma ry validationSumma ry = new ValidationSumma ry();
validationSumma ry.DisplayMode =
System.Web.UI.W ebControls.Vali dationSummaryDi splayMode.Bulle tList;
validationSumma ry.ShowMessageB ox = true;
validationSumma ry.HeaderText = "These things must be fixed before you can
proceed:";
validationSumma ry.ShowSummary = false;
validationSumma ry.ID = "validSummary1" ;
validationSumma ry.EnableClient Script = true;

FormPlaceHolder .Controls.Add(s pacer);
FormPlaceHolder .Controls.Add(v alidationSummar y);
}

Thanks!

Nov 19 '05 #2

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

Similar topics

10
3171
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. When I try to add a new row to UC1 the requiredfieldvalidators for UC2 fire and therefore the page is not posted. If I remove one control, everything works fine. I have 2 questions. 1. How can I get the validators for each control to only...
2
6469
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 been filled out correctly. If so, I need to update a database, and do other things. In order to check if the form is ok, I've tried looping through the validator controls, and I've also tried using page.isvalid.
1
2130
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 to programming, but I am new to ASP.NET and Web application design in general... loved the concept of user controls and dynamically adding them to a page. So what I wound up with was an application that dynamically loads two user controls directly...
4
2494
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 the table. I know that I can dynamically add controls (eg a textbox) to the page controls collection of a web form in a server event which will then be rendered onto the form, as in the following snippet: System.Web.UI.WebControls.TextBox...
1
4718
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 on the page. On encountering a TextBox control, it looks at the ID to discern the field the TextBox represents, and then creates the appropriate validation controls and adds them to the page. The controlToValidate property of each validation...
1
1354
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 hosting page has a summary validator on it. Each tabbed user control is created dynamically on page_init (on both postback and not postback), but only the tabbed user control which corresponds to the selected tab is marked as visible. This has...
1
1116
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. Validators will be added dynamically as well - for some of these controls. When the page is posted back to the server, I will need to be able to retrieve the values of the controls. ViewState will need to be maintained for these controls for cases...
5
1956
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 some textboxes (dynamically added). I know I could move it around with CSS but I want to move towards an accessible site that will display forms in the right order without CSS. Regards, Chris.
1
1206
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 added a asp panel to the page and created a procedure in my c# datalayer.cs file called drawsubmitcomments in that procedure i create the text boxes and buttons.
0
8802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.