Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can not fire an event on VB code behind 1

Status
Not open for further replies.

vilrbn

Programmer
Oct 29, 2002
105
FR
Hello,

I want to close a window using an HTML Input file Control button.
First, I need to execute the btnClose_Click sub, then close the window.
After a click on the button, the window is successfuly closed but I never go through the btnClose_Click code.
Can you help me ?

Below is my code:

<script language=&quot;javascript&quot;>
<!---->
function Close_Window(){
window.close();
}
</script>

<form language=&quot;vb&quot; id=&quot;Form1&quot; method=&quot;post&quot; encType=&quot;multipart/form-data&quot; runat=&quot;server&quot;>

<input id=&quot;btnClose&quot; type=&quot;submit&quot; onserverclick=&quot;btnClose_click&quot; onclick=&quot;Close_Window();&quot; value=&quot;Close Window&quot; name=&quot;btnClose&quot; runat=&quot;server&quot;>

Code behind:

Public Sub btnClose_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.ServerClick
End Sub
 
This will not happen. Because the event in the client is fired first. Since the Client Event close the window btnClose_Click will not be fired.

If your requirement is like do some operation on btnClose_Click and the close the client window. U can try this out. Have a server control DIV tag (hidden) in your page.
After the code that has to be done for btnClose_Click the to the div tag innertext pass the javascript code to close the window.

Public Sub btnClose_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
'Your Code go here..
Div1.InnerText = &quot;<Script Language='Javascript'> window.close(); </script>&quot;
End Sub

Now this will be sent to client and since the Div tag renders the script and executes the script and closes the window of the client.

HOPE THIS HELPS..

(If this works, a STAR Please)..
 
I tried to create the DIV tag and now, I can go through my sub without problem.
But the Close action is not working. Where am I wrong ?
Anyway, thanks for your explanations.

<input id=&quot;btnClose&quot; type=&quot;submit&quot; value=&quot;Close Window&quot; name=&quot;btnClose&quot; runat=&quot;server&quot; onserverclick=&quot;btnClose_click&quot;>

<div id=&quot;divClose&quot; runat=&quot;server&quot;></div>

Public Sub btnClose_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.ServerClick
divClose.Visible = False
divClose.InnerText = &quot;<Script Language='Javascript'>window.close();</script>&quot;
End Sub
 
divClose.Visible = True The panel must be visible for this trick to work. When an aspx control visible property is set to false the control is not actually written out with the html. It is however stored in the viewstate, but this is encoded so your script is never being run. Just make sure the panel divClose is visible when you exit the command. Another way to do this is to simply do a response.write. It's all a matter of comfort.
HTH That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hi Vilbrn,
This is what i have done.. u can try this also..

In ASPX
<DIV id=&quot;divScript&quot; style=&quot;Z-INDEX: 102; LEFT: 46px; WIDTH: 100px; POSITION: absolute; TOP: 369px; HEIGHT: 100px&quot; runat=&quot;server&quot; ms_positioning=&quot;FlowLayout&quot;></DIV>

<asp:Button id=&quot;btnClose&quot; runat=&quot;server&quot; Text=&quot;Close&quot;></asp:Button>

In Code Behind
Protected WithEvents divScript As System.Web.UI.HtmlControls.HtmlGenericControl

Public Sub btnClose_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.ServerClick

strScript = &quot;<script language='javascript'> window.close(); </script>&quot;
divScript.InnerText = strScript
End Sub
 
I have tested all of your examples but it's not working.
Now, I can get execute my sub, but can not close the window.

My ASP code:

<input id=&quot;btnClose&quot; type=&quot;submit&quot; value=&quot;Close Window&quot; runat=&quot;server&quot; onserverclick=&quot;btnClose_Click&quot;>
<div id=&quot;divClose&quot;></div>

Code behind:

Protected WithEvents divClose As New System.Web.UI.HtmlControls.HtmlGenericControl()

Protected WithEvents btnClose As System.Web.UI.HtmlControls.HtmlInputButton

Public Sub btnClose_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.ServerClick
divClose.Visible = True
divClose.InnerText = &quot;<Script Language='Javascript'>window.close();</script>&quot;
End Sub

Don't know if I have to add a Runat=server to my DIV tag.
divclose.innertext in code behind has no effect.
I tried to include my button in my DIV tag, same pb.

Please help me !
 
vilrbn I actually just use a label for this.

At the top of my page I include a asp label with no text and the id lblClose.

In the button click event I simply set the text property to the javascript. Works like a charm. Or as I said above simply do a response.write(&quot;<Script Language='Javascript'>window.close();</script>&quot;)

Have fun. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Yep !
Using a label or response.write, it's working perfectly !!!
Many thanks for your help and patience !!
 
no prob That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top