19

I have a question, I have a button like below:

<asp:Button ID="savebtn" runat="server" OnClick="savebtn_Click" Visible="false" />

I then have HTML button like like below:

<button id="btnsave" onclick="fncsave">Save</button>

I have the javascript below:

<script type="text/javascript">
     function fncsave()
     {
        document.getElementById('<%= savebtn.OnClick %>').click()
     }
</script>

My question now is, how can I call the asp:Button OnClick from the HTML button? I have read you can do this by calling from JavaScript by Id but is not working.

Any help will be really appreciate it.

Thank you

2
  • could be a dupe... @Mortalus Is it cool to call the __doPostBack method? I've never done that in practice. Commented Mar 5, 2013 at 4:13
  • I'm not sure as what you mean by "cool" when referring to a JavaScript method :) but yes you can use it. If you can emulate an actual client button click like one of the answers that would be simpler because sometimes you might want to do a partial post back and then the answer in the question i have posted won't work . Commented Mar 5, 2013 at 5:06

2 Answers 2

31

Set style= "display:none;". By setting visible=false, it will not render button in the browser. Thus,client side script wont execute.

<asp:Button ID="savebtn" runat="server" OnClick="savebtn_Click" style="display:none" />

html markup should be

<button id="btnsave" onclick="fncsave()">Save</button>

Change javascript to

<script type="text/javascript">
     function fncsave()
     {
        document.getElementById('<%= savebtn.ClientID %>').click();
     }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

The proper onclick would be onclick="fncsave(); return false;"
Life saver. If I use return false; will it prevent a PostBack?
6

If you're open to using jQuery:

<script type="text/javascript">
 function fncsave()
 {
    $('#<%= savebtn.ClientID %>').click();
 }
</script>

Also, if you are using .NET 4 or better you can make the ClientIDMode == static and simplify the code:

<script type="text/javascript">
 function fncsave()
 {
    $("#savebtn").click();
 }
</script>

Reference: MSDN Article for Control.ClientIDMode

5 Comments

my pleasure. Question. why not just use the ASP.NET button?
I'm using some CSS with jQuery and the ASP.NET is giving me a hard time to style it or make my design work.
That might be the subject for another post. HTML generated by .NET should operate no different that "plain old html" controls with respect to CSS
One more thing, using "static" clientIDMode might "lower the bar" regarding this CSS difficulty.
I've gone trough pretty much every answer about my issue around the web, but your line "Also, if you are using .NET 4 or better you can make the ClientIDMode == static and simplify the code:" Finally made all the difference!!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.