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.
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.
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.
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;
}
Last edited by nemaroller; May 5th, 2005 at 04:16 PM.
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
VB Code:
table1.Rows(0).Cells(2).Controls(0).isvalid
as the property does not exist, and trying it the other way above just does not work.
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.