ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 300,378 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 1,526 people online right now. Registration is fast and FREE... Join Now!




calling delegate from usercontrol

 

calling delegate from usercontrol, delegate's javascript not doing anything

janne_panne

23 Jun, 2009 - 11:25 PM
Post #1

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 527



Thanked: 107 times
My Contributions
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.

User is offlineProfile CardPM
+Quote Post


janne_panne

RE: Calling Delegate From Usercontrol

24 Jun, 2009 - 02:32 AM
Post #2

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 527



Thanked: 107 times
My Contributions
Problem solved.

I decided to use Events instead: Cancel button raises an event which is then captured (or something like that?) in the form which owns the control.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 09:21PM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month