Search code examples
validationuser-controlsascx

Validation check for controls inside UserControl inside a Repearter on a Page


I have a UserControl say - GiveIdentity.ascx (it has 2 TextBox fields SSN and DOB)

I have another UserControl say - GiveIdentityList.ascx (basically this is a repeater rendering say 3 instances of GiveIdentity UserControl)

Now I have this GiveIdentityList UserControl on say Default.aspx page.

I want to ipliment a 'IsValid' property or method.. on the GiveIdentity.ascx control itself... so that it would retrun whether all textbox fields on that particular control are valid or not... (remember I don't wanna user Page.Validators() or something on the Default.aspx page) - I want the UserControls which are inside another UserControls which is a repeater - to expose the IsValid property - suggesting that all text and date controls inside itself are valid or not....

I appreciate your help... Thanks


Solution

  • You can expose a public method in your user control to any page that uses the control. You should also add an event to alert the parent pages if the state changes. Adding these methods are easy enough:

    Partial Class user_controls_myControl
        Inherits System.Web.UI.UserControl
    
       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
       End sub
    
       Public Function isValid() as boolean
    
          if(me.checkbox1.text<>"" andalso me.checkbox2.text<>"") then
               return true
          else
               return false
          end if 
       End sub
    end Class
    

    Now, in your ASPX page that holds all of these controls, you can check that specific function to make sure they are valid. For instance, if you have a user control called giveIdentity1, you'll want to iterate over the repeater and cast an object to your user control type. You can then check the isValid function like this:

    dim getBool as boolean 
    getBool = me.giveIdentity1.isValid
    

    If you need help iterating over the repeater, let me know

    You can also create your own server events to register changes. You can read a post to a similar topic I responded to here: Handling events of usercontrols within listview

    If this does not answer your question, let me know.