I have a javascript function on html page between , tag. But I want to write that script function on code behind language using C#. In other words I want to move that javascript function on html page to c# code behind language. Can anybody help me please?
![]() |
0 |
![]() |
You can make use of RegisterStartupScript like
Dim current As ScriptManager = ScriptManager.GetCurrent(Me.Page) If current IsNot Nothing AndAlso current.IsInAsyncPostBack Then ScriptManager.RegisterStartupScript(Me, Me.GetType(), "__AddJS", ";function test() { alert('Test'); }", True) Else ClientScript.RegisterStartupScript(Me.GetType(), "__AddJS", function test() { alert('Test'); }", True) End If
Please mark as Answer if it helps u. Thanks!
Parth Patel
Techsture Technologies
Software Developer
Ahmedabad
![]() |
1 |
![]() |
How to add JavaScript at runtime using C# in Asp.Net 2.0
http://sohelruet.wordpress.com/2008/06/14/how-to-add-javascript-at-runtime-using-c-in-aspnet-20/
Allan Horwitz
![]() |
0 |
![]() |
Hi,
RegisterStartupScript("Alert", "<script language=""javascript"">Confirm('Are you sure ');</script>")
check above code
Thanks
Avinash Tiwari
Remember to click “Mark as Answer” on the post, if it helps you.
![]() |
0 |
![]() |
you can use the ClientScriptManager (http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx) introduced with ASP .Net 2.0 to register scripts from the code behind.
![]() |
0 |
![]() |
you need to use RegisterClientScriptBlock, try below codeString scriptText = ""; scriptText += "<script>alert('hello')</script>";
ClientScriptManager.RegisterClientScriptBlock(this.GetType(), "CounterScript", scriptText, true);Refer below link to know more:http://msdn.microsoft.com/en-us/library/3hc29e2a.aspx
Please mark it as answer if it resolves the issue.
visit: http://technicalsol.blogspot.com
![]() |
0 |
![]() |
sumitd:
ClientScriptManager.RegisterClientScriptBlock(this.GetType(), "CounterScript", scriptText, true);
But I want to bind that javascript function to button.
just like
((ImageButton)e.Item.FindControl("btnChangePassword")).Attributes.Add("onclick", "return addRow('" + Index + "','" + Login + "' );");
I want to create javascript function createTable in c# code and pass it to the button attribute.
just like
((ImageButton)e.Item.FindControl("btnChangePassword")).Attributes.Add("onclick", "createTable();");
![]() |
0 |
![]() |
Like this?
if ( e.Item.ItemIndex == 0 )
{
System.Text.StringBuilder javaScript = new System.Text.StringBuilder();
javaScript.Append("function yourJavaScriptFunction()\n");
javaScript.Append("{\n");
javaScript.Append("// code for function here...\n");
javaScript.Append("}\n");ClientScript.RegisterClientScriptBlock(GetType(), "YourJavaScriptFunctionScript", javaScript.ToString(), true);
}((ImageButton)e.Item.FindControl("btnChangePassword")).Attributes.Add("onclick", "return yourJavaScriptFunction();");
NC...
![]() |
0 |
![]() |
Try this
String scriptText = "";
scriptText += "function addRow(index, login){";
scriptText += "alert(index); alert(login); ";
scriptText += "}";
ClientScriptManager.RegisterClientScriptBlock(this.GetType(),
"CounterScript", scriptText, true);
TextBox1.Attributes.Add("onkeyup", "DisplayCharCount()");
((ImageButton)e.Item.FindControl("btnChangePassword")).Attributes.Add("onclick", "return addRow('" + Index + "','" + Login + "' );");
Please mark it as answer if it resolves the issue.
visit: http://technicalsol.blogspot.com
![]() |
0 |
![]() |
sumitd:
Try this
String scriptText = "";
scriptText += "function addRow(index, login){";
scriptText += "alert(index); alert(login); ";
scriptText += "}";
ClientScriptManager.RegisterClientScriptBlock(this.GetType(),
"CounterScript", scriptText, true);
TextBox1.Attributes.Add("onkeyup", "DisplayCharCount()");
((ImageButton)e.Item.FindControl("btnChangePassword")).Attributes.Add("onclick", "return addRow('" + Index + "','" + Login + "' );");
What is "TextBox1"? Why post essentially the same thing that I posted 16 hours prior?
NC...
![]() |
0 |
![]() |
If you check my first post on this, the msdn link tells the same thing which i have posted in my last post, i just customized it as per the user need.
As far as TextBox1 is concerned i missed to remove it. It should be
String scriptText = "";
scriptText += "function addRow(index, login){";
scriptText += "alert(index); alert(login); ";
scriptText += "}";
ClientScriptManager.RegisterClientScriptBlock(this.GetType(),
"CounterScript", scriptText, true);
((ImageButton)e.Item.FindControl("btnChangePassword")).Attributes.Add("onclick", "return addRow('" + Index + "','" + Login + "' );");
Please mark it as answer if it resolves the issue.
visit: http://technicalsol.blogspot.com
![]() |
0 |
![]() |