如何在客户端javascript验证后触发服务器端事件 [英] How to fire server side event after client side javascript validation

查看:73
本文介绍了如何在客户端javascript验证后触发服务器端事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net页面,我在其中放置了一个按钮控件,其中包含OnClientClick和OnClick事件,如下所示:

I have a asp.net page on which I have placed a Button control which has OnClientClick and OnClick events as follows:

<asp:Button Text="Activate" ID="btnActivate"  OnClientClick="return ValidateActivation()"                                                                     OnClick="btnActivate_Click" runat="server" />





在OnClientClick上触发的javascript函数显示一个警告,如果验证失败则返回false。



此外,如果验证失败,则在OnClientClick上触发的javascript函数返回true。所以在这种情况下,我希望触发服务器端事件。但服务器端事件没有被触发。



这是javascript代码供您参考:



The javascript function fired on OnClientClick displays an alert and returns false if the validation fails.

Also, the javascript function fired on OnClientClick returns true if the validation fails. So in this case I expect the server side event to be fired. But the server side event is not fired.

Here is the javascript code for your reference:

function ValidateActivation() {
                var selected = false;
                $('#<%=(gvParent.ClientID%>').find("input:checkbox").each(function () {
                    if (this.checked == true) { selected = true; }
                });
                if (selected == false) {
                    var oWnd = alert("Select at least one Division/Sub-Division to activate.");
                    return false;
                }
                else {
                    return true;
                }
            }





基本上这是一个javascript,检查应该在gvParent中检查至少一个复选框Gridview。



警报条件完美无缺。但页面不回发,即使选中复选框,也不会触发事件btnActivate_Click。



我已经确认控件进入声明的'else'部分。



任何建议,替代方案?





编辑:



这是我的页面结构





Basically this is a javascript which checks that atleast one checkbox should be checked in the gvParent Gridview.

The alert condition works perfectly. But the page does not postback and the event btnActivate_Click is not fired even if the check box is checked.

I have verified that the control enters in the 'else' part of the statement.

Any suggestions, alternatives ?




This is my page structure

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">	
			<telerik:RadScriptBlock ID="RadScriptBlock1"  runat="server">
        <script type="text/javascript" language="javascript">		
		.......my script here ...
		</script>
		</telerik:RadScriptBlock>
		</Content>
		
		
		<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" runat="server">
    <telerik:RadScriptManager ID="radScriptManager1"  runat="server">
    </telerik:RadScriptManager>
	
	
	<telerik:RadAjaxLoadingPanel ID="lp1"  runat="server" Skin="Outlook">
            </telerik:RadAjaxLoadingPanel>
            <telerik:RadAjaxPanel ID="ap1"  runat="server" Width="100%"

                LoadingPanelID="lpID1">
              
                        <telerik:RadPanelBar ClientIDMode="Static" ID="myid"  runat="server"

                            OnClientItemClicking="onClicking" ExpandMode="SingleExpandedItem"  önClientItemExpand="scrollToTop"

                            Skin="Windows7" Width="100%">
                            <Items>
                                <telerik:RadPanelItem  runat="server" CssClass="new" Expanded="true" Text="my text"

                                    Value="head1">
                                    <Items>
                                        <telerik:RadPanelItem  runat="server" Value="info">
                                            <ItemTemplate>
											
											<asp:GridView ID="gvParent" runat="server"...

推荐答案

' #<%=(gvParent.ClientID%> ;')。find( input:checkbox)。each( function (){
if this .checked == true ){selected = true ; }
});
if (选择== false ){
var oWnd = alert( 选择至少一个要激活的分部/子部门。 );
return false ;
}
其他 {
return ;
}
}
('#<%=(gvParent.ClientID%>').find("input:checkbox").each(function () { if (this.checked == true) { selected = true; } }); if (selected == false) { var oWnd = alert("Select at least one Division/Sub-Division to activate."); return false; } else { return true; } }





基本上这是一个javascript,检查应该在gvParent中检查至少一个复选框Gridview。



警报条件完美无缺。但页面不回发,即使选中复选框,也不会触发事件btnActivate_Click。



我已经确认控件进入声明的'else'部分。



任何建议,替代方案?





编辑:



这是我的页面结构





Basically this is a javascript which checks that atleast one checkbox should be checked in the gvParent Gridview.

The alert condition works perfectly. But the page does not postback and the event btnActivate_Click is not fired even if the check box is checked.

I have verified that the control enters in the 'else' part of the statement.

Any suggestions, alternatives ?




This is my page structure

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">	
			<telerik:RadScriptBlock ID="RadScriptBlock1"  runat="server">
        <script type="text/javascript" language="javascript">		
		.......my script here ...
		</script>
		</telerik:RadScriptBlock>
		</Content>
		
		
		<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" runat="server">
    <telerik:RadScriptManager ID="radScriptManager1"  runat="server">
    </telerik:RadScriptManager>
	
	
	<telerik:RadAjaxLoadingPanel ID="lp1"  runat="server" Skin="Outlook">
            </telerik:RadAjaxLoadingPanel>
            <telerik:RadAjaxPanel ID="ap1"  runat="server" Width="100%"

                LoadingPanelID="lpID1">
              
                        <telerik:RadPanelBar ClientIDMode="Static" ID="myid"  runat="server"

                            OnClientItemClicking="onClicking" ExpandMode="SingleExpandedItem"  önClientItemExpand="scrollToTop"

                            Skin="Windows7" Width="100%">
                            <Items>
                                <telerik:RadPanelItem  runat="server" CssClass="new" Expanded="true" Text="my text"

                                    Value="head1">
                                    <Items>
                                        <telerik:RadPanelItem  runat="server" Value="info">
                                            <ItemTemplate>
											
											<asp:GridView ID="gvParent" runat="server"...


<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">

        </Content>


        <asp:Content ID="Content2" ContentPlaceHolderID="mainContent" runat="server">
    <telerik:RadScriptManager ID="radScriptManager1"  runat="server">
    </telerik:RadScriptManager>


<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="ap1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="ap1" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>

    <telerik:RadAjaxLoadingPanel ID="lp1"  runat="server" Skin="Outlook">
            </telerik:RadAjaxLoadingPanel>
            <telerik:RadAjaxPanel ID="ap1"  runat="server" Width="100%"

                LoadingPanelID="lpID1">

              Place All your POstback controls here

        <telerik:RadScriptBlock ID="RadScriptBlock1"  runat="server">
        <script type="text/javascript" language="javascript">
        .......my script here ...
        </script>
        </telerik:RadScriptBlock>
         </telerik:RadAjaxPanel>


Hello,



If you really want to manually call __doPostBack(), the first parameter is the .NET generated name for the control. This can be retrieved on the server side using Control.ClientID. The second parameter is any extra data that should be passed along in the request. Most of the time I see this field is an empty string. So from your javascript you can postback by calling __doPostBack(’btnActivate’, ’’); from the else part.



You can find more information about postback here[^].



Regards,
Hello,

If you really want to manually call __doPostBack(), the first parameter is the .NET generated name for the control. This can be retrieved on the server side using Control.ClientID. The second parameter is any extra data that should be passed along in the request. Most of the time I see this field is an empty string. So from your javascript you can postback by calling __doPostBack('btnActivate', ''); from the else part.

You can find more information about postback here[^].

Regards,


这篇关于如何在客户端javascript验证后触发服务器端事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆