Results 1 to 8 of 8

Thread: how to perform validation on textbox in templatecolumn of datagrid?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    how to perform validation on textbox in templatecolumn of datagrid?

    In my DataGrid i've created a textbox in one of the template column which means all rows of that template column will have the same ID for the textbox.

    How do I perform validation on each textbox only when that row is selected using the Select event only

    Thx!
    Last edited by asp.net_vb.net; Jul 16th, 2005 at 06:33 AM.

  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: how to perform validation on textbox in templatecolumn of datagrid?

    Place a validator in the templatecolumn too.

    In the ItemDataBound event, do a FindControl() for the textbox and the validator, and set the validator's ControlToValidate property to the textbox... assign the textbox's ClientId property to the ControlToValidate property.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: how to perform validation on textbox in templatecolumn of datagrid?

    Quote Originally Posted by mendhak
    Place a validator in the templatecolumn too.

    In the ItemDataBound event, do a FindControl() for the textbox and the validator, and set the validator's ControlToValidate property to the textbox... assign the textbox's ClientId property to the ControlToValidate property.
    VB Code:
    1. Dim validator As System.Web.UI.WebControls.RegularExpressionValidator
    2.         validator = CType(e.Item.FindControl("Regularexpressionvalidator123"), System.Web.UI.WebControls.RegularExpressionValidator)
    3.  
    4.         Dim txtbox As System.Web.UI.WebControls.TextBox
    5.         txtbox = CType(e.Item.FindControl("textbox"), System.Web.UI.WebControls.TextBox)
    6.        
    7.         validator.ControlToValidate = txtbox.ClientID

    this is what I typed in the itemdatabound event but there is an error saying
    VB Code:
    1. Line 612:        txtbox = CType(e.Item.FindControl("textbox"), System.Web.UI.WebControls.TextBox)
    2. Line 613:      
    3. Line 614:        validator.ControlToValidate = txtbox.ClientID

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: how to perform validation on textbox in templatecolumn of datagrid?

    Quote Originally Posted by mendhak
    Place a validator in the templatecolumn too.

    In the ItemDataBound event, do a FindControl() for the textbox and the validator, and set the validator's ControlToValidate property to the textbox... assign the textbox's ClientId property to the ControlToValidate property.
    the purpose is to validate the textbox for that row only when a select button is clicked but not other textboxes located in the same column. But the problem is that they have the same ID for the txtbox and when the Controltovalidate is set to the textbox ID, it will validate all of the textbox controls which it should not

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

    Re: how to perform validation on textbox in templatecolumn of datagrid?

    OK, a few changes to what I said.

    Instead of

    validator.ControlToValidate = txtbox.ClientID

    put

    validator.ControlToValidate = txtbox

    That should do it.

    Btw, you never said what the error was, but try what I just said right now.

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

    Re: how to perform validation on textbox in templatecolumn of datagrid?

    That won't work - the ControlToValidate property expects a string, not a control class object.

    You never stated what the error was, but you need the UniqueID property.

    validator.ControlToValidate = txtbox.UniqueID.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: how to perform validation on textbox in templatecolumn of datagrid?

    The error is
    Code:
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error: 
    
    
    Line 613:        txtbox = CType(e.Item.FindControl("textbox"), System.Web.UI.WebControls.TextBox)
    Line 614:       
    Line 615:        validator.ControlToValidate = txtbox.UniqueID

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

    Re: how to perform validation on textbox in templatecolumn of datagrid?

    Quote Originally Posted by nemaroller
    That won't work - the ControlToValidate property expects a string, not a control class object.

    You never stated what the error was, but you need the UniqueID property.

    validator.ControlToValidate = txtbox.UniqueID.
    My bad, but not-so-bad, I had no IDE and Intellisense

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