Results 1 to 9 of 9

Thread: Validating dynamically created textboxes

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Validating dynamically created textboxes

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Validating dynamically created textboxes

    Each Validator has a ControlToValidate property, doesn't it?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Re: Validating dynamically created textboxes

    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.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Validating dynamically created textboxes

    I'll look into this when I get into work, can you show all the relevant code you have so far?

  5. #5
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Re: Validating dynamically created textboxes

    Example:
    VB Code:
    1. Public myTextBox As TextBox
    2. Public myValidator As CustomValidator
    3.  
    4. myTextBox = New TextBox
    5.  
    6. myValidator = New CustomValidator
    7. myValidator.ControlToValidate = myTextBox.ID
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: Validating dynamically created textboxes

    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:
    1. Public myTextBox As TextBox
    2. Public myValidator As CustomValidator
    3.  
    4. myTextBox = New TextBox
    5.  
    6. myValidator = New CustomValidator
    7. 'TEXTBOX must be added to its container first
    8. Controls.Add(myTextBox)
    9. myValidator.ControlToValidate = myTextBox.ID

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Re: Validating dynamically created textboxes

    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:
    1. dim tr as new tablerow
    2.         dim tc as new tablecell
    3.  
    4.         dim tb as new textbox
    5.         tb.id = "txt1"
    6.         tc.contols.add(tb)
    7.         tr.cells.add(tc)
    8.  
    9.         tc=new tablecell
    10.  
    11.         dim cv as new customvalidator
    12.         cv.controltovalidate = "txt1"
    13.         tc.controls.add (cv)
    14.  
    15.         tr.cells.add(tc)
    16.  
    17.         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.
    Last edited by davidrobin; May 6th, 2005 at 03:37 AM.

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: Validating dynamically created textboxes

    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.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Re: Validating dynamically created textboxes

    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:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.  
    4.  
    5.         '****************************************************************
    6.         '*
    7.         '
    8.         Dim tr As New TableRow
    9.         Dim cell As New TableCell
    10.  
    11.         Dim t As New TextBox
    12.  
    13.         cell.Controls.Add(t)
    14.         tr.Cells.Add(cell)
    15.  
    16.         cell = New TableCell
    17.         cell.Text = "hello?"
    18.         tr.Cells.Add(cell)
    19.  
    20.         cell = New TableCell
    21.         Dim p As New CustomValidator
    22.         cell.Controls.Add(p)
    23.         tr.Cells.Add(cell)
    24.  
    25.         Table1.Rows.Add(tr)
    26.  
    27.         Table1.GridLines = GridLines.Both
    28.  
    29.         p.ControlToValidate = t.ClientID
    30.  
    31.         Response.Write(t.ClientID)
    32.         Response.Write(p.ControlToValidate)
    33.         Response.Write(p.ClientID)
    34.     End Sub
    35.  
    36.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    37.  
    38.         'Property does not exist
    39.         'table1.Rows(0).Cells(2).Controls(0).isvalid
    40.  
    41.         'This does not work
    42.         Dim c As New CustomValidator
    43.         c = DirectCast(Table1.Rows(0).Cells(2).Controls(0), CustomValidator)
    44.  
    45.         Dim t As New TextBox
    46.         t = DirectCast(Table1.Rows(0).Cells(0).Controls(0), TextBox)
    47.  
    48.         If t.Text <> "qwerty" Then
    49.             c.IsValid = False
    50.         Else
    51.             c.IsValid = True
    52.         End If
    53.     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:
    1. 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.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width