Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm just doing :

Javascript:
JavaScript
function assign(){
 var sHidVar = 'something';
 document.getElementById('<%=hdnPage.ClientID%>') = sHidVar ;
 alert(document.getElementById('<%=hdnPage.ClientID%>'));
}


ASPX
ASP.NET
...
 <asp:HiddenField ID=hdnPage runat=server  />
...


Here is the problem when I do this:
ASPX :
HTML
<a href=#  önclick="Javascript:assign();">Test</a>

it runs fine, the variable is assigned and I can access from CodeBehind by:

C#
string s = hdnPage.Value;


but, to run this function on page load like :

C#
protected void Page_Load(object sender, EventArgs e)
...
ClientScript.RegisterClientScriptBlock(this.GetType(), "assign", "assign();", true);
..
}


it gives an error, saying document.getElementById(...) is not an object

how else can I call the function on page load, of is there something wrong with the function

PS: Also tried the
JavaScript
$('<%=hdnPage.ClientID%>')
, doesn't work
Posted

Place your JavaScript function before just before the end of the tag and test.
 
Share this answer
 
Comments
saud_a_k 31-Oct-12 8:14am    
I use a MasterPage , I need to place the js in the content
You cannot call the javascript function on server side page_load. For that you need to user javascript window.onload event like this
JavaScript
window.onload=function assign(){
 var sHidVar = 'something';
 document.getElementById('<%=hdnPage.ClientID%>') = sHidVar ;
 alert(document.getElementById('<%=hdnPage.ClientID%>'));
}
 
Share this answer
 
Comments
saud_a_k 31-Oct-12 7:47am    
Thanx, that was simple
But still can't you call a jscript fun. from C# ?
bbirajdar 31-Oct-12 8:37am    
Because javascript is executed on client side and server side script is executed on server side.... You should read more on client-server processing model.
saud_a_k 31-Oct-12 8:47am    
Yes I understand that,
of course there is the webmethod way that uses static functions,
but then how are you supposed to use ClientScript.RegisterClientScriptBlock etc.. ??
bbirajdar 31-Oct-12 9:00am    
No.. WebMethods are used for different purpose

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