Click here to Skip to main content
15,885,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please Take a look to following code.

Default.Aspx
HTML
<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
C#
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

Hi,

Set an event handler to the OnClientClick event on the button:
XML
<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.
 
Share this answer
 
Comments
amit Baswa 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
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
 
Share this answer
 
Call Javascript function on onClientClick event and server side onclick event.
 
Share this answer
 
Comments
amit Baswa 21-Nov-12 12:00pm    
Please Describe your Solution.
By the way there is nothing like 'onClientClick' for '<input /> button'
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.
 
Share this answer
 
Comments
amit Baswa 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 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)



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