precedence between OnClientClick and OnClick?

P

Phil

Hi,

I'm a little confused about the precedence of actions between OnClientClick
and OnClick. In this example, what will be executed first and why?

Thanks
Phil

in code-behind:
---------------
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Hiddenfield1.value="end text"
....
end sub

in aspx file:
----------
<asp:HiddenField ID="hd1" runat="server" value="beginning
text"></asp:HiddenField>
<asp:Button ID="Button1" OnClientClick="test()" runat="server"/>

<script language="javascript" type="text/javascript">
function test()
{
var x = document.getElementById("hd1").value
alert(x)
}
</script>
 
M

Mark Rae

I'm a little confused about the precedence of actions between
OnClientClick and OnClick. In this example, what will be executed first
and why?

OnClientClick gets executed first so that the postback can be cancelled if
required...

<script type="text/javascript">
function validateForm()
{
if (something fails)
{
return false;
}
}
</script>

<asp:Button ID="Button1" runat="server" OnClientClick="return
validateForm();" OnClick="Button1_Click" Text="Click me" />
 
E

Eliyahu Goldin

OnClientClick always goes first since it happens on client side, when you
view the page in your browser. OnClick happens on the server where the page
arrives after a postback.
 
P

Phil

Thanks both.
I have another question in a new thread and i thing is related to the
predecence ...

Eliyahu Goldin said:
OnClientClick always goes first since it happens on client side, when you
view the page in your browser. OnClick happens on the server where the
page
arrives after a postback.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Phil said:
Hi,

I'm a little confused about the precedence of actions between OnClientClick
and OnClick. In this example, what will be executed first and why?

Thanks
Phil

in code-behind:
---------------
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Hiddenfield1.value="end text"
....
end sub

in aspx file:
----------
<asp:HiddenField ID="hd1" runat="server" value="beginning
text"></asp:HiddenField>
<asp:Button ID="Button1" OnClientClick="test()" runat="server"/>

<script language="javascript" type="text/javascript">
function test()
{
var x = document.getElementById("hd1").value
alert(x)
}
</script>
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top