Surendra Mourya
Surendra Mourya

Reputation: 611

Onclientclick is not working for in asp button

Onclientclick is not working for me in asp button. The button is present inside update panel

    <asp:UpdatePanel ID="UpLoad" runat="server">
<ContentTemplate>
     <asp:Button ID="btnProcess" runat="server" Text="Process" OnClientClick="alert('hi');" onClick="btnProcessing_Click" />
</ContentTemplate>
<Triggers>
    <asp:PostBackTrigger ControlID="btnProcess" />
</Triggers>

Can someone help me where am i going wrong.

Upvotes: 0

Views: 12004

Answers (3)

Surendra Mourya
Surendra Mourya

Reputation: 611

I achieved this with the help of jquery.

 $('#<%=btnProcess.ClientID%>').click(function () {
        alert('hi');
    });

It worked. Thanks all for your help

Upvotes: 2

Bikee
Bikee

Reputation: 1197

You can use PostBackTrigger inside UpdatePanel to trigger both OnClick and OnClientClick events. Here's how:

<asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
        <asp:Button ID="btnProcess" runat="server" Text="Process" OnClientClick="javascript:alert('hi');" onClick="btnProcess_Click" />
    </ContentTemplate>
<Triggers>
   <asp:PostBackTrigger ControlID="btnProcess" />
</Triggers>
 </asp:UpdatePanel>

Upvotes: 0

Rakesh Lankapalli
Rakesh Lankapalli

Reputation: 106

  function validate() {
            //if validation sucess return true otherwise return false.
            if (document.getElementById("txtSample").value != "") {
                return true;
            }
            alert('Enter a value');
            return false;
        }
<body>
    <form id="form1" runat="server">
    <div>
        Enter a value:<asp:TextBox ID="txtSample" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnSubmit" runat="server" Text="Click Me" OnClientClick="javascript:return validate();"
            OnClick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>


In aspx page::
=============

protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write("The textbox value is " + txtSample.Text);
}

Upvotes: 0

Related Questions