Client-side validation on user control

Discussion in 'ASP .Net' started by =?Utf-8?B?RGF2aWQgRGF2aXM=?=, May 10, 2006.

  1. I cannot get a web user control to validation client-side using a required
    field validator. It only validates server side.

    I have a web user control in .net 2.0. I have a single text box on it. I
    have a public Text property defined below:

    <ValidationProperty("Text")> _
    Partial Class MyWebUserControl
    Inherits System.Web.UI.UserControl

    Public Property Text() As String
    Get
    Return TextBox1.Text
    End Get
    Set(ByVal value As String)
    TextBox1.Text = value
    End Set
    End Property

    End Class

    I have a RequiredFieldValidator on the same web page as my user control. I
    put the user control's id in the ControlToValidate property of the validator
    (please note that I must type it in, it doesn't show up in the dropdown list).

    The control validates only server-side, not client-side. The html source
    for my page contains the following:

    <div>
    <input name="MyWebUserControl1$TextBox1" type="text"
    id="MyWebUserControl1_TextBox1" />

    <span id="RequiredFieldValidator1" style="color:Red;">The user
    control text is required.</span
    </div>
    ....
    <script type="text/javascript">
    <!--
    var RequiredFieldValidator1 = document.all ?
    document.all["RequiredFieldValidator1"] :
    document.getElementById("RequiredFieldValidator1");
    RequiredFieldValidator1.controltovalidate = "MyWebUserControl1";
    RequiredFieldValidator1.errormessage = "The user control text is required.";
    RequiredFieldValidator1.isvalid = "False";
    RequiredFieldValidator1.evaluationfunction =
    "RequiredFieldValidatorEvaluateIsValid";
    RequiredFieldValidator1.initialvalue = "";
    // -->
    </script>

    Notice that the id for the control is "MyWebUserControl1_TextBox1" and the
    javascript is just calling it "MyWebUserControl1". I'm not sure if this is a
    problem or not.

    Any suggestions? --David
     
    =?Utf-8?B?RGF2aWQgRGF2aXM=?=, May 10, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?RGF2aWQgRGF2aXM=?=

    MSDN Guest

    RequiredFieldValidator1.controltovalidate = TextBox1.ClientID() or
    "MyWebUserControl1_TextBox1";

    "David Davis" <> wrote in message
    news:...
    >I cannot get a web user control to validation client-side using a required
    > field validator. It only validates server side.
    >
    > I have a web user control in .net 2.0. I have a single text box on it. I
    > have a public Text property defined below:
    >
    > <ValidationProperty("Text")> _
    > Partial Class MyWebUserControl
    > Inherits System.Web.UI.UserControl
    >
    > Public Property Text() As String
    > Get
    > Return TextBox1.Text
    > End Get
    > Set(ByVal value As String)
    > TextBox1.Text = value
    > End Set
    > End Property
    >
    > End Class
    >
    > I have a RequiredFieldValidator on the same web page as my user control.
    > I
    > put the user control's id in the ControlToValidate property of the
    > validator
    > (please note that I must type it in, it doesn't show up in the dropdown
    > list).
    >
    > The control validates only server-side, not client-side. The html source
    > for my page contains the following:
    >
    > <div>
    > <input name="MyWebUserControl1$TextBox1" type="text"
    > id="MyWebUserControl1_TextBox1" />
    >
    > <span id="RequiredFieldValidator1" style="color:Red;">The user
    > control text is required.</span
    > </div>
    > ...
    > <script type="text/javascript">
    > <!--
    > var RequiredFieldValidator1 = document.all ?
    > document.all["RequiredFieldValidator1"] :
    > document.getElementById("RequiredFieldValidator1");
    > RequiredFieldValidator1.controltovalidate = "MyWebUserControl1";
    > RequiredFieldValidator1.errormessage = "The user control text is
    > required.";
    > RequiredFieldValidator1.isvalid = "False";
    > RequiredFieldValidator1.evaluationfunction =
    > "RequiredFieldValidatorEvaluateIsValid";
    > RequiredFieldValidator1.initialvalue = "";
    > // -->
    > </script>
    >
    > Notice that the id for the control is "MyWebUserControl1_TextBox1" and the
    > javascript is just calling it "MyWebUserControl1". I'm not sure if this
    > is a
    > problem or not.
    >
    > Any suggestions? --David
     
    MSDN, May 10, 2006
    #2
    1. Advertising

  3. I tried this already and I get a "Unable to find control id
    'WebUserControl1_TextBox1' referenced by the 'ControlToValidate' property of
    'RequiredFieldValidator1'" error, even when I expose the textbox in my user
    control as a property.

    "MSDN" wrote:

    >
    > RequiredFieldValidator1.controltovalidate = TextBox1.ClientID() or
    > "MyWebUserControl1_TextBox1";
    >
    > "David Davis" <> wrote in message
    > news:...
    > >I cannot get a web user control to validation client-side using a required
    > > field validator. It only validates server side.
    > >
    > > I have a web user control in .net 2.0. I have a single text box on it. I
    > > have a public Text property defined below:
    > >
    > > <ValidationProperty("Text")> _
    > > Partial Class MyWebUserControl
    > > Inherits System.Web.UI.UserControl
    > >
    > > Public Property Text() As String
    > > Get
    > > Return TextBox1.Text
    > > End Get
    > > Set(ByVal value As String)
    > > TextBox1.Text = value
    > > End Set
    > > End Property
    > >
    > > End Class
    > >
    > > I have a RequiredFieldValidator on the same web page as my user control.
    > > I
    > > put the user control's id in the ControlToValidate property of the
    > > validator
    > > (please note that I must type it in, it doesn't show up in the dropdown
    > > list).
    > >
    > > The control validates only server-side, not client-side. The html source
    > > for my page contains the following:
    > >
    > > <div>
    > > <input name="MyWebUserControl1$TextBox1" type="text"
    > > id="MyWebUserControl1_TextBox1" />
    > >
    > > <span id="RequiredFieldValidator1" style="color:Red;">The user
    > > control text is required.</span
    > > </div>
    > > ...
    > > <script type="text/javascript">
    > > <!--
    > > var RequiredFieldValidator1 = document.all ?
    > > document.all["RequiredFieldValidator1"] :
    > > document.getElementById("RequiredFieldValidator1");
    > > RequiredFieldValidator1.controltovalidate = "MyWebUserControl1";
    > > RequiredFieldValidator1.errormessage = "The user control text is
    > > required.";
    > > RequiredFieldValidator1.isvalid = "False";
    > > RequiredFieldValidator1.evaluationfunction =
    > > "RequiredFieldValidatorEvaluateIsValid";
    > > RequiredFieldValidator1.initialvalue = "";
    > > // -->
    > > </script>
    > >
    > > Notice that the id for the control is "MyWebUserControl1_TextBox1" and the
    > > javascript is just calling it "MyWebUserControl1". I'm not sure if this
    > > is a
    > > problem or not.
    > >
    > > Any suggestions? --David

    >
    >
    >
     
    =?Utf-8?B?RGF2aWQgRGF2aXM=?=, May 11, 2006
    #3
  4. =?Utf-8?B?RGF2aWQgRGF2aXM=?=

    Parag Guest

    Hi,
    You can solve the problem by setting the name property for the textbox and
    then using document.getelementbyname function in JavaScript should be able
    to get the control into some variable. Once you get hold of the control on
    the client-side then application of any validation function shouldn't
    present you any problem.

    Regards
    Parag Kulkarni
    MTS
    Persistent Systems Private Limited




    "David Davis" <> wrote in message
    news:...
    >I tried this already and I get a "Unable to find control id
    > 'WebUserControl1_TextBox1' referenced by the 'ControlToValidate' property
    > of
    > 'RequiredFieldValidator1'" error, even when I expose the textbox in my
    > user
    > control as a property.
    >
    > "MSDN" wrote:
    >
    >>
    >> RequiredFieldValidator1.controltovalidate = TextBox1.ClientID() or
    >> "MyWebUserControl1_TextBox1";
    >>
    >> "David Davis" <> wrote in message
    >> news:...
    >> >I cannot get a web user control to validation client-side using a
    >> >required
    >> > field validator. It only validates server side.
    >> >
    >> > I have a web user control in .net 2.0. I have a single text box on it.
    >> > I
    >> > have a public Text property defined below:
    >> >
    >> > <ValidationProperty("Text")> _
    >> > Partial Class MyWebUserControl
    >> > Inherits System.Web.UI.UserControl
    >> >
    >> > Public Property Text() As String
    >> > Get
    >> > Return TextBox1.Text
    >> > End Get
    >> > Set(ByVal value As String)
    >> > TextBox1.Text = value
    >> > End Set
    >> > End Property
    >> >
    >> > End Class
    >> >
    >> > I have a RequiredFieldValidator on the same web page as my user
    >> > control.
    >> > I
    >> > put the user control's id in the ControlToValidate property of the
    >> > validator
    >> > (please note that I must type it in, it doesn't show up in the dropdown
    >> > list).
    >> >
    >> > The control validates only server-side, not client-side. The html
    >> > source
    >> > for my page contains the following:
    >> >
    >> > <div>
    >> > <input name="MyWebUserControl1$TextBox1" type="text"
    >> > id="MyWebUserControl1_TextBox1" />
    >> >
    >> > <span id="RequiredFieldValidator1" style="color:Red;">The user
    >> > control text is required.</span
    >> > </div>
    >> > ...
    >> > <script type="text/javascript">
    >> > <!--
    >> > var RequiredFieldValidator1 = document.all ?
    >> > document.all["RequiredFieldValidator1"] :
    >> > document.getElementById("RequiredFieldValidator1");
    >> > RequiredFieldValidator1.controltovalidate = "MyWebUserControl1";
    >> > RequiredFieldValidator1.errormessage = "The user control text is
    >> > required.";
    >> > RequiredFieldValidator1.isvalid = "False";
    >> > RequiredFieldValidator1.evaluationfunction =
    >> > "RequiredFieldValidatorEvaluateIsValid";
    >> > RequiredFieldValidator1.initialvalue = "";
    >> > // -->
    >> > </script>
    >> >
    >> > Notice that the id for the control is "MyWebUserControl1_TextBox1" and
    >> > the
    >> > javascript is just calling it "MyWebUserControl1". I'm not sure if
    >> > this
    >> > is a
    >> > problem or not.
    >> >
    >> > Any suggestions? --David

    >>
    >>
    >>
     
    Parag, May 11, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    14
    Views:
    4,211
    Chad Z. Hower aka Kudzu
    Jan 30, 2004
  2. =?Utf-8?B?dmlkeWE=?=
    Replies:
    1
    Views:
    781
    Kevin Spencer
    Jun 2, 2005
  3. Boss302
    Replies:
    0
    Views:
    1,098
    Boss302
    Nov 21, 2006
  4. Bogdan
    Replies:
    2
    Views:
    686
    Bogdan
    Jun 9, 2008
  5. Stuart Whiteford

    Custom Client-Side Validation in Web User Control

    Stuart Whiteford, Feb 22, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    274
    Stuart Whiteford
    Feb 26, 2004
Loading...

Share This Page