1

I'm new to coding javascript/VB.NET and I'm having trouble making my Button2 onClick event fire.

Code-Behind Click Event for Button1 in Page.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 File

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
}

Page OnLoad of Page.aspx

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

The Problem is only the Javascript code is being fired and Modal Page is opened. However, after closing Modal Page, the code-behind click Event is NOT triggered. Any Ideas what's wrong with the code?

Page.aspx Button 2 Mark-up

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

Code-Behind Click Event for Button2 in Page.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

EDIT: A friend of mine suggested using window.open instead of window.showmodaldialog() and the code-behind click event is being triggered! However, I need to use window.showmodaldialog since it will be what the users will be expecting,

Daniel
  • 11
  • 4

2 Answers2

1

click() doesn't fire click event in JavaScript. Use jQuery or use dispatchEvent See this post: How to simulate a click with JavaScript?

Community
  • 1
  • 1
Othella
  • 34
  • 1
  • 6
  • I tried using the dispatchEvent and it also ONLY triggers the javascript code, which is in this case, the code to open modal dialog/window. – Daniel Dec 16 '14 at 11:11
0

Why not call the ShowModal function in stead of triggering an event?

If you need to have the parameters dynamically you can add data-attributes to your element.

EricP
  • 21
  • 3
  • Yes, that would allow the the Modal Page to open, but I also need the code-behind function _hiddenBtnComments_click() to execute AFTER the modal page closes. – Daniel Dec 16 '14 at 11:26