This is how we did it in our app.
 
When we loop through the dataset to create controls dynamically, we have
a flag too which tells us whether we need to add a validator to the
control or not. If the flag says Y, then right after the code that adds
the control to the form we add the validator code and assign the ID of
the control just added above to the ControlToValidate property of the
validator control.
 
Here is an example:
 
foreach(DataRow row in DataSet.Tables[0].Rows)
{
            if(row["ControlType"].ToString().Equals("textbox"))
            {
                        TextBox tb = new TextBox();
                        tb.ID = row["ControlID"].ToString();
                        this.Controls.Add(tb);
                        
                        if(row{"IsRequired"].ToString().Equals("Y"))
                        {
                          RequiredFieldValidator rfv = new
RequiredFieldValidator();
                          rfv.ControlToValidate =
row["ControlID"].ToString();
                          rfv.ErroMessage = "your error message";
                          this.Controls.Add(rfv);
                        }
            }
}
 
Rajendra.
 
-----Original Message-----
From: moron_psychomaniac [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 16, 2004 5:02 AM
To: [EMAIL PROTECTED]
Subject: [AspNetAnyQuestionIsOk] Validator for dynamically created
control
 
I have just created a dynamic ASP.NET form. The fields and its type 
(textbox, checkbox, radiobutton, etc) are controlled through the SQL 
database.
Now I want to validate user-input by using the validator provided by 
ASP.NET. However, I can't really do it because all my controls 
(textbox, checkbox, etc) are created dynamically.
Here's a sample to the code for validator:

<asp:RequiredFieldValidator id="valRequired" runat="server" 
ControlToValidate="textbox1"
    ErrorMessage="* You must enter a value into textbox1" 
Display="dynamic">*
</asp:RequiredFieldValidator>

Notice that it has a field "ControlToValidate". It means I have to 
specify the name of the control to be validated even before the 
control itself is even created.

Anybody has a suggestion?
Thanks a lot!




Yahoo! Groups Sponsor
ADVERTISEMENT
click here
<http://us.ard.yahoo.com/SIG=129es87p2/M=298184.5285298.6392945.3001176/
D=groups/S=1705006764:HM/EXP=1095411739/A=2352667/R=0/SIG=11t4iivpf/*htt
p:/www.netflix.com/Default?mqso=60185359&partid=5285298> 
 
<http://us.adserver.yahoo.com/l?M=298184.5285298.6392945.3001176/D=group
s/S=:HM/A=2352667/rand=359831111> 
 
  _____  

Yahoo! Groups Links
*         To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
  
*         To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]
cribe> 
  
*         Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service <http://docs.yahoo.com/info/terms/> . 


[Non-text portions of this message have been removed]



------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to