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

How to Handle ASP.NET Validation with RadAjaxManager onkeypress client side event

5 Answers 201 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
skobyjay
Top achievements
Rank 1
skobyjay asked on 10 Mar 2011, 05:53 AM

Requirements

RadControls 2010.3.1317.35version

.NET version 3.5

Visual Studio version 2010

programming language C#

browser support

all browsers supported by RadControls


PROJECT DESCRIPTION
This project demonstrates how to handle ASP.NET Form Validation with the RadAjaxManager when fired from the onkeypress event from a textbox and perform an async postback if the validation succeeded.

<head runat="server">
    <title></title>
    <telerik:RadCodeBlock runat="server" ID="radCodeBlock">
        <script type="text/javascript">
            function CheckEnterKey(sender, args) {
                var keyCode = args.get_keyCode();
                if (keyCode == 13) {
                    Page_ClientValidate("Search");
                    if (Page_IsValid) {
                        var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>");
                        ajaxManager.ajaxRequestWithTarget('<%= btnSearch.UniqueID %>', '');
                    }
                }
            }
        </script>
    </telerik:RadCodeBlock>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="radScriptMgr">
    </telerik:RadScriptManager>
    <telerik:RadInputManager ID="RadInputManager1"  runat="server">       
        <telerik:RegExpTextBoxSetting BehaviorID="Setting3" EmptyMessage="Enter Zip Code" ClientEvents-OnKeyPress="CheckEnterKey"
            ValidationExpression="\d{5}(-\d{4})?" ErrorMessage="Invalid Zip Code" Validation-IsRequired="true" Validation-ValidationGroup="Search">
                <TargetControls>
                    <telerik:TargetInput ControlID="txtZipCode" />
                </TargetControls>
            </telerik:RegExpTextBoxSetting
    </telerik:RadInputManager>
    <telerik:RadAjaxLoadingPanel Skin="Default" runat="server" ID="pnlLoadingPanel">
    </telerik:RadAjaxLoadingPanel>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="btnSearch">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="pnlSearch" LoadingPanelID="pnlLoadingPanel" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <div style="text-align: center; margin: 0 auto;">
     <asp:TextBox ID="txtZipCode" ValidationGroup="Search" runat="server" MaxLength="5" Width="133px" Height="20px"></asp:TextBox>
      <telerik:RadButton runat="server" ID="btnSearch" Text="Search" ValidationGroup="Search">
       <Icon PrimaryIconCssClass="rbSearch" PrimaryIconLeft="4" PrimaryIconTop="4" />
      </telerik:RadButton>      
        <br /><br />
        <asp:Panel ID="pnlSearch" Height="100%" DefaultButton="btnSearch" Width="100%" runat="server">
            <asp:Literal ID="litTimeOfSearch" runat="server"></asp:Literal><br /><br /><br /><br />
        </asp:Panel>
    </div>
    </form>
</body>
</html>

After this you can use some event handling to perform the necessary action postback.

protected void Page_Load(object sender, EventArgs e)
 {
 
     this.btnSearch.Click += new EventHandler(btnSearch_Click);
 
 }
 
 void btnSearch_Click(object sender, EventArgs e)
 {
     Thread.Sleep(2000);
     litTimeOfSearch.Text = string.Format("<b>Last Search Executed at</b>: {0} <br/> <b>Zip code </b> : {1}",  DateTime.Now.ToShortTimeString()
         ,txtZipCode.Text);
 }

5 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 15 Mar 2011, 02:46 PM
Hello skobyjay,

Thank you for sharing your experience with the community.
However I added the provided code, and observed that it does not work in all browsers. That is why I converted the thread to a forum one.
Can you check it out and let me know if I missed something out?

Greetings,
Iana
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
skobyjay
Top achievements
Rank 1
answered on 15 Mar 2011, 07:13 PM
Hi there,

can you provide a description what browser(s) that the code does not perform in and what the problem is? I would like to be able to fix this so that I can get this submitted as a code example for the community.

Thank you,

Bobby
0
Iana Tsolova
Telerik team
answered on 16 Mar 2011, 02:49 PM
Hi skobyjay,

In Firefox the the postback is not prevented.
I would also ask you to confirm the sample works in IE. You can also check how it goes in the other browsers of the list below:
http://www.telerik.com/products/aspnet-ajax/getting-started/browser-support.aspx

Kind regards,
Iana
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
skobyjay
Top achievements
Rank 1
answered on 14 Jul 2011, 04:47 AM
Hi Iana,

Sorry for that late response. I'm confused by what you mean "In Firefox the the postback is not prevented". I have tested in all browsers and the results I expect are happening.

I'm providing a solution that allows you to submit an ajax request via client script registering the onkeydown of the Enter key. Is this what you are attempting?
0
Iana Tsolova
Telerik team
answered on 20 Jul 2011, 09:28 AM
Hi Skobyjay,

I tested the sample in a few browsers and you can see how it behaves on my side in this video. Let me know if this is the expected behavior and if I missed something when adding the solution to the runnable sample.

Regards,
Iana
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
Ajax
Asked by
skobyjay
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
skobyjay
Top achievements
Rank 1
Share this question
or