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

J

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.getElementById(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
 
L

Lucas Tam

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.
 
J

J-T

You mean I should replace btnSend with btnSend.clientID in
document.getElementById(btnSend).click();?

Thanks
 
M

Mark Rae

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
}
 
L

Lucas Tam

J-T said:
You mean I should replace btnSend with btnSend.clientID in
document.getElementById(btnSend).click();?

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

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
 
M

Mark Rae

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...
 

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

Top