precedence between OnClientClick and OnClick?

  • Thread starter Thread starter Phil
  • Start date Start date
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>
 
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" />
 
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.
 
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>
 
Back
Top