calling javascript from c#
-
hi guys, I am stuck on the issue of being able to call a javascrip function from my c# code. This is my javascript:
<SCRIPT LANGUAGE="JScript"> function showPosition() { var oElement = document.all.resultPanel; return oElement.offsetLeft; } </SCRIPT>
The javascript itself works fine, the problem comes down to how do I call my javascript in c#. This was my attempt:private void CallJavascript() { string popupScript = "<SCRIPT LANGUAGE='JScript'>" + "function showPosition(){ var oElement = document.all.resultPanel; return oElement.offsetLeft;}</SCRIPT>"; Page.RegisterStartupScript("PopupScript", popupScript); Label1.Text = popupScript.ToString(); //this is wrong }
Can someone please help me correct the CallJavascript() method. thanks -
hi guys, I am stuck on the issue of being able to call a javascrip function from my c# code. This is my javascript:
<SCRIPT LANGUAGE="JScript"> function showPosition() { var oElement = document.all.resultPanel; return oElement.offsetLeft; } </SCRIPT>
The javascript itself works fine, the problem comes down to how do I call my javascript in c#. This was my attempt:private void CallJavascript() { string popupScript = "<SCRIPT LANGUAGE='JScript'>" + "function showPosition(){ var oElement = document.all.resultPanel; return oElement.offsetLeft;}</SCRIPT>"; Page.RegisterStartupScript("PopupScript", popupScript); Label1.Text = popupScript.ToString(); //this is wrong }
Can someone please help me correct the CallJavascript() method. thanks -
Page.ClientScript.RegisterStartupScript(me.GetType(), "dummyKey", "function name()", true);
jagadeeshkumar2106 wrote:
Page.ClientScript.RegisterStartupScript(me.GetType(), "dummyKey", "function name()", true);
thanks for posting however i still dont get this ClientScript.RegisterStartupScript method. If my javascript is return a value how does this get handled in ClientScript.RegisterStartupScript ?
-
jagadeeshkumar2106 wrote:
Page.ClientScript.RegisterStartupScript(me.GetType(), "dummyKey", "function name()", true);
thanks for posting however i still dont get this ClientScript.RegisterStartupScript method. If my javascript is return a value how does this get handled in ClientScript.RegisterStartupScript ?
-
A hidden field can be something similar to the input element as suggested in the example below ?
<script runat="server"> public void Page_Load(Object sender, EventArgs e) { // Define the name and type of the client scripts on the page. String csname2 = "ButtonClickScript"; Type cstype = this.GetType(); // Get a ClientScriptManager reference from the Page class. ClientScriptManager cs = Page.ClientScript; // Check to see if the client script is already registered. if (!cs.IsClientScriptBlockRegistered(cstype, csname2)) { StringBuilder cstext2 = new StringBuilder(); cstext2.Append("<script type=\"text/javascript\"> function DoClick() {"); cstext2.Append("var oElement = document.all.Panel1; Form1.Message.value= oElement.offsetLeft;} </"); cstext2.Append("script>"); cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false); } } </script> <html > <head> <title>ClientScriptManager Example</title> </head> <body> <form id="Form1" runat="server"> <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" /> <asp:Panel ID="Panel1" runat="server" style="position:absolute;top: 375px;left: 628px;height: 422px;width: 513px; border-color:Red;border-width:thick;border-style:solid"></asp:Panel> </form> </body> </html>
edit: Example is based from MSDN article http://msdn2.microsoft.com/en-us/library/z9h4dk8y.aspx[[^](http://msdn2.microsoft.com<br mode= "New Window")] edit: sorry I worked out what a hidden field is thanks to google http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlinputhidden(VS.71).aspx[^]modified on Thursday, December 06, 2007 8:33:21 AM