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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trigger JavaScript with ASP.NET "OnClick" 1

Status
Not open for further replies.

Isadore

Technical User
Feb 3, 2002
2,167
US
I know how to create an event handler in a textbox, triggering a JavaScript event, e.g.,

txtmytextbox.attributes.add("OnChange", "myfucntion(this);")

How do I trigger a JavaScript event with an ASP.NET button without creating an HTML button? I would like to fire the OnClick event of the ASP.NET button and at the same time trigger the call for the execution of a JavaScript function.

Any ideas?
 
Alternatively, I could use the &quot;INPUT&quot; style button and not use <asp:Button.../> but I can't seem to get the HTML INPUT button to &quot;size&quot; to the same size as the asp button; otherwise, I could go either either totally Java, or totally dot NET.

What I am trying to do is open up a pop-up window of pre-determined size (a small pop up window).

I couldn't find any references in my ASP.NET books that suggest how to &quot;re-size&quot; a window when you send the Response.Redirect statement. If I could re-size the window when it opens in dot NET only I can abandon Javascript altogether for this operation.

Thanks in advance.
 
Quite simple actually. Use the exact same code switching &quot;onChange&quot; with &quot;onClick&quot;

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Zarcom - tried that; didn't seem to work.

Is this difficult in dot NET? If I could add code to the Response.Redirect handler that does two things (1) sizes the redirected window; and (2) centers it when it does pop up; then problem solved.

If I go the JavaScript route, and it sounds like we're close to the solution here, then I have to send a QueryString value along with the call for the java function (something I haven't done to date).

Which route would you take Zarcom? Java or dot NET here?

It's always the small stuff that nails me...thanks!
 
I would go with a bit of a mix actually. As far as I know you are correct .NET doesn't explicitly support resizing the window. Which in a way makes sense. It is a server side technology. Any way lets see what I can bake up jus a sec... Got 2 for you

Dim sb As New StringBuilder()
Dim word As String = &quot;laudable&quot;

sb.Append(&quot;<Script language=javascript>&quot;)
'sb.Append(&quot;function Open(){&quot;)
sb.Append(&quot;window.open(' sb.Append(word)
sb.Append(&quot;','mywindow', 'width=600, height=600')&quot;)
'sb.Append(&quot;}&quot;)
sb.Append(&quot;</Script>&quot;)

RegisterStartupScript(&quot;mine&quot;, sb.ToString)


Put this code in your button onclick handler in the code behind. Simply writes javascript to the page which will then be run.


Dim sb As New StringBuilder()
Dim word As String = &quot;laudable&quot;

sb.Append(&quot;<Script language=javascript>&quot;)
sb.Append(&quot;function Open(){&quot;)
sb.Append(&quot;window.open(' sb.Append(word)
sb.Append(&quot;','mywindow', 'width=600, height=600')&quot;)
sb.Append(&quot;}&quot;)
sb.Append(&quot;</Script>&quot;)

RegisterStartupScript(&quot;opener&quot;, sb.ToString)

Button1.Attributes.Add(&quot;onClick&quot;, &quot;Open()&quot;)


PUt this one into your page onload event. You could also use this with a simple html button so no round trip to the server takes place.

Hope you like em. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Zarcom: This site would be a dead letter with out guy.
Thanks. IF I have any problems I'll get back to you but I think I can take it from here.
 
Zarcom: One other point. Using XP Style buttons I have been unable to make an HTML button &quot;look like&quot; an aspx button. Perhaps I should try and resize the asp buttons to look like the HTML buttons but the latter are better looking. Small point. I&quot;ll get back with ya.
 
Two problems, one corrected:

1) sb.Append(&quot;window.open('
..had to remove the semi-colon ; at the end, and...

2) ..now stuck trying to get past &quot;String must end with a double quote&quot; error in the last line...q.v.,

sb.Append(&quot;</Script>&quot;)

...doesn't like the way this is ending...tried every concievable combination...still tweaking.
 
From the compiled error:

sb.Append(&quot;','mywindow', 'width=300, height=300')&quot;)
Line 71: sb.Append(&quot;}&quot;)
Line 72: sb.Append(&quot;
Line 73: #End ExternalSource
 
Zarcom: On the semi-colon. Don't know how two of them got on the post (2 above) but even with one semi it wouldn't let me through the error until I removed it completely, that's when I got hung up on the double string problem.
 
Not sure what is happening here Izy. I used that exact code and it worked for me.
Are you using code behind or spagetti code?

A note on the xp style buttons. I simply used an input tag and the button was displayed xp style. However, I am using an XP machine so...
If you can't get them to look right then you could just use a web form button. I realize that it causes a post back however, as far as the user is concerned this won't matter as they need to wait for the new window to open and load anyway correct?

If I think of something on the double quote thing I'll let you know. Or if you can clarify exactly what you have and what error is occuring i'll take a look whichever. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hey guys,

Just a note on the XP button thing:
If you're using XP, and you're style is set to one of XP's styles, all buttons are going to look that way.
i.e. I have the &quot;Mac&quot; style selected for my XPHome pc. If I go to Yahoo mail, all the buttons look like that &quot;Mac&quot; style. However, if I go to work, they look normal.

I'd suggest trying to use CSS to alter the look of the buttons, as I'm thinking that the XP style might override any buttons that don't have a CSS attached to them.

Just a thought though.

D'Arcy
 
Zarcom: Thanks. What's spagetti code? I use a single aspx, no code behind, but will move to VS SDE when we move to the SQL Server. If it worked for you I should be able to get it going.
 
ahh shit! spaggetti code is just what you are doing. VB code mixed together with html code. aka not code behind.

I do know that there are a few tricks with spaggetti style and quotes and such.

umm try this separate the </
sb.append(&quot;<&quot;)
sb.append(&quot;/&quot;)
sb.append(&quot;Script>&quot;)

I think that will do it but amn't sure That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
:O

Mark, you said a swear word! Thats not good.

You may be unsure of forum etiquette. Why not check here for a description:FAQ796-2540


ROFLMAO!!! I've been WAITING to be able to make a joke about that!
:D

my feeble recourse since my pittiful Bombers are in the playoffs no more...*sniff*
;)

D
 
[rofl2] AHAHAHA
ha
ha...
your so funny :-D

Yes it seems as though we will be triumphant monsee That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Zarcom: u da man. Danke shone. Breaking it up so it fits spagetti logic worked like a charm. Here's the whole page and it works perfectly.

<%@ Page Language=&quot;VB&quot; Debug=&quot;true&quot; %>
<%@Import Namespace = &quot;Microsoft.VisualBasic&quot;%>
<%@Import Namespace = &quot;System&quot;%>
<%@Import Namespace = &quot;System.Web&quot;%>
<%@Import Namespace = &quot;System.Web.UI&quot;%>
<%@Import Namespace = &quot;System.Web.UI.WebControls&quot;%>
<%@Import Namespace = &quot;System.Web.UI.HtmlControls&quot;%>


<script runat=&quot;server&quot;>

Private Sub Page_Load(sender As Object, e As EventArgs)

Dim sb As New StringBuilder()
Dim word As String = &quot;laudable&quot;

sb.Append(&quot;<Script language=javascript>&quot;)
sb.Append(&quot;function Open(){&quot;)
sb.Append(&quot;window.open(' sb.Append(word)
sb.Append(&quot;','mywindow', 'width=600, height=600')&quot;)
sb.Append(&quot;}&quot;)
sb.append(&quot;<&quot;)
sb.append(&quot;/&quot;)
sb.append(&quot;Script>&quot;)

RegisterStartupScript(&quot;opener&quot;, sb.ToString)

Button1.Attributes.Add(&quot;onClick&quot;, &quot;Open()&quot;)

End Sub

</script>

<HTML>
<HEAD>
<title>Historical Site Activity</title>
</HEAD>
<body background=&quot;images/lgtbrown.gif&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Test&quot;/>
</form>
</body>
</HTML>
 
jfrost: You mentioned in an earlier post that the Add attribute java problem was a &quot;memory&quot; one; i.e., once it sticks its there for the duration.

I tried for a few hours tonite to try and change the attribute via VB using the asp:Button but with no luck. Had some success, but always 1 step from perfect. It would be nice if you could use an IF..Then statement and change the button's attribute based on circumstance - hence, the default validators are still the primary way to both validate and send up a message box.
 
Which brings on the final question Zarc.

How do you change the &quot;word&quot; variable in your javascript? In my application I need to throw out 1,2 and 3 depending on what button is clicked.

So, I reproduced the whole routine three times because I couldn't figure out how to &quot;change&quot; the word variable (or in my case an Integer) because, as Mr. Frost has pointed out; once the attribute sticks it's there seemingly for good.

Ideally you should be able to click on the button, throw in some variable &quot;word&quot; and then move with that QueryString and not have to reporduce the entire routine several times although it works ok that way. In spagetti code, a little extra java never hurts.

As important as Java is there should be a few FAQ's written up on the basics.
 
Hey Isadore,

Well, depending on HOW you add the attribute in the code behind, it can/cannot be there for good.

i.e. if you put it in the page's web designer genereated code region of the code behind, its there as soon as the page loads. However, if you put it in a button click (lets say you want the button to have the on-click added only in specific circumstances), you'd need to add it elsewhere in your code (more than likely some sort of event). problem is that you could run into timing issues. For instance, if you want a button to display a msgbox when clicked, and you have the code that adds the attribute in the buttons clicked event, then you'll actually have to click the button twice to get it to work (once to actually set the script, and again to execute it).

it is possible to dynamically change what the onClick attribute is in the code behind, it just means thinking through the flow of interaction first.

hth

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top