1

用户在确定取消弹出窗口中按下确定按钮后,我将如何触发按钮单击事件?(如返回确认)如果用户按下“确定”但我不起作用,我使用以下代码调用 btnCommitSaving 点击事件。代码有问题吗?

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

请帮忙 :)

4

5 回答 5

1

今天早些时候才这样做。最简单的方法是__doPostBack从 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);

Request["__EVENTARGUMENT"]如果你对这些感兴趣,这些论点就会回来。棘手的事情是使用UniqueID而不是ClientID__doPostBack. 许多网站告诉您使用ClientID,这将执行回发,但不会路由到事件处理程序。

编辑

另一种选择是OnClientClick在按钮上设置一个返回 true/false 的 javascript 函数:

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

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

这样,代码隐藏方法只会在 JS 函数返回 true 时触发。

于 2013-09-26T03:44:18.313 回答
1

对你有帮助

                    <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();
        }
    }
}
于 2013-09-26T05:13:57.557 回答
1

尝试这个:

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

或者

ClientScriptManager csm = Page.ClientScript;
csm.RegisterClientScriptBlock(this.GetType(), "__Mensagem", "if (confirm('Proceed?')){$('#" + btnCommitSaving.ClientID + "').click();}", true);
于 2013-09-26T05:18:01.750 回答
0

尝试这个:

csm.RegisterStartupScript(this.GetType(), "__Mensagem", "if (confirm('Proceed')){  var button = document.getElementByID('btnCommitSaving');button.click();}", true);
于 2013-09-26T03:35:50.127 回答
0

我在您的代码中发现了问题。首先,您的代码中的document.getElementById是错误的。JavaScript区分大小写。

第二个问题是您的代码在页面上完全呈现之前正在查找按钮btnCommitSaving 。所以var button = document.getElementById('btnCommitSaving')返回 null 并且button.click()事件没有执行。

当所有HTML 页面都被渲染时,尝试通过其他方式调用它。

于 2013-09-26T05:38:59.473 回答