Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Please Take a look to following code.
 
Default.Aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
    <script type="text/javascript">
        function call() {
            alert("Call Client");
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="btnclick" name="btnclick" type="button" runat="server" onserverclick="btnclick_ServerClick"  value="Input Button"/><br />
        <asp:Button ID="btnClickAsp" runat="server" Text="ASP Button" OnClick="btnclick_ServerClick"/>
    </div>
    </form>
</body>
</html>
 
Default.Aspx.cs
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnclick.Attributes.Add("onserverclick", "return call()");
        btnClickAsp.Attributes.Add("onclick", "return call()");
 
    }
    protected void btnclick_ServerClick(object sender, EventArgs e)
    {
        Response.Redirect("default2.aspx",false);
    }
 
}
 
Now My problem is that on Click of 'Asp Button' an Alert Box of JavaScript is shown and on click 'Ok' the server side method 'btnclick_ServerClick' is called and redirect to 'Default2.aspx' Page.
BUT
This does not happen same thing for '<input /> Button'. It don't even call JavaScript function nor get call to server side. But I need both to be called. please suggest me right code. Am I going wrong somewhere?
Posted 21-Nov-12 6:27am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Set an event handler to the OnClientClick event on the button:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
    <script type="text/javascript">
        function call() {
            alert("Call Client");
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="btnclick" name="btnclick" type="button" runat="server" onserverclick="btnclick_ServerClick" OnClientClick="return call()"  value="Input Button"/><br />
        <asp:Button ID="btnClickAsp" runat="server" Text="ASP Button" OnClick="btnclick_ServerClick" OnClientClick="return call()"/>
    </div>
    </form>
</body>
</html>
If you do that, you don't need to set the attributes in the Page_Load function.
 
Hope this helps.
  Permalink  
Comments
amit Baswa at 21-Nov-12 12:02pm
   
I did as you mentioned above. But its only calls server side function i.e 'btnclick_ServerClick'. but I want an alert of javascript first then the call to server side function. So please provide me another solution...Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

onclick=" if (inputcall()) { __doPostBack('btnclick', ''); }"
 
this line I think you should check if inputcall has returned true or false and then execute the code.
 
for exp:
OnClientClick="if(!FireConfirm()) return false;"
 
OR ELSE.. check in inputcall if it returns true everytime
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Call Javascript function on onClientClick event and server side onclick event.
  Permalink  
Comments
amit Baswa at 21-Nov-12 12:00pm
   
Please Describe your Solution.
By the way there is nothing like 'onClientClick' for '<input /> button'
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You have to call javascript on input button click event and then use pagemethod or Postback in javascript to call server side function for redirecting it to another page.
  Permalink  
Comments
amit Baswa at 21-Nov-12 12:31pm
   
ok...I got your point. and I tried the following Code
function inputcall() {
return confirm("Call Client input");
}
--------------------------------
<input id="btnclick" name="btnclick" type="button" runat="server" onclick=" if (inputcall()) { __doPostBack('btnclick', ''); }" önserverclick="btnclick_ServerClick1" value="Input Button"/>
 

 
Here first I get Confirm Message, Now I was expecting that if I pressed OK then I will be postBacked and if I press Cancel then It must not call to BostBack,
But this is creating problem for me. On 'Ok' I m getting postback. But if I press Cancel then also I am getting Postback to serverSide event.
Please Suggest me.
Thanks.
Nelek at 21-Nov-12 17:23pm
   
Please don't post solutions to add information, to ask something or to comment another user.
- To add information to your message, you can use the widget "Improve question" / "Improve solution" at the bottom of your text.
- To ask/answer a user, you can use the widget "Have a question or comment?" (as I am doing right now with you) or the widget "reply" in another comment.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 460
1 OriginalGriff 220
2 Kornfeld Eliyahu Peter 210
3 DamithSL 210
4 Peter Leow 100
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100