1

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 :)

2

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you tell me how to implement it in my code? because my problem is if a user pressed OK in a OK-Cancel Button he will perform the buttonclick if Cancel then Do nothing.
or if applicable if OK was pressed do a method.
I provided you with 2 different approaches that should work (didnt test it to be sure...). I typically don't register JS functions from the code behind, that gets messy.
1

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();
        }
    }
}

4 Comments

still nothing happens
ohw im sorry wrong peopel to comment im just starting to try your code.
yes it is working but in your code, the confirmation appears after the click, what i intented to do is in the middle of the flow of the program (not after a click lets say after an iF) it would perform a method/button click after a user has clicked OK and do nothing if no.
ya i understand your problem but its alternative way which i used. i don't know how client side method call server side if you get please let me know also .if this is helpful to you please vote my answer
1

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);

1 Comment

still nothing happens
0

Try this:

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

2 Comments

/Vash nothing happens still just passes through it,
im sorry but i do not know because im new in programmin can you tell me how to look for the error
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.