Search code examples
c#asp.netasp.net-webpages

Invoke OnClientClick of button in code behind


I have an button in my aspx web page, which should be 'clicked' in my code behind c# code. The OnClientClick method then call a JS confirm dialog box and so the btn_Click function only get executed when user click 'OK'...

This is my code so far: Variante 1

aspx:

<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />

aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
       // set OnClientClick 
       btnConfirm.OnClientClick = "confirm('Save in DB?');";

       // invoke method
       Type t = typeof(System.Web.UI.WebControls.Button);
       object[] p = new object[1];
       p[0] = EventArgs.Empty;
       MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
       m.Invoke(btnConfirm, p);
}
protected void btnConfirm_Click(object sender, EventArgs e)
    {
        //save something in database
    }

Got code samples by: http://forums.asp.net/t/1046550.aspx?How+do+you+raise+a+button+click+event+from+outside+the+button+

I want to call the OnClientClick method, but when I replace 'OnClick' with 'OnClientClick' this error appear:

System.NullReferenceException was unhandled by user code Message=Object reference not set to an instance of an object.

Edit: Variante 2:

I tried to rewrite my program like this: aspx:

<script language="javascript">
function invokeButtonClick() {
        document.getElementById("btnConfirm").click();
    }

    function Validate() {
        return confirm('Save in db?');
    }
</script>
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />

aspx:cs:

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "InvokeButton", "invokeButtonClick();", true);
}

protected void btnConfirm_Click(object sender, EventArgs e)
{
   // save something in database
}

But then the page postback before my btnConfirm_Click function get called...

Thanks,

Michael


Solution

  • If OnClientClick has a return value of true then the codebehind OnClick event will fire. If OnClientClick is returned false then it won't.

    If OnClientClick doesn't return anything the codebehind event will fire regardless. So you want..

    <asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />
    
    <script language="javascript">
        function Validate() {
            return confirm('Save in db?');
        }
    </script>
    

    Codebehind:

    protected void Page_Load(object sender, EventArgs e)
    {
       // set OnClientClick 
       btnConfirm.OnClientClick = "return Validate();";
    }
    
    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        //save something in database
    }
    

    UPDATE:

    This post just came to my attention, don't know why I didn't include it in my original post but you can use 'OnClientClick' in the aspx tag:

    <asp:Button runat="server" ID="btnConfirm" OnClientClick="return Validate();" Text="Confirm" OnClick="btnConfirm_Click" />
    
    <script language="javascript">
        function Validate() {
            return confirm('Save in db?');
        }
    </script>