Link to home
Start Free TrialLog in
Avatar of dba123
dba123

asked on

Having to click button twice

why do I have to click my Accept button twice to get to fire off the Javascript function?  Do I need a check on Postback here?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        panAgreement.Visible = True

        sb.Append(vbCrLf)
        sb.Append("<script language='javascript' type='text/javascript'>" & vbCrLf)
        sb.Append("<!--" & vbCrLf)
        sb.Append("function closePopUp()" & vbCrLf)
        sb.Append("{" & vbCrLf)
        sb.Append(" window.opener.location.reload();")
        sb.Append(" self.close();")
        sb.Append("}" & vbCrLf)
        sb.Append("//-->" & vbCrLf)
        sb.Append("</script>")
        sb.Append(vbCrLf)
        Page.RegisterStartupScript("closePopUp", sb.ToString)
        btnAccept.Attributes.Add("onclick", "closePopUp()")
    End Sub

    Protected Sub btnAccept_Click(ByVal sender As Object, ByVal e As EventArgs)
        vcUser.UpdateEBookPolicyDate()
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder("")

    End Sub
Avatar of jef06
jef06
Flag of United States of America image

The code can go multiple time in Page_load
you might want to add
If page.IsPostBack = false Then
   btnAccept.Attributes.Add("onclick", "closePopUp()")
Also to not add multiple time the script closeopopup you can check if it is present
if Page.isRegisterStartupScript("closePopUp") = False Then
      Page.RegisterStartupScript("closePopUp", sb.ToString)
Avatar of dba123
dba123

ASKER

hmm, thanks!!!
Avatar of dba123

ASKER

nope, putting that in didn't help....
Avatar of dba123

ASKER

also I noticed that Intellisense is not picking this up

isRegisterStartupScript

but is picking up everything else fine such as the egisterStartupScript
Avatar of dba123

ASKER

I debugged it, but don't see what the problem is.  Your check for the postback is correct.  When I click the button, it definitely is calling Page.RegisterStartupScript("closePopUp", sb.ToString) the first click but nothing happens.
Avatar of dba123

ASKER

It has something to do with the page lifecycle...probably the register isn't happening the first time through...
ASKER CERTIFIED SOLUTION
Avatar of dba123
dba123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial