This is a migrated thread and some comments may be shown as answers.

Javascript validation code not running in ascx usercontrol

3 Answers 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
AkAlan
Top achievements
Rank 2
AkAlan asked on 07 Jan 2010, 07:40 PM
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>

3 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 12 Jan 2010, 07:53 AM
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.
0
AkAlan
Top achievements
Rank 2
answered on 12 Jan 2010, 04:18 PM

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

0
Martin
Telerik team
answered on 13 Jan 2010, 12:43 PM
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.
Tags
Grid
Asked by
AkAlan
Top achievements
Rank 2
Answers by
Martin
Telerik team
AkAlan
Top achievements
Rank 2
Share this question
or