Javascript validation code not running in ascx usercontrol

4 posts, 0 answers
  1. AkAlan
    AkAlan avatar
    121 posts
    Member since:
    Jun 2009

    Posted 07 Jan 2010 Link to this post

    I have a RadGrid with a User Control (.ascx page) used as a pop up Insert/Edit form. I want to do some custom validation on the form but the javascript never runs. I looked at the source code of the page after it loaded and the script I wrote isn't even there. How can I make this work?

    <script language="javascript" type="text/javascript">  
        function ValidateNumber(txt) {  
            alert("Entries must be numeric!");  
            } 
    <asp:TextBox ID="txtHours4" onchange="ValidateNumber(this)"  runat="server"></asp:TextBox>
  2. Martin
    Admin
    Martin avatar
    811 posts

    Posted 12 Jan 2010 Link to this post

    Hello Alan,

    I am afraid that I am not able to reproduce the issue. Here is the code I tried:

    ASPX:

    <telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource"
            AutoGenerateEditColumn="true">
            <MasterTableView AutoGenerateColumns="False" EditMode="PopUp" CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridBoundColumn DataField="MyField" HeaderText="Bound column" />
                </Columns>
                <EditFormSettings EditFormType="WebUserControl" UserControlName="MyUserControl.ascx">
                </EditFormSettings>
            </MasterTableView>
        </telerik:RadGrid>

    ASPX.CS:

    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("MyField");
            for (int i = 0; i < 5; i++)
            {
                table.Rows.Add(i);
            }
            RadGrid1.DataSource = table;
        }

    ASCX:

    <script type="text/javascript">
            function ValidateNumber(tb)
            {
                alert(tb.value);
            }
    </script>
     
    <asp:TextBox ID="txtHours4" onchange="ValidateNumber(this)" runat="server"></asp:TextBox>

    The above code run as expected on my side and I suggest you to give it a try.

    Hope this helps,
    Martin
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. AkAlan
    AkAlan avatar
    121 posts
    Member since:
    Jun 2009

    Posted 12 Jan 2010 Link to this post

    Thanks Martin with the help of your post I was able to figure out why I was getting an Object Required error. I had the control nested in <Div> tags. Once I removed them it worked (although there goes my pretty form structure). I have a support ticket open (Support ID:272326) on  a similar matter that I think will really help me out with the div tags. Looks like this is what Veli is suggesting:

     

    RadScriptManager.RegisterArrayDeclaration(Page, "textBoxIds", "'" + txtLabourHours1.ClientID + "'")

     

    If you think there is something else I need to do to solve the nested div situation I would like to hear from you. Thanks again.

     

     

    Alan

  5. Martin
    Admin
    Martin avatar
    811 posts

    Posted 13 Jan 2010 Link to this post

    Hello Alan,

    I think that the post of my colleague Veli was pretty descriptive so I just have nothing to add. Please follow the approach recommended by him and let us know if further assistance is needed.

    Greetings,
    Martin
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top