Document.getElementById('Button').click();不触发代码隐藏

Document.getElementById('Button').click(); not triggering Code-Behind

本文关键字:隐藏 代码 Button getElementById Document click      更新时间:2023-09-26

我是编写javascript/VB.NET的新手,我在触发Button2 onClick事件时遇到了问题。

页面中按钮 1 的代码隐藏单击事件.aspx.vb

Protected Sub _lnbComments_Click(ByVal sender As Object, ByVal e As System.EventArgs)
           //Some Code that needs to run before opening Modal Page
           Dim Script As String = "JavaScriptCode();"
           ScriptManager.RegisterStartupScript(Me.upnToolBar, Me.upnToolBar.GetType(), "CommentsClick", Script, True)
End Sub

JS文件

function ShowModal(page,name,style){
    var r = window.showModalDialog(page,window,style);
}
function JavaScriptCode(){
    var jsButton = document.getElementById('ct100_SiteContent__hiddenBtnComments'); //I made sure ClientID is correct
    jsButton.click() //This should trigger onClick event and OPEN modalPage
}

页面加载.aspx

Me._hiddenBtnComments.Attributes.Add("onclick","ShowModal('SomePage.aspx','SomePage','somestyle')")

问题只是Javascript代码被触发并且模态页面被打开。但是,关闭模式页后,不会触发代码隐藏单击事件。任何想法代码有什么问题?

页面.aspx按钮 2 标记

<asp:Button id="_hiddenBtnComments" runat="server" style="display:none" onclick="_hiddenBtnComments_click"></asp:Button>

页面中 Button2 的代码隐藏单击事件.aspx.vb

Protected Sub _hiddenBtnComments_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _hiddenBtnComments.Click
//Some Code that needs to run AFTER Modal Page closes.
End Sub

编辑:我的一个朋友建议使用window.open而不是window.showmodaldialog((,并且代码隐藏点击事件被触发!但是,我需要使用window.showmodaldialog,因为它将是用户所期望的,

click()不会

在 JavaScript 中触发点击事件。使用 jQuery 或使用 dispatchEvent 请参阅这篇文章:如何使用 JavaScript 模拟点击?

为什么不调用 ShowModal 函数来代替触发事件呢?

如果需要动态参数,则可以向元素添加数据属性。