Sharepoint 2010 provides a custom tag ScriptLink to register script files in a webpart. This can also be done using simple html script tag but using ScriptLink ensures all the sharepoint script namespaces are available for your custom script.
All the scripts that are to be registered should be placed in Layouts/ProjectName folder of your project. By default, when you create a project Layouts folder will not be added.
To add it, right click on your project –> add –> SharePoint “Layouts” Mapped Folder
A default folder with project name will be automatically created under Layouts folder.
Add your custom script files in Layouts/ProjectName/Scripts folder (create Scripts folder if not exist, it can be any name like JS etc.).
Add your code in the script file, here in this example I have added some alert code which is opened by clicking on web part button ‘click here’
Now add ScriptLink tag in your visual web part (i,e. .ascx control)
If you want to do the same in a standard web part, add the below code in OnPreRender method
Deploy your solution and add a your webpart in a page (say home page).
On clicking button ‘Click Here’ an alert is displayed.