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

ajax radgrid and javascript

1 Answer 58 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arve
Top achievements
Rank 2
Arve asked on 17 Nov 2008, 03:39 PM
RadAjaxPanel makes trouble for client-side validation - written in javascript.

I have a RadGrid wich looks like this:

 

<telerik:RadAjaxPanel ID="rap" runat="server" >

 

 

<telerik:RadGrid

 

 

Skin="WebBlue"

 

 

ID="RadGrid1"

 

 

runat="server"

 

 

GridLines="None"

 

 

AllowPaging="True"

 

 

AllowSorting="True"

 

 

Width="97%"

 

 

AutoGenerateColumns="False"

 

 

EnableAJAX="true"

 

 

ShowStatusBar="true"

 

 

>

 

 

<MasterTableView GridLines="None" Width="100%" CommandItemDisplay="Top" >

 

 

<Columns>

 

 

<telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />

 

 

<telerik:GridBoundColumn Display="false" HeaderText="Phone" DataField="Phone" />

 

 

<telerik:GridBoundColumn Display="false" HeaderText="MobilePhone %>" DataField="MobilePhone" />

 

 

<telerik:GridBoundColumn Display="false" HeaderText="Email " DataField="Email" />

 

 

<telerik:GridButtonColumn UniqueName="DeleteColumn" CommandName="Delete" Text="Delete" />

 

 

</Columns>

 

 

<EditFormSettings UserControlName="DependentPersonDossiersEditForm.ascx" EditFormType="WebUserControl">

 

 

<EditColumn UniqueName="EditCommandColumn1" />

 

 

</EditFormSettings>

 

 

</MasterTableView>

 

 

</telerik:RadGrid>

 

 

</telerik:RadAjaxPanel>

I use a usercontrol for edits, inserts:

 

...
<

div>

 

 

 

 

<table frame="void">

 

 

 

 

<tr>

 

 

 

 

<td>

 

 

 

 

<div>

 

 

 

 

<wwui:Field ID="uiPhoneField" runat="server" LabelFor="uiPhone" Label="Phone">

 

 

 

 

<telerik:RadTextBox ID="uiPhone" runat="server" />

 

 

 

 

<asp:CustomValidator

 

 

 

 

ID="uiPhone_cv"

 

 

 

 

runat="server"

 

 

 

 

ErrorMessage="*"

 

 

 

 

Text="*"

 

 

 

 

ControlToValidate="uiPhone"

 

 

 

 

Display="Static"

 

 

OnServerValidate="uiPhone_cv_ServerValidate"

 

 

 

 

/>

 

 

 

 

</wwui:Field>

 

 

 

 

<wwui:Field ID="uiMobilePhoneField" runat="server" LabelFor="uiMobilePhone" Label="MobilePhone">

 

 

 

 

<telerik:RadTextBox ID="uiMobilePhone" runat="server" />

 

 

 

 

<asp:CustomValidator

 

 

 

 

ID="uiMobilePhone_cv"

 

 

 

 

runat="server"

 

 

 

 

ErrorMessage="*"

 

 

 

 

Text="*"

 

 

 

 

ControlToValidate="uiMobilePhone"

 

 

 

 

Display="Static"

 

 

 

 

/>

 

 

 

 

</wwui:Field>

 

 

 

 

<wwui:Field ID="uiEmailField" runat="server" LabelFor="uiEmail" Label="Email">

 

 

 

 

<telerik:RadTextBox ID="uiEmail" runat="server" />

 

 

 

 

<asp:CustomValidator

 

 

 

 

ID="uiEmail_cv"

 

 

 

 

runat="server"

 

 

 

 

ErrorMessage="*"

 

 

 

 

ControlToValidate="uiEmail"

 

 

 

 

Display="Static"

 

 

 

 

/>

 

 

 

 

</wwui:Field>

 

 

 

 

</div>

 

 

 

 

</td>

 

 

 

 

</tr>

 

 

 

 

<tr>

 

 

 

 

<td>

 

 

 

 

<asp:Button ID="btnUpdate" ValidationGroup="DependentPersonDossiersGrid" BackColor="Red" text="Confirm Edit" runat="server" CommandName="Update" Visible="true"></asp:Button>

 

 

 

 

<asp:Button ID="btnInsert" ValidationGroup="Grid" text="Perform Insert" runat="server" CommandName="PerformInsert" Visible="false"></asp:Button>

 

 

 

 

&nbsp;

 

 

 

 

<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="false" CommandName="Cancel"></asp:Button>

 

 

 

 

</td>

 

 

 

 

</tr>

 

 

 

 

</table>

 

 

 

 

</div>
...

In the usercontrols codebehind I register javascripts like this:

 

 

ClientScriptManager csm = Page.ClientScript;

 

 

if ( !csm.IsClientScriptBlockRegistered ( "PhoneFormatValidate" ) )

 

{

 

StringBuilder sbphone = new StringBuilder ( 512 );

 

sbphone.Append (

" <script type='text/javascript' language='javascript'>" );

 

sbphone.Append (

" function PhoneFormatValidate(source, clientside_args) " );

 

sbphone.Append (

" { " );

 

sbphone.Append (

" var phoneStr; " );

 

sbphone.Append (

" phoneStr = clientside_args.Value;" );

 

sbphone.Append (

" phoneStr = phoneStr.replace(/\\s+/g, '');" );

 

sbphone.Append (

" clientside_args.IsValid = true; " );

 

sbphone.Append (

" if (phoneStr.length > 0) " );

 

sbphone.Append (

" { " );

 

sbphone.Append (

" if (!phoneStr.match(/^((\\+(([0-9]){2})\\(([0-9]){2}\\)([0-9]){6})|(([0-9]){8}))$/)) " );

 

sbphone.Append (

" clientside_args.IsValid = false; " );

 

sbphone.Append (

" } " );

 

sbphone.Append (

" } " );

 

sbphone.Append (

" </script>" );

 

 

 

 

csm.RegisterClientScriptBlock (

typeof ( CommunicationType ), "PhoneFormatValidate", sbphone.ToString ( ) );

 

}

 

if ( !csm.IsClientScriptBlockRegistered ( uiEmail.ClientID ) )

 

{

 

StringBuilder sb = new StringBuilder ( 512 );

 

sb.Append (

" <script type='text/javascript' language='javascript'>" );

 

sb.Append (

" function " + uiEmail.ClientID + "_EmailFormatValidate(source, clientside_args) " );

 

sb.Append (

" { " );

 

sb.Append (

" var emailStr; " );

 

sb.Append (

" emailStr = (document.getElementById('" + uiEmail.ClientID + "')).value;" ); //clientside_args.Value;" );

 

 

 

 

sb.Append (

" clientside_args.IsValid = true; " );

 

sb.Append (

" emailStr = emailStr.replace(' ','');" );

 

sb.Append (

" if (emailStr.replace(/\\s+/g, '').length > 0) " );

 

sb.Append (

" { " );

 

sb.Append (

" if (!emailStr.match(/^((mailto:)?([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+)$/)) " );

 

sb.Append (

" clientside_args.IsValid = false; " );

 

sb.Append (

" } " );

 

sb.Append (

" } " );

 

sb.Append (

" </script>" );

 

 

 

 

csm.RegisterClientScriptBlock (

typeof ( EMailType ), uiEmail.ClientID, sb.ToString ( ) );

 

}

uiPhone_cv.ClientValidationFunction =

"PhoneFormatValidate";

 

uiMobilePhone_cv.ClientValidationFunction =

"PhoneFormatValidate";

 

uiEmail_cv.ClientValidationFunction = uiEmail.ClientID +

"_EmailFormatValidate";

This works fine without the ajaxpanel.
But when I wrap the grid in an ajaxpanel I get a javascript-error (Error: Object expected) when the validation is supposed to fire.
What is the right way to register javascript wich depends on known the clientid?
In context of ajax enabled.

Mvh.

Arve

 

1 Answer, 1 is accepted

Sort by
0
Maria Ilieva
Telerik team
answered on 19 Nov 2008, 11:24 AM
Hello Arve,

Please try using the ResponseScripts collection of RadAjaxPanel. More information for this matter could be found in the following online resources:

ResponseScripts topic



ResponseScripts example

Best wishes,
Maria Ilieva
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Arve
Top achievements
Rank 2
Answers by
Maria Ilieva
Telerik team
Share this question
or