Click here to Skip to main content
15,895,841 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
In Code Behind
protected void Button1_Click(object sender, EventArgs e)
   {
   set();
   }
public void set()
   {
       string s = "10";
       HiddenField1.Value = s;
   }

In Aspx Source

<script language="javascript" type="text/javascript">

function get() {
           var s = form1.HiddenField1.value;
           alert(s);
       }

</script>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" OnClientClick="get()" Text="Button" />

i have a hidden field and iam setting its value in set method in the above code
now i just want to access that value into my javascript's Function named Get

but iam faceing little diffretn output than expected

: the output is like this

1. when i click the button for the first time i get an empty alert but wen i click again am getting the the value i gave

2. When i hardcode the value in the page_load its taking only that value n not getting updated during remaining code execution
Posted
Updated 22-Jun-11 21:51pm
v3

1 solution

That's correct. The OnClientClick is fired before the postback event. So first the 'get()' is fired and then the 'set()'. The first time the 'get()' is not able to find anything, the second time the 'set()' from the first click has fired and set the values, so that the 'get()' now sees the value.

With hardcoding you overwrite your hiddenfield content on every page load with the default value you gave.

So both behaviors are standard .NET. What are you exactly trying to achieve?
 
Share this answer
 
Comments
RakeshMeena 24-Jun-11 1:39am    
Correct, My 5!....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900