Add script to page from user control .ASP.NET - asp.net

how can i add a javascript file to the page head from a user control?
Thanks

use Page.RegisterStartupScript("pranay","javascriptFunction()")
put this thing in you load method of user control
check this out :
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx

Are you using master page in your site? If yes then you should do all such includes in the master page. But then also you might face issue with the path of javascript file. You can include the file in following way:
<script type="text/javascript" language="javascript" src="<%= this.ResolveClientUrl("~/Script/jquery.js") %>"></script>
If you want to do it from User Control. Try the following:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Define the name, type and url of the client script on the page.
Dim csname As String = "ButtonClickScript"
Dim csurl As String = "~/script_include.js"
Dim cstype As Type = Me.GetType()
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the include script is already registered.
If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then
cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
End If
End Sub

Related

pass code behind variable value to link in aspx

I have value in variable in code behind file. I want to pass that value to link present in aspx page. this is my code behind file code.
string SchoolCode = Request.QueryString["School_Code"].ToString();
This is my aspx page.
See Progress Report
I want to show progressreportschool.aspx with the url in the link by passing schoolcode.
You can do this in server-side
See Progress Report
Code Behind(VB.Net)
Private Sub aprogress_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles aprogress.ServerClick
aprogress.HRef = "ProgressReportSchool.aspx?" & SchoolCode
End Sub

How to create a link to a website in vb code behind?

It may be simple but the search words in Google give too many irrelevant results.
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
'The link goes here
End If
End Sub
Use Response.Redirect if you want to send the current page to a new url:
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
Response.Redirect("http://www.stackoverflow.com")
End If
End Sub
To open a new url in a new window/tab you would have to use javascript. Normally I would recommend just putting the javascript directly onto the aspx page but in the event that the url will use data from the code behind to generate the url you can use the ClientScript.RegisterStartupScript function.
Protected Sub Menu1_MenuItemClick(sender As Object, e As MenuEventArgs) Handles Menu1.MenuItemClick
If e.Item.Text = "SomeItem" Then
Dim sURL As String = "http://www.stackoverflow.com"
ClientScript.RegisterStartupScript(Me.GetType(), "script", "window.open('" & sURL + "', 'popup_window');", True)
End If
End Sub

generating pages through a single literal control

What if I use one literal control and assign to it - a string, dynamically built for the whole html page in it's code behind file? Will the literal control modify the output in any way? e.g. add extra html, encoding etc
E.g. ASPX file will contain just this
<%
#Page Language="VB"
Title="SamplePage"
CodeFile="SamplePageCodeBehind.aspx.vb"
Inherits="SamplePage"
%><asp:Literal Id="litContents" Runat="Server" />
And in the codebehind (SamplePageCodeBehind.aspx.vb):
Protected Sub Page_Load(
ByVal sender As Object,
ByVal e As System.EventArgs
) Handles Me.Load
Dim sHtml As String = _
"<html><head></head><body>hello world</body></html>"
litContents.Text = sHtml
End Sub
Thanks!
Instead of using the literal control, you could take the complete control over your rendering
Response.Clear()
Response.Write("any html string")
Response.Flush()
Response.End()
in your Page_Load method. This way you could be sure that no unwanted tags are rendered.

Save images to folder in VB.net

I am trying to save an image from the client side to a folder using vb.net
"" the image that has the myImage ID ""
<asp:Image runat="server" ID="myImage" ImageUrl="http://www.govcomm.harris.com/images/1F-81-imageLinks650a.jpg" />
<asp:Image runat="server" ID="myImage2" ImageUrl="http://www.govcomm.harris.com/images/2F-81-imageLinks650b.jpg" />
this is just the location where i want to save my image :
i haven't run or try any thing with this code , i am just wondering how to do this
this location is on the server side
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim saveLocation As String = Server.MapPath("PDFs")
End Sub
Also, I would like to know if there is a way to use the id to save since i might have more than one image to be saved.
Try this one.....
import System.Net
Dim filepath As String = Server.MapPath(myImage.ImageUrl)
Using client As New WebClient()
client.DownloadFile(filepath, Server.MapPath("Specify the path where you want to store+imagename")) //------For example client.DownloadFile(filepath,Server.MapPath("~/Image/282.gif"))
End Using
If you want to upload a file from the client side (from the user via browser) to the server folder, you need to user the FileUpload control
<asp:FileUpload ID="FileUpload1" runat="server" />
And in your Codebehind, you can save that to a location by Calling the PostedFile.SaveAs method
If FileUpload1.HasFile Then
somefileNameWithExtension="file.pdf" ' Replace this with a a valid file name
FileUpload1.PostedFile.SaveAs(somefileNameWithExtension)
End If
EDIT : As per the comment
If you want to download a file from the internet, you can do it with the WebClient classes DownloadFile method. Here is an example.
Using webClient As New WebClient()
Dim targrtFileName = "D:\\myfile.png" '
Dim sourceFile = "http://converter.telerik.com/App_Themes/images/ccHead.png"
'read the Source of your image control and replace in sourceFile variable.
webClient.DownloadFile(sourceFile , targrtFileName)
End Using

Using and accessing Webresource

I know this has been asked before. Yes, i did my research but it doesn't seem to be working for me so i hope you experts can help me out :)
Here's what my Project looks like
http://i.stack.imgur.com/nnPZJ.png
Yes, the Build Action is Embedded Resource. I also added this in the AssemblyInfo
Assembly: WebResource("WFL.WebResource.EXT.XXX.png", "image/png")
So now, in the default.aspx i say
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cs As ClientScriptManager = Page.ClientScript
Dim strReturn As String = cs.GetWebResourceUrl(Me.GetType(), "WFL.WebResource.EXT.XXX.png")
Dim strReturn2 As String = cs.GetWebResourceUrl(GetType(_Default), "WFL.WebResource.EXT.XXX.png")
Image1.ImageUrl = "http://localhost" + strReturn
Image2.ImageUrl = "http://localhost" + strReturn2
Response.Write("http://localhost" + strReturn)
Response.Write("http://localhost" + strReturn2)
End Sub
But when accessing the returned URL, i get The resource cannot be found.
What am i doing wrong?
Thanks in advance.
This being VB and not C#, you don't need the .EXT portion in WFL.WebResource.Ext.XXX.png. That is something C# needs (to specify folder paths), but VB doesn't. You just want your namespace then resource. Try WFL.WebResource.XXX.png and see if that works.

Resources