Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a button created on a form. When I click on the button a "div" panel is created. This button is also executing its onclick event.

My problem is, the onclick event is not getting triggered/fired from the function ShowPanel()

Here is the code as below:

ASP.NET Page:

XML
<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
    <script type="text/javascript">
        function ShowPanel() {
                $('#btnSubmit').trigger('click'); -- Calling the btnSubmit_Click event
                $('#divcontrol').toggle("slow");

                return false;
            }
    </script>


----Creating the small div panel and submit button---
XML
<div id="divcontrol" style="background-color: Green; border: 2 solid red;width:300px;height:100px;color:White;font-size:30px; display :none">
    </div>

----Submit button------
   <asp:Button id="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return ShowPanel();
return false;" OnClick="btnSubmit_Click"/>


-------------C# Onclick event for Submit Button-------
C#
protected void btnSubmit_Click(object sender, EventArgs e)
       {
             GetDetails();
       }


The "div" panel is created when the button is clicked, but the onclick event is not getting fired/triggered from the function (ShowPanel()).

Is there a way how I can fire the btnSubmit_Click event from the function (ShowPanel()?
Posted
Updated 21-Oct-15 22:43pm
v3
Comments
F-ES Sitecore 22-Oct-15 4:56am    
This code;

$('#btnSubmit')

accesses an item with an ID of btnSubmit. View the source of the page, are there any items there with an ID of btnSubmit?
JacoBosch 22-Oct-15 6:20am    
Yes, the only ID of btnSubmit is the Submit button
F-ES Sitecore 22-Oct-15 6:35am    
Did you view the page source and see an element with

<input type="submit" id="btnSubmit" ...

or are you just assuming?
JacoBosch 22-Oct-15 7:26am    
Yes, the ID is there :-)

1 solution

OnClientClick calls the function ShowPanel(); and then you have written "return false;". You are also returning false from the function. So, it can't fire the server side method.

The code should look like below...
HTML
<asp:Button id="btnSubmit" runat="server" Text="Submit" OnClientClick="javascript:return ShowPanel(); "  OnClick="btnSubmit_Click"/>

XML
<script type="text/javascript">
        function ShowPanel() {
      $('#divcontrol').toggle("slow");

                return true;
            }
    </script>
 
Share this answer
 

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