I am dynamically creating textboxes and for each textbox a custom validator control.
How do I link the validator control to the textbox so it can be Validated.
Thanks
Printable View
I am dynamically creating textboxes and for each textbox a custom validator control.
How do I link the validator control to the textbox so it can be Validated.
Thanks
Each Validator has a ControlToValidate property, doesn't it?
It does but with the custom validator control you cannot reference it in code as it is not a physical control on the webform.
I have also not managed to linke any dynamically created validation control to a dynamically created textbox. For me it just doesn't work, so I am hoping others have had better success and can tell me how.
I'll look into this when I get into work, can you show all the relevant code you have so far?
Example:
VB Code:
Public myTextBox As TextBox Public myValidator As CustomValidator myTextBox = New TextBox myValidator = New CustomValidator myValidator.ControlToValidate = myTextBox.ID
A dynamically created textbox does not have an ID until it is added to its container.
An easily missed clarification is - you have to set the ControlToValidate property only after the control you want to validate has been added to its container - otherwise it does have an ID, and you will get the infamous 'ControlToValidate cannot be set to nothing' error.
VB Code:
Public myTextBox As TextBox Public myValidator As CustomValidator myTextBox = New TextBox myValidator = New CustomValidator 'TEXTBOX must be added to its container first Controls.Add(myTextBox) myValidator.ControlToValidate = myTextBox.ID
I am not at work at the moment but the way I am doing it is this.
This code is done from memory so there may be minor syntactic errors.
VB Code:
dim tr as new tablerow dim tc as new tablecell dim tb as new textbox tb.id = "txt1" tc.contols.add(tb) tr.cells.add(tc) tc=new tablecell dim cv as new customvalidator cv.controltovalidate = "txt1" tc.controls.add (cv) tr.cells.add(tc) table1.rows.add(tr)
As you can see I am creating an new textbox, giving it an ID then adding it to the table web control.
I wonder if the textbox retains the id I specify? I am presuming it does but it would be good to check that out.
Do you think the fact the controls are contained within a table makes any difference? I wouldn't have thought so.
Specificying an id probably won't make a difference. The framework normally appends the name of the control, or increments from control collection.
For instance, if the control was added to a panel...
pnl0_txt1
would be the clientid seen on the client-side.
Anyway, you just have to be sure to set the controlToValidate property after the controls have been added to the parent container - without which, how could the framework possibly know how to identify the controls? The framework must use the parent_child naming scheme mentioned above for postback and viewstate, so you can only accurately assign it after it has been added to the container.
The below code is in C#, but I think you can convert it to VB easily enough since its mostly .Net methods except the declarations.
Code:private void Page_Load(object sender, System.EventArgs e)
{
CustomValidator validator;
Table table;
TableRow tr;
TableCell tc;
TextBox textbox;
textbox = new TextBox();
validator = new CustomValidator();
tr = new TableRow();
tc = new TableCell();
tc.Controls.Add(textbox);
tr.Cells.Add(tc);
tc = new TableCell();
tc.Controls.Add(validator);
tr.Cells.Add(tc);
table = new Table();
table.Rows.Add(tr);
//following line for this demonstration.. adds controls to the document.
Page.Controls[1].Controls.Add(table);
//now that the controls have been added to the parent
//container, you can properly assign the property. It is
//only after the controls have been added, can the framework
//distinguish the naming scheme.
validator.ControlToValidate = textbox.ClientID;
}
Thanks for all your help, I am getting closer now but there are a few things I need to iron out.
I have a web form with a table and a button (web controls).
This is the code on the form.
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here '**************************************************************** '* ' Dim tr As New TableRow Dim cell As New TableCell Dim t As New TextBox cell.Controls.Add(t) tr.Cells.Add(cell) cell = New TableCell cell.Text = "hello?" tr.Cells.Add(cell) cell = New TableCell Dim p As New CustomValidator cell.Controls.Add(p) tr.Cells.Add(cell) Table1.Rows.Add(tr) Table1.GridLines = GridLines.Both p.ControlToValidate = t.ClientID Response.Write(t.ClientID) Response.Write(p.ControlToValidate) Response.Write(p.ClientID) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Property does not exist 'table1.Rows(0).Cells(2).Controls(0).isvalid 'This does not work Dim c As New CustomValidator c = DirectCast(Table1.Rows(0).Cells(2).Controls(0), CustomValidator) Dim t As New TextBox t = DirectCast(Table1.Rows(0).Cells(0).Controls(0), TextBox) If t.Text <> "qwerty" Then c.IsValid = False Else c.IsValid = True End If End Sub
Now my problem is how to reference the custom validator so I can set the IsValid property to true/false. I can't do it this way
as the property does not exist, and trying it the other way above just does not work.VB Code:
table1.Rows(0).Cells(2).Controls(0).isvalid
Another interesting thing is (Which is OK for me because I want a CustomValidator control) if you change the validator control to a requiredfieldvalidator control the page will not postback when you click the button. At least that is what happened to me.
So the question remains:
How can I set the IsValid property of a customValidator control created dynamically when a button is pressed?
I have attached the webform to save you copying and pasting code.