Custom Client-Side Validation in Web User Control

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Custom Client-Side Validation in Web User Control

    Guys,

    I have a custom validator that has the default property ControlToValidate
    plus one called SecondControlToValidate that I have created, like so...

    <ows:eitherorvalidator id="eovNumberName" runat="server"
    ControlToValidate="txtNumber" SecondControlToValidate="txtName"
    EnableClientScript="True"></ows:eitherorvalidator>

    ....in my Validator Class I have the following...

    Protected eitherOrSecondControlToValidate As String

    Public Property SecondControlToValidate() As String
    Get
    Return eitherOrSecondControlToValidate
    End Get
    Set(ByVal Value As String)
    eitherOrSecondControlToValidate = Value
    End Set
    End Property

    Protected Overrides Sub AddAttributesToRender(ByVal writer As
    HtmlTextWriter)
    If (Me.DetermineRenderUplevel And Me.EnableClientScript) Then
    MyBase.AddAttributesToRender(writer)
    writer.AddAttribute("secondcontroltovalidate",
    Me.SecondControlToValidate)
    End If
    End Sub

    All this works fine when the textboxes and validator is straight on the
    page, but I can't get it working in a Web User Control. I know about
    UniqueID and ClientID but what I don't know is how get the ClientID from the
    string being passed in so I can use it in the AddAttributesToRender Sub (or
    should I be passing in a Control instead).

    Mucho head scratching.

    TIA,
    Stuart.


    Stuart Whiteford Guest

  2. Similar Questions and Discussions

    1. custom web control +client-side javascript + postback
      Hello, I am trying to create a new Web Control (ASP.NET 1.1) that contains among other textboxes. The content of these textboxes (runat=server)...
    2. Can client-side script be included in a user control (ascx)?
      I am creating a simple usercontrol (.ascx). There is some client-side functionality I would like it to have. I tried adding in a Script element,...
    3. how can i add client side javascript to a web user control?
      hi, i have a web user control that i wish to add some client-side javascript to. something like this: onChange="javascript:DoSomething();" ...
    4. validation summary doesnt display when there's client-side validation
      I have a custom validator that validates a numeric field, txtField, that allows for thousand separators. I also placed a validation summary so...
    5. only custom validation control does server side validation?
      On a CustomValidator you have to provide the validation code because otherwise it doesn't know what to do for the validation. Other validator...
  3. #2

    Default Re: Custom Client-Side Validation in Web User Control

    When providing a control's ID to the client side, always use the ClientID
    property as it is the value represented in the id= attribute of the HTML
    generated. Use FindControl() to get the control itself.

    Dim vOtherControl =
    Me.NamingContainer.FindControl(Me.SecondControlToV alidate) ' this will
    throw an exception if not found. So use Try..Catch
    writer.AddAttribute("secondcontroltovalidate", vOtherControl.ClientID)

    --- Peter Blum
    [url]www.PeterBlum.com[/url]
    Email: [email]PLBlum@PeterBlum.com[/email]
    Creator of "Professional Validation And More" at
    [url]http://www.peterblum.com/vam/home.aspx[/url]

    "Stuart Whiteford" <stuart-nospam-whiteford@firenet.uk.com> wrote in message
    news:uECBI%23V%23DHA.2180@TK2MSFTNGP09.phx.gbl...
    > Guys,
    >
    > I have a custom validator that has the default property ControlToValidate
    > plus one called SecondControlToValidate that I have created, like so...
    >
    > <ows:eitherorvalidator id="eovNumberName" runat="server"
    > ControlToValidate="txtNumber" SecondControlToValidate="txtName"
    > EnableClientScript="True"></ows:eitherorvalidator>
    >
    > ...in my Validator Class I have the following...
    >
    > Protected eitherOrSecondControlToValidate As String
    >
    > Public Property SecondControlToValidate() As String
    > Get
    > Return eitherOrSecondControlToValidate
    > End Get
    > Set(ByVal Value As String)
    > eitherOrSecondControlToValidate = Value
    > End Set
    > End Property
    >
    > Protected Overrides Sub AddAttributesToRender(ByVal writer As
    > HtmlTextWriter)
    > If (Me.DetermineRenderUplevel And Me.EnableClientScript) Then
    > MyBase.AddAttributesToRender(writer)
    > writer.AddAttribute("secondcontroltovalidate",
    > Me.SecondControlToValidate)
    > End If
    > End Sub
    >
    > All this works fine when the textboxes and validator is straight on the
    > page, but I can't get it working in a Web User Control. I know about
    > UniqueID and ClientID but what I don't know is how get the ClientID from
    the
    > string being passed in so I can use it in the AddAttributesToRender Sub
    (or
    > should I be passing in a Control instead).
    >
    > Mucho head scratching.
    >
    > TIA,
    > Stuart.
    >
    >

    Peter Blum Guest

  4. #3

    Default Re: Custom Client-Side Validation in Web User Control

    Peter,

    Managed to get this working.

    Thanks,
    Stuart.


    "Peter Blum" <PLBlum@Blum.info> wrote in message
    news:eljXjmm%23DHA.2512@TK2MSFTNGP11.phx.gbl...
    > When providing a control's ID to the client side, always use the ClientID
    > property as it is the value represented in the id= attribute of the HTML
    > generated. Use FindControl() to get the control itself.
    >
    > Dim vOtherControl =
    > Me.NamingContainer.FindControl(Me.SecondControlToV alidate) ' this will
    > throw an exception if not found. So use Try..Catch
    > writer.AddAttribute("secondcontroltovalidate", vOtherControl.ClientID)
    >
    > --- Peter Blum
    > [url]www.PeterBlum.com[/url]
    > Email: [email]PLBlum@PeterBlum.com[/email]
    > Creator of "Professional Validation And More" at
    > [url]http://www.peterblum.com/vam/home.aspx[/url]
    >
    > "Stuart Whiteford" <stuart-nospam-whiteford@firenet.uk.com> wrote in
    message
    > news:uECBI%23V%23DHA.2180@TK2MSFTNGP09.phx.gbl...
    > > Guys,
    > >
    > > I have a custom validator that has the default property
    ControlToValidate
    > > plus one called SecondControlToValidate that I have created, like so...
    > >
    > > <ows:eitherorvalidator id="eovNumberName" runat="server"
    > > ControlToValidate="txtNumber" SecondControlToValidate="txtName"
    > > EnableClientScript="True"></ows:eitherorvalidator>
    > >
    > > ...in my Validator Class I have the following...
    > >
    > > Protected eitherOrSecondControlToValidate As String
    > >
    > > Public Property SecondControlToValidate() As String
    > > Get
    > > Return eitherOrSecondControlToValidate
    > > End Get
    > > Set(ByVal Value As String)
    > > eitherOrSecondControlToValidate = Value
    > > End Set
    > > End Property
    > >
    > > Protected Overrides Sub AddAttributesToRender(ByVal writer As
    > > HtmlTextWriter)
    > > If (Me.DetermineRenderUplevel And Me.EnableClientScript) Then
    > > MyBase.AddAttributesToRender(writer)
    > > writer.AddAttribute("secondcontroltovalidate",
    > > Me.SecondControlToValidate)
    > > End If
    > > End Sub
    > >
    > > All this works fine when the textboxes and validator is straight on the
    > > page, but I can't get it working in a Web User Control. I know about
    > > UniqueID and ClientID but what I don't know is how get the ClientID from
    > the
    > > string being passed in so I can use it in the AddAttributesToRender Sub
    > (or
    > > should I be passing in a Control instead).
    > >
    > > Mucho head scratching.
    > >
    > > TIA,
    > > Stuart.
    > >
    > >
    >
    >

    Stuart Whiteford Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139