Albert Laure
Albert Laure

Reputation: 1722

fire a button click event using java script

How would i fire a button click event after a user pressed the OK button in a OK cancel pop up window?? (like return confrim) i used the following code to call the btnCommitSaving click event if the user pressed OK but i doesnt work. is there something wrong with the code?

 ClientScriptManager csm = Page.ClientScript;
 csm.RegisterClientScriptBlock(this.GetType(), "__Mensagem", "if (confirm('" + "Proceed" + "')){  var button = document.getElementByID(btnCommitSaving);button.click();}", true);

please help :)

Upvotes: 1

Views: 2166

Answers (5)

Vikash Singh
Vikash Singh

Reputation: 814

I have found problem in your code. first of all document.getElementById is wrong in your code. JavaScript is case sensitive.

second problem is that your code is finding button btnCommitSaving before it gets rendered completely on page. so var button = document.getElementById('btnCommitSaving') is returning null and button.click() event is not executing.

Try it to call by some other way when all HTML Page got rendered.

Upvotes: 0

sangram parmar
sangram parmar

Reputation: 8726

Try this:

<asp:Button OnClientClick="return confirm('Are you sure you want to do that?');" OnClick="btnCommitSaving_Click" />

or

ClientScriptManager csm = Page.ClientScript;
csm.RegisterClientScriptBlock(this.GetType(), "__Mensagem", "if (confirm('Proceed?')){$('#" + btnCommitSaving.ClientID + "').click();}", true);

Upvotes: 1

SANDEEP
SANDEEP

Reputation: 1082

it would be helpful to you

                    <div class="loginBox">
                    <script type="text/javascript">
                        function Confirm() {
                            var confirm_value = document.createElement("INPUT");
                            confirm_value.type = "hidden";
                            confirm_value.name = "confirm_value";
                            if (confirm("Are you sure Want to submit all Budgeted Requirement ?")) {
                                confirm_value.value = "Yes";
                            } else {
                                confirm_value.value = "No";
                            }
                            document.forms[0].appendChild(confirm_value);
                        }
                    </script>
                        <asp:Button ID="BtnSubmit" Text="Submit" runat="server" OnClientClick = "Confirm();" OnClick="BtnSubmit_Click" />
                    </div>


protected void BtnSubmit_Click(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (grdBudgetMgr.Rows.Count > 0)
    {
        if (confirmValue == "Yes")
        {

            ClsBudgetManager objBudgetManager = new ClsBudgetManager();
            objBudgetManager.UpdateBudgetSentAllStatus(Convert.ToInt32(hdnClientId.Value), Convert.ToInt32(ddlBudgetlist.SelectedValue), true);
            GetData();
        }
    }
}

Upvotes: 1

debracey
debracey

Reputation: 6597

Just did this earlier today. Easiest way is to use __doPostBack from javascript:

// Assume myButton is an <asp:Button> (or anything) previously defined
// that has an OnClick event registered in the code behind
var uniqueID = '<%= myButton.UniqueID %>';
var additionalData = '';
__doPostBack(uniqueID, arguments);

The arguments come back in Request["__EVENTARGUMENT"] if you're interested in those. The tricky thing is to use UniqueID instead of ClientID with the __doPostBack. A lot of sites tell you to use ClientID, this will do the postback, but won't route into the event handler.

Edit

Another opition is to set OnClientClick on the button to a javascript function that returns true/false:

function someJSFunction()
{
   return confirm('Are you sure you want to do that?');
}

<asp:Button OnClientClick='return someJSFunction();' OnClick='CodeBehindMethod' />

This way, the codebehind method will only fire if the JS function returns true.

Upvotes: 1

suff trek
suff trek

Reputation: 39767

Try this:

csm.RegisterStartupScript(this.GetType(), "__Mensagem", "if (confirm('Proceed')){  var button = document.getElementByID('btnCommitSaving');button.click();}", true);

Upvotes: 0

Related Questions