Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 14308 |  Welcome, Guest!   Register  Login
 Home > Forums > JavaScript > call a JavaScript function in codebehind ...
Nerdanalysis

call a JavaScript function in codebehind

Replies: 4 | Posted by: Nerdanalysis on 5/12/2012 | Category: JavaScript Forums | Views: 2587 | Status: [Member] | Points: 10  


I want to call a JavaScript function in codebehind. It is working fine on the button click event, not on the pageload event. As I can understand this is a Page Life Cycle's Get and post cause. Is there anyway i can get that javascript function on the page load event.

Html code :-

</asp:Label><asp:HiddenField ID="HiddenField1" runat="server" />
<script type="text/javascript" language="javascript">
try {
var i = 1000;
document.getElementById('<%= HiddenField1.ClientID %>').value = i;
}
catch (ex) {

alert(ex);
}
</script>

Code Behind :-

protected void Page_Load(object sender, EventArgs e)
{
try
{
string abc = HiddenField1.Value;
Label1.Text = abc;
Response.Write("asdfas" + abc);
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}

}


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Hariinakoti
Hariinakoti  
Posted on: 5/12/2012 12:53:20 PM
Level: Starter | Status: [Member] | Points: 25

i think itz not possible Nerdanalysis.We can call this function at button click event only.

Thanks & Regards
Hari

Nerdanalysis, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Denny
Denny  
Posted on: 5/14/2012 7:59:17 AM
Level: Starter | Status: [Member] | Points: 25

Hi
You could go for a method of ScriptManager as I would give a demo code.


Here strScript can be your javascript code in Page Load event
You should write the JS in this fashion given below
PageLoad()
{
string strScript = @"function fn()"
+ "{"
+ "var i=1000;"
+ "document.getElementById('" <%= HiddenField1.ClientID %> "').value=i;"
+ "}";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Script", strScript, true);
}

This is a sample ..
Change it acccording to your requirements..

Regards,
Denny Kurian

Nerdanalysis, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Denny
Denny  
Posted on: 5/14/2012 8:23:35 AM
Level: Starter | Status: [Member] | Points: 25

Hi,
Since itz a hidden field you could go for ScriptManager.RegisterHiddenField(Control, String, String)

You have some methods in Script Manager to use it in the UpdatePanel
Plz check it out..

Regards,
Denny Kurian

Nerdanalysis, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Dotnetrajanikanth
Dotnetrajanikanth  
Posted on: 5/15/2012 2:00:27 AM
Level: Starter | Status: [Member] | Points: 25

Page.ClientScript.RegisterStartupScript(Me.GetType(), "Key", "<Your Function Name in Javascript>", True)

eg:

Page.ClientScript.RegisterStartupScript(Me.GetType(), "SampleKey", "Validate();", True)


This will work i am using this code

____________
www.flickr.com/photos/psdesigner/

Nerdanalysis, if this helps please login to Mark As Answer. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 12:59:54 PM