469,631 Members | 980 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

How to call a javascript function from a HTML button on a user control

J-T
I have a user control called "Test1" with two button on it ,one is input and
the other one is a server control as follow:

<input type="button"
onClick="this.disabled=true;document.getElementByI d(btnSend).click();value="Go!">
<asp:button id="btnSend" runat="server" onClick="click" Width="90px"
Text="HiddenControl" Visible="False"></asp:button>

As you can see the asp.net button is calling a java script function as
follows:

<script language="c#" runat="server">
void click(object sender, EventArgs e)
{
//do something
}
</script>

when I run this code I get a java script error that "btnSend" is undefined
which generated from the first line (HTML button).I don;t know how to call
click even of server control from the html control.Dose anyone know how to
tackle this problem?

Thanks a lot


Nov 19 '05 #1
7 2292
"J-T" <Re**@Reza.Com> wrote in
news:eG**************@TK2MSFTNGP09.phx.gbl:
when I run this code I get a java script error that "btnSend" is
undefined which generated from the first line (HTML button).I don;t
know how to call click even of server control from the html
control.Dose anyone know how to tackle this problem?


You need to retrieve the rendered button name by using the property
btnSend.clientID.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #2
J-T
You mean I should replace btnSend with btnSend.clientID in
document.getElementById(btnSend).click();?

Thanks
"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@127.0.0.1...
"J-T" <Re**@Reza.Com> wrote in
news:eG**************@TK2MSFTNGP09.phx.gbl:
when I run this code I get a java script error that "btnSend" is
undefined which generated from the first line (HTML button).I don;t
know how to call click even of server control from the html
control.Dose anyone know how to tackle this problem?


You need to retrieve the rendered button name by using the property
btnSend.clientID.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 19 '05 #3
"J-T" <Re**@Reza.Com> wrote in message
news:eG**************@TK2MSFTNGP09.phx.gbl...
As you can see the asp.net button is calling a java script function as
follows:

<script language="c#" runat="server">


Er, no it isn't - it's calling an in-line C# function...

And that's where the problem lies. Why are you trying to write your
server-side code in-line?

It looks to me as if you're trying to prevent users double-clicking your
first button by mistake, by disabling it on the first click - that's fine.

So, do the following, and all will be well:

1) Recreate the page with a separate code-behind page

2) Add the following client-side code to the BODY section:

<form id=frm runat=server>

<asp:button id="btnSend" runat="server" onClick="btnSend_Click()"
Width="90px" Text="Go!" />

</form>

3) In the code-behind page, add the following line *above* the Page_Load
event:

protected Button btnSend;

4) In the code-behind page, add the following line *inside* the Page_Load
event:

btnSend.Attributes.Add("onclick", "this.disabled=true;");

5) In the code-behind page, add the following code *underneath* the
Page_Load event:

public void btnSend_Click(object sender, System.EventArgs e)
{
//do something
}
Nov 19 '05 #4
"J-T" <Re**@Reza.Com> wrote in news:eZ**************@TK2MSFTNGP12.phx.gbl:
You mean I should replace btnSend with btnSend.clientID in
document.getElementById(btnSend).click();?


Yup exactly. But the ClientID property is a codebehind property.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #5
J-T
> 1) Recreate the page with a separate code-behind page

2) Add the following client-side code to the BODY section:

<form id=frm runat=server>

<asp:button id="btnSend" runat="server" onClick="btnSend_Click()"
Width="90px" Text="Go!" />

</form>

I am trying to do this in a user control ,I cannot have a form.

Thanks

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:eD**************@TK2MSFTNGP10.phx.gbl... "J-T" <Re**@Reza.Com> wrote in message
news:eG**************@TK2MSFTNGP09.phx.gbl...
As you can see the asp.net button is calling a java script function as
follows:

<script language="c#" runat="server">


Er, no it isn't - it's calling an in-line C# function...

And that's where the problem lies. Why are you trying to write your
server-side code in-line?

It looks to me as if you're trying to prevent users double-clicking your
first button by mistake, by disabling it on the first click - that's fine.

So, do the following, and all will be well:

1) Recreate the page with a separate code-behind page

2) Add the following client-side code to the BODY section:

<form id=frm runat=server>

<asp:button id="btnSend" runat="server" onClick="btnSend_Click()"
Width="90px" Text="Go!" />

</form>

3) In the code-behind page, add the following line *above* the Page_Load
event:

protected Button btnSend;

4) In the code-behind page, add the following line *inside* the Page_Load
event:

btnSend.Attributes.Add("onclick", "this.disabled=true;");

5) In the code-behind page, add the following code *underneath* the
Page_Load event:

public void btnSend_Click(object sender, System.EventArgs e)
{
//do something
}

Nov 19 '05 #6
"J-T" <Re**@Reza.Com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I am trying to do this in a user control ,I cannot have a form.


Well, like most people, I'm not psychic - you never mentioned a user control
till now...

So, remove the form tags...
Nov 19 '05 #7
J-T
I tried what you said ,it dosen't work :-(

Thanks anyway

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
"J-T" <Re**@Reza.Com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I am trying to do this in a user control ,I cannot have a form.


Well, like most people, I'm not psychic - you never mentioned a user
control till now...

So, remove the form tags...

Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by trinitypete | last post: by
4 posts views Thread by archana | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.