Javascript not firing on a link button - even though it is there.

T

tshad

Using VS 2005, I have the following linkbutton:

<asp:LinkButton ID="lnkSendEmail" runat="server" Text="Send Email"
CssClass="cssbutton"
Width="165px" OnClick="lnkSendEmail_Click"
CausesValidation="False" />

I want to add a Javascript to it so I do the following in my Page_Load
(!IsPostBack) event:

lnkSendEmail.OnClientClick = "Email(this);";

Here is the code that is rendered:

<a onclick="Email(this);" id="ctl00_mContentPlaceHolder_lnkSendEmail"
class="cssbutton"
href="javascript:__doPostBack('ctl00$mContentPlaceHolder$lnkSendEmail','')"
style="display:inline-block;width:165px;">Send Email</a>


So the onclick is there, but it never gets done - I assume because of the
postback, but not sure.

How would I fix this to do this javascript?

Thanks,

Tom
 
M

Mark Rae [MVP]

So the onclick is there, but it never gets done - I assume because of the
postback, but not sure.

How would I fix this to do this javascript?

Firstly, are you absolutely sure that the script "never gets done"...? If
you put a breakpoint (or use the debugger; method) in the JavaScript
function, does execution pause as expected...?

If not, are you absolutely sure that the Email method is available to the
page once it's been rendered to the browser, i.e. can the hyperlink "see"
the Email method...? An easy way to check is to do this:
lnkSendEmail.OnClientClick = "alert('Hello');"; If that works, then you have
almost certainly put the JavaScript method / file in the wrong location...

Finally, might you have accidentally turned JavaScript off...?
 
M

Michael Nemtsev [MVP]

Hello tshad,

And where is your JS code?
Seems that you miss your implementation. Because if u put the following code
on the page the "Hi" message box is shown when u click the linkButton

<script type="text/javascript">
function Email()
{
alert('hi');
}
</script>

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


t> Using VS 2005, I have the following linkbutton:
t>
t> <asp:LinkButton ID="lnkSendEmail" runat="server" Text="Send Email"
t> CssClass="cssbutton"
t> Width="165px" OnClick="lnkSendEmail_Click"
t> CausesValidation="False" />
t> I want to add a Javascript to it so I do the following in my
t> Page_Load (!IsPostBack) event:
t>
t> lnkSendEmail.OnClientClick = "Email(this);";
t>
t> Here is the code that is rendered:
t>
t> <a onclick="Email(this);" id="ctl00_mContentPlaceHolder_lnkSendEmail"
t> class="cssbutton"
t> href="javascript:__doPostBack('ctl00$mContentPlaceHolder$lnkSendEmail
t> ','')"
t> style="display:inline-block;width:165px;">Send Email</a>
t> So the onclick is there, but it never gets done - I assume because of
t> the postback, but not sure.
t>
t> How would I fix this to do this javascript?
t>
t> Thanks,
t>
t> Tom
t>
 
T

tshad

Michael Nemtsev said:
Hello tshad,

And where is your JS code?

Duhhh.

That was the problem.

The <style> line accidently got deleted when I was doing some undos.

Thanks,

Tom
Seems that you miss your implementation. Because if u put the following
code on the page the "Hi" message box is shown when u click the linkButton

<script type="text/javascript">
function Email()
{
alert('hi');
}
</script>

---
WBR, Michael Nemtsev [.NET/C# MVP] :: blog:
http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

t> Using VS 2005, I have the following linkbutton:
t> t> <asp:LinkButton ID="lnkSendEmail" runat="server" Text="Send Email"
t> CssClass="cssbutton"
t> Width="165px" OnClick="lnkSendEmail_Click"
t> CausesValidation="False" />
t> I want to add a Javascript to it so I do the following in my
t> Page_Load (!IsPostBack) event:
t> t> lnkSendEmail.OnClientClick = "Email(this);";
t> t> Here is the code that is rendered:
t> t> <a onclick="Email(this);"
id="ctl00_mContentPlaceHolder_lnkSendEmail"
t> class="cssbutton"
t> href="javascript:__doPostBack('ctl00$mContentPlaceHolder$lnkSendEmail
t> ','')"
t> style="display:inline-block;width:165px;">Send Email</a>
t> So the onclick is there, but it never gets done - I assume because of
t> the postback, but not sure.
t> t> How would I fix this to do this javascript?
t> t> Thanks,
t> t> Tom
t>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top