ZerOne
ZerOne

Reputation: 1326

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

Upvotes: 4

Views: 65764

Answers (3)

Hugo Yates
Hugo Yates

Reputation: 2111

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>

Upvotes: 8

Abdul Khaliq
Abdul Khaliq

Reputation: 2285

100% code behind confirmation! Sometiems its so annyoing to Code behind or Server side confirmation in asp.net when you want to Delete or Update something, I got its solution after a long search as below:

<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return confirm('Are you sure to BLOCK this customer ?')" OnClick="btnSave_Click" />

Now whenever you will click this button it 'll ask to confirm, if you 'll select "NO" page will not post and if you select "Yes" then page will post and your button click event will be fire... like below

Code behind:

protected void Page_Load(object sender, EventArgs e)
    {
    } 

 // Before coming here,,,there will be confirmation   
 protected void btnSave_Click(object sender, EventArgs e)
    {
      GetCustomerInfo();
    }

Upvotes: 3

Mahesh
Mahesh

Reputation: 8892

You need to Add both the events on the button click. OnClientClick handles the Client side script code and OnClick handles the server side event calling.
So add both events and there handlers. The NullReferenceException you are getting because of that.

<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" OnClientClick="return Validate();" />

and in your javascript code can be

function Validate(){
     if (valied())
        return true;
    return false;
}

Upvotes: 5

Related Questions