Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a asp.net page on which I have placed a Button control which has OnClientClick and OnClick events as follows:
ASP.NET
<asp:Button Text="Activate" ID="btnActivate"  OnClientClick="return ValidateActivation()"                                                                     OnClick="btnActivate_Click" runat="server" />


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:
JavaScript
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;
                }
            }


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 ?


Edit:

This is my page structure

ASP.NET
<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"...
Posted
Updated 24-Jul-13 5:39am
v2
Comments
bbirajdar 24-Jul-13 11:12am    
I also tried adding __doPostBack('btnActivate_Click', "OnClick"); in the 'else' part.

Also tried with __doPostBack('<%=btnActivate.ClientID%>', "OnClick");
woopsydoozy 24-Jul-13 11:15am    
What's the context of your button? Is it in something like an UpdatePanel?
bbirajdar 24-Jul-13 11:19am    
Oh yes.. Not exactly an UpdatePanel . But its inside a RadAjaxPanel .. Is that something affecting the postback ?

XML
<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>
 
Share this answer
 
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,
 
Share this answer
 
It seems no problem with the code.
i've tested @my end with the following script modified.

XML
<script>
 function ValidateActivation() {
             var selected = true;
             if (selected == false) {
                 alert(selected);
                 return false;
             }
             else {
                   alert(selected);
                 return true;
             }
         }
 </script>


if variable selected is set to 'true' the button's server side event is firing fine.
 
Share this answer
 
Comments
bbirajdar 24-Jul-13 11:21am    
Yes. This is what I was thinking. Actually I posted only the relevant code. Now somebody above has asked me whether the btnActivate is inside an UpdatePanel.. I guess this is affecting the postback...
vinay.tatipamula 24-Jul-13 11:25am    
Oh, then suggest you to keep the script just before the body end tag, if the script is in 'head' tag and check..!
bbirajdar 24-Jul-13 11:27am    
Okay..Let me try
bbirajdar 24-Jul-13 11:34am    
I placed the javascript at the end of the page inside the body tag. But it still fails to do a postback..
vinay.tatipamula 24-Jul-13 11:33am    
along with the above suggestion, i guess u have the following structure.

<telerik:RadScriptManager ID="RadScriptManager1" runat="server">

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<ajaxsettings>
<telerik:ajaxsetting ajaxcontrolid="RadAjaxPanel">
<updatedcontrols>
<telerik:ajaxupdatedcontrol controlid="RadAjaxPanel">




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

Your button

Your Grid

etc....


</form>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900