referencing javascript function on button event

  • Thread starter Thread starter JohnE
  • Start date Start date
J

JohnE

Hello. I have an asp:Button that in the click event I would like to have it
run a javascript function (function ProteusListPrint()). I currently have
the following in the click event;

btnProteusListPrint.Attributes.Add("onclick", "ProteusListPrint()");

This is not working. Am I missing something or is this not the correct way
to reference it in the click event of the button (C#)?

Thanks... John
 
JohnE said:
Hello. I have an asp:Button that in the click event I would like to have it
run a javascript function (function ProteusListPrint()). I currently have
the following in the click event;

btnProteusListPrint.Attributes.Add("onclick", "ProteusListPrint()");

This is not working. Am I missing something or is this not the correct way
to reference it in the click event of the button (C#)?

Thanks... John

You have server side onclick. And you have client side onclick that
works on the client side where you can access javascript on the client side.

Are you using the right onclick?
 
JohnE said:
Hello. I have an asp:Button that in the click event I would like to
have it run a javascript function (function ProteusListPrint()). I
currently have the following in the click event;

btnProteusListPrint.Attributes.Add("onclick", "ProteusListPrint()");

This is not working. Am I missing something or is this not the
correct way to reference it in the click event of the button (C#)?

You want to add it /before/ the user clicks the button, for example in the
Page_Load event (check If !Page.IsPostBack as you only want to add it once).
Just look at the rendered HTML in the browser to be sure it has been added
the way you intend.

Andrew
 
Hello. I have an asp:Button that in the click event I would like to have it
run a javascript function (function ProteusListPrint()).  I currently have
the following in the click event;

btnProteusListPrint.Attributes.Add("onclick", "ProteusListPrint()");

This is not working.  Am I missing something or is this not the correctway
to reference it in the click event of the button (C#)?

Thanks... John

Your code looks correct. You need to check if ProteusListPrint() is
working properly. Try to add, for example,

alert("started");
.... code
alert("ended");

to see that it is really executed.

Hope this helps
 
Your code looks correct. You need to check if ProteusListPrint() is
working properly. Try to add, for example,

I concur.
The problem is that the javascript isn't working for whatever reason.

That "Attributes.Add" injects javascript which will run on the client.
 
Mr. Arnold said:
You have server side onclick. And you have client side onclick that works
on the client side where you can access javascript on the client side.

Are you using the right onclick?

By definition, yes he is.
 
I concur.
The problem is that the javascript isn't working for whatever reason.

That "Attributes.Add"  injects javascript which will run on the client.

I got your point, I see now that he said "in the click event of the
button (C#)". Not sure what does it exactly mean, but for sure the
code has to be in the Page_Load event as it is already mentioned by
Andrew.
 
Alexey Smirnov said:
I got your point, I see now that he said "in the click event of the
button (C#)". Not sure what does it exactly mean, but for sure the
code has to be in the Page_Load event as it is already mentioned by
Andrew.
.

I should have been a bit clearer plus provide further information. Below is
the source code for the button, the full function, and the code behind.

<asp:Button ID="btnProteusListPrint" runat="server" Height="25px"
Width="100px" Text="Print" onclick="btnProteusListPrint_Click" />

function ProteusListPrint()
{
var a = window.open('ProteusListPrint.aspx', '',
'height=500px,width=800px,scrollbars=1');
a.document.close();
a.focus();
a.print();
}


protected void btnProteusListPrint_Click(object sender, EventArgs e)
{
btnProteusListPrint.Attributes.Add("onclick", "ProteusListPrint()");

}


I have found that if I use an html input button and add the function to the
button's onclick in the source code, it works fine. But, if I use an
asp:button it does not. So, if I understand what is being provided, what I
have in the code behind click event should actually go in the page_load?
Then what goes in the click event for when the user clicks on the button on
the webpage?

I am rather new to asp.net/c#/javascript/html so please excuse my newbieness
type questions.

.... John
 
JohnE said:
I should have been a bit clearer plus provide further information.
Below is the source code for the button, the full function, and the
code behind.

<asp:Button ID="btnProteusListPrint" runat="server" Height="25px"
Width="100px" Text="Print" onclick="btnProteusListPrint_Click" />

Change
onclick="btnProteusListPrint_Click"

to
onClientClick="ProteusListPrint()"


And remove this code:
protected void btnProteusListPrint_Click(object sender, EventArgs
e) {
btnProteusListPrint.Attributes.Add("onclick",
"ProteusListPrint()");

}

HTH,

Andrew
 
Alexey Smirnov said:
I got your point, I see now that he said "in the click event of the
button (C#)". Not sure what does it exactly mean, but for sure the
code has to be in the Page_Load event as it is already mentioned by
Andrew.
.

I might have gotten it figured out. I added the button click event to the
page_load as it was mentioned. In the button click I removed it leaving the
button click event empty. Now it is working as intended when the print
button is clicked.

If this is correct, great. If not, let me know.

Thanks to everyone for their feedback.
 
Thanks to everyone for their feedback.

Hopefully to aid your understanding, <asp:Button /> webcontrols have an
OnClick method and, since v2, an OnClientClick method. There's no need to
use Attributes.Add("onclick", .......) any more, unless you're still using
v1.x.

OnClick runs on the server and OnClientClick (as its name suggests) runs on
the client. <asp:Button /> webcontrols can have one or both of these events
at the same time.

In fact, it's usual to have both because the OnClientClick event runs first
and, if it returns false, then the server-side OnClick event doesn't fire.
This is immensely useful e.g. in getting users to confirm server-side
actions, e.g.

<asp:Button ID="MyButton" runat="server" Text="Delete"
OnClick="MyButton_Click" OnClientClick="return confirm('Are you sure?');" />

Clicking the above button will cause the JavaScript prompt to fire and, if
the user responds "No", the server-side event won't even be called...

I use this all the time, especially for client-side validation...
 
JohnE said:
Hello. I have an asp:Button that in the click event I would like to have it
run a javascript function (function ProteusListPrint()). I currently have
the following in the click event;

btnProteusListPrint.Attributes.Add("onclick", "ProteusListPrint()");

This is not working. Am I missing something or is this not the correct way
to reference it in the click event of the button (C#)?

Thanks... John

I would like to thank everyone that responded. It is the info gathered
(right or wrong) that has a tendency to be remembered. I learned several
things from this post that no doubt will be used again.
Thanks.
 
Back
Top