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

  • Thread starter Thread starter tshad
  • Start date Start date
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
 
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...?
 
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>
 
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>
 
Back
Top