• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1457
  • Last Modified:

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
0
dba123
Asked:
dba123
  • 6
1 Solution
 
jef06Commented:
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)
0
 
dba123Author Commented:
hmm, thanks!!!
0
 
dba123Author Commented:
nope, putting that in didn't help....
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dba123Author Commented:
also I noticed that Intellisense is not picking this up

isRegisterStartupScript

but is picking up everything else fine such as the egisterStartupScript
0
 
dba123Author Commented:
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.
0
 
dba123Author Commented:
It has something to do with the page lifecycle...probably the register isn't happening the first time through...
0
 
dba123Author Commented:
Ok, figured it out...it was the order of what was happening.

I ended up putting the stringbuilder in the page load but then the call to that function in my button click event and then got rid of the btnAccept.Attributes.Add("onclick", "closePopUp()")
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now