简体   繁体   中英

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

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.

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

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

尝试这个:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM