So, the case is this:
I have a FormMain which has a button on. That button opens FormSendMail which has my usercontrol "ctlMailSender" on it. On that usercontrol I have few textboxes and two buttons. Other button is supposed to send a mail according to the information in textboxes and the other button is supposed to close the form and that's a problem at the moment.
I wanted to make the control to be more versatile (not to always close the form, because sometimes I might want to just go to previous page instead of closing etc.) so I decided that I'll call a delegate method when the close button is clicked.
So here is the code for the form:
CODE
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Set some information to the form (sorry for the finnish code in some parts) '
ctlMailSender.Vastaanottaja() = Me.Session("MailSenderReceiver") ' Set receiver address '
ctlMailSender.Liitetiedosto() = Me.Session("MailSenderAttachment") ' Set Attachments '
ctlMailSender.Otsikko() = Me.Session("MailSenderSubject") & " tunnit" ' Set Subject '
' Set the delegate on the usercontrol which should handle the closing of the form '
Dim ClosingDelegate As New ctlMailSender.ClosingDelegate(AddressOf CloseForm)
ctlMailSender.MyClosingDelegate() = ClosingDelegate
End If
End Sub
' Method which is given to ctlMailSender as parameter '
Private Sub CloseForm()
Try
' Clear the session variables '
Me.Session("MailSenderReceiver") = Nothing
Me.Session("MailSenderAttachment") = Nothing
Me.Session("MailSenderSubject") = Nothing
Catch ex As Exception
Finally
' Close the form (NOT WORKING) '
Dim vSb As New System.Text.StringBuilder()
vSb.Append("<script language=""javascript"">")
vSb.Append("window.close();")
vSb.Append("</scri")
vSb.Append("pt>")
' I have already tried the following methods, none of them closed the form: '
'Dim vManager As ClientScriptManager = Page.ClientScript '
'vManager.RegisterStartupScript(Page.GetType(), "close", vSb.ToString()) '
'ClientScript.RegisterStartupScript(Me.GetType(), "close", vSb.ToString()) '
'Page.RegisterStartupScript("close", vSb.ToString()) '
'ClientScript.RegisterStartupScript(Me.GetType(), "close", "window.close();", True) '
'Response.Write(vSb.ToString()) '
End Try
End Sub
And the code for the usercontrol ctlMailSender:
CODE
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
' Property which sets the delegate to session (had problem with this too: '
' if the delegate was a private variable I got nullreferenceexception'
' when calling it so I decided to put it on session)'
Friend Property MyClosingDelegate() As ClosingDelegate
Get
Return Me.Session("SMClosingDelegate")
End Get
Set(ByVal value As ClosingDelegate)
Me.Session("SMClosingDelegate") = value
End Set
End Property
Friend Delegate Sub ClosingDelegate()
' Cancel button: Clears the session which has delegate and fires the delegate method'
Protected Sub imbCancel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imbCancel.Click
Dim vClosingDelegate As ClosingDelegate = Me.Session("SMClosingDelegate")
Me.Session("SMClosingDelegate") = Nothing
vClosingDelegate()
End Sub
No exceptions are thrown. Debugger goes to the CloseForm() method like it is supposed to but no form is closed when I used any of the shown methods for registering the javascript.
So, what am I doing wrong inside the CloseForm() method? Or should I do the whole code in a different way?
If I call:
Page.RegisterStartupScript("close", vSb.ToString())
on the Cancel button event it closes the form. But that would mean that the control would always close the form, I would have to create new control if I wanted to make next ctlMailSender to go back to the previous page instead of closing the form.