javascript code using c# code behind

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
azharrahi
4/28/2009 11:41:04 AM
asp.net.client-side 24353 articles. 2 followers. Follow

10 Replies
7147 Views

Similar Articles

[PageSpeed] 36
Get it on Google Play
Get it on Apple App Store

 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
Coool
4/28/2009 11:45:50 AM
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
allanhorwitz
4/28/2009 11:46:12 AM

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
qwe123kids
4/28/2009 11:46:48 AM

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
BigjimFRG
4/28/2009 11:49:10 AM
you need to use RegisterClientScriptBlock, try below code 
String 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
4/28/2009 11:51:44 AM

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
azharrahi
4/28/2009 3:13:08 PM

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
NC01
4/28/2009 4:11:10 PM

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
4/29/2009 5:29:59 AM

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
NC01
4/29/2009 11:58:51 AM

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
sumitd
4/29/2009 12:52:49 PM
Reply:

Similar Artilces:

C# code behind variable in client side javascript
Is there a way to add a code behind variable to a javascript: window.open command in the NavigateURL or DataNavigateUrlFormatString of Hyperlink Columns and Bound Hyberlink columns? M view post 1039Sushila Bowalekar PatelVisual ASP/ASP.NET MVPhttp://weblogs.asp.net/sushilasb How about inside a plain client side window.open call in javascript? view post 1034Sushila Bowalekar PatelVisual ASP/ASP.NET MVPhttp://weblogs.asp.net/sushilasb Thanks a ton! ...

Call javascript function from C# code behind using html source code output
I have three tabs on a Main.aspx page(tabs 1, 2, and 3) created using javascript.  When the user leaves the main page and returns to it (if postback) I need the tab selection to remain in session.  In other words, if the user picks tab "2" then leaves that page (but doesn't close the browser) then returns I want that same tab "2" to be active for them.  That way they don't have to reselect the last tab they were on.  How do I call the function from my code behind page using C# ?  here's some of my javascript code I need help with...

Calling a Form Code-Behind function using Javascript from the Client Side
Hi,Is there any way to call a function in the form codebehind from the client side using javascript? If yes, how? How to send parameters?Thanks. Try reading this article.  It is kind of the opposite of what you are trying to do but maybe it will help.http://www.tek-tips.com/viewthread.cfm?qid=1027513&page=1Hope this helps,MattADV Enterpriseswww.adventerprises.net do a view source on the page and you most liekly will see a do post back function. You can call that with JavaScript and it forces the page back. You can send parameters back by placing them inside of hidden form e...

finding server control, in the code behind, created from client side using javascript
Hi experts   i wanna add server side controls using javascript and access it in code behind but i am not able to access it can u give me the solution example javascript file function AddControl() { var table=document.createElement('table') var tr=document.createElement('tr') var cell1=document.createElement('td') cell1.innerHtml='<input type="text" ID="txtSample" runat="server"/> tr.appendChild(cell1) table.appendChild(tr); }   if i try to get this control in code behind like this i am not able to get ...

Question about client side and code behind code
How is code behind code processed on client side? I ask because I'm trying to figure out a problem I have with a link button in a gridview. If I open the page and run it as normal everything fires fine. I click on the link button and it loads the new page. However if I email the page to myself and click on the link button nothing happens. I do not think that the Gridview_Rowcommand fires. The link button represents a DB ID field and all the code is in the GridView_RowCommand event in the code behind and fires a Server.Transfer at the end to load the new page. I have tried Response.Redi...

How to call code behind code using javascript ?
Hi I have written code behind for logout button in one page. like That  protected void ImageButton1_Click(object sender, ImageClickEventArgs e)    {        //Code For Logout of User         clsAdminUsers obAdmin = new clsAdminUsers(Request.PhysicalApplicationPath);        obAdmin._UserId = lblID.InnerText;        Application.Lock();        bool blnResult = obAdmin.LogoutUser();   &n...

executing server side code and client side (javascript) code simultaneously while clicking a button
how to execute server side code and client side (javascript) code simultaneously while clicking a button in asp.net. You can't. When clicking a button: the client-side executes, the page does a PostBack, then the server-side executes.NC... what if i want to update a database and then open a new windows is it possible with Jscript.net?...

use VB.NET and C#.NET code in the same C#.NET project
All-- Here is a sample that is "off the beaten path", (at least for me). Is it possible, in an ASP.NET application, using the code-behind page building technique, to have both pages written in VB.NET and pages written C#.NET?At http://www.WebLogicArts.com/DemoList.aspx there is a sample that shows that, (contrary to popular belief), it IS possible to mix ASP.NET pages built with C#.NET with ASP.NET pages built with VB.NET in the same VS.NET 2003 project. Note that this is just a "fun" sample to see if it can be done and I do not recommend this practice as a "standard" way of develo...

Atlas client side control value available to server side C# code behind
I have an existing ASP.NET application. The page contains a datagrid (asp:datagrid). In my current C# code behind, I fetch data from SQL Server 2005 and bind to the datagrid when a button is selected (Click event). There can be many entires in the grid however, so the user typically enters text in a textbox as a filter on the returned results from SQL Server 2005. Now after studying an Atlas sample animalservice http://atlas.asp.net/docs/atlas/doc/data/default.aspx, with an HTML suggestion text box, I would like to use client-side Atlas to give the user a better indication of...

Confirmation Message Box using Javascript in between code from Code behind
Hi all, Im using C# as my code behind, im having a code in the button click event where im checking whether a file is exists, if it exists then it show the confirmation message box like "file found, want to continue", but its happening if we register the script in the page load  and its not checking the condition. By clciking the button itself its showing the message box.   i need that script should get pop up only after the condition get satisfied. Is it possible? Please help me out to solve this problem. Thanks a lot in advance. The code is given below private void Page...

HANDLING THE SEQUENCE OF CODE BEHIND CODE AND CLIENT SIDE SCRIPTING?
In a form on a page I want to save the data values entered into the varieous server controls, such as RadioButtonLists, TextBoxes, etc. before displaying the saved information in a new page suitable for later print. I have a LinkButton on page1.aspx, whose Click event uses the code behind to save the values into a dataset. The LinkButton also has a OnClientClick then execute the JavaScript on page1.aspx, that simply does a window.open of page2.aspx that uses a different Masterpage from Page1 in order to respect the printing requirements. But!!!!!!! The client side script is executed befor...

Can I use HTML code to execute a function in c# code behind
I want to ask ..if it is possible to call a function  writen inside the code behind (.cs) -c#- because I am using a grid component (AxpDataGrid component)and I want to put an HTML button inside the grid and call my function when the user clicked on it .  Eng.Osman Hamadwww.jeddah4it.com hi, yes sure you can in your html just write <%# YourFunctionInCodeBehind(Parameters) %> this will call your function also there is event handler for the DataGrids called ItemDataBound fires for every new added item, regards,Mohamed Hasan Yes, Just make the function ...

client side code & server side code
what is the difference between client side coding & server side coding ? when to use client side & server side code? Client side code runs on the user's browser and includes languages such as javascript and vbscript. Server side code runs on the web server and can be written in languages such as ASP.NET, PHP, ASP, ColdFusion etc. Website Design Darlington - http://mdssolutions.co.ukhttp://lessthandot.com - Experts, Information, Ideas & Knowledgehttp://aspnetlibrary.com - An online resource for professional ASP.NET developersPlease remember to click "Mark as Answer...

How to convert C#.Net code to VB.Net code ???...
HI, i was searching something new, then i suddenly find some tips to convert C#.Net code to VB.Net but i did not do well. i am puttint its url here plz tell me how to convert C#.Net code to VB.Net. http://www.dotnetspider.com/code/C-273-Convert-C-VB-NET.aspx Jasim...Please remember to click “Mark as Answer” on the post that helps youJasim AkhtarNew Delhi ( INDIA ) It would be better if you write the problem you having, while converting from C# to VB.NET. You cannot completely rely on online converters. ThanksMark post(s) as "Answer" that helped youElectronic ScrewWebsite||Bl...

Web resources about - javascript code using c# code behind - asp.net.client-side

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

JavaScript - Wikipedia, the free encyclopedia
programming styles. JavaScript was formalized in the ECMAScript language standard and is primarily used in the form of client-side JavaScript ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
... editor - report abuse/spam - help the entire directory only in Tools/Obfuscators Description Top : Computers : Programming : Languages : JavaScript ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript
The Facebook SDK for JavaScript provides a rich set of client-side functionality for adding Social Plugins, making API calls and implementing ...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: jobs@sometrics.com Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 1/19/2016 3:34:14 PM