Logo 
Search:

Asp.net Answers

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds
  Question Asked By: Aysel Kaya   on Feb 13 In Asp.net Category.

  
Question Answered By: Komal Mohammad   on Feb 13

I've set up a prj with both solutions. The syntax may be a bit awkward
for the VBers amoungst us .. but it's best to suss it out.

Just make a new web app under localhost and delete what's in WebForm1 and
replace it with the below


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.IO;

namespace WebApplication9876
{
class myJS
{
string js = "";
public myJS()
{
}
public string  getjs()
{
js += "<script>\n";
js += "alert('hello dan from class');\n";
js += "</script>\n";
return js;
}

}

/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
//true to get from class above, false to get from assembly
bool getfromclass = false;//true;
getScript(getfromclass);
}
private void getScript(bool s)
{
string myjs = s ? RegisterClientScriptFromClass() :
RegisterClientScriptFromAssembly();
if (!Page.IsClientScriptBlockRegistered("WebApplication9876.WebForm1"))
Page.RegisterClientScriptBlock("WebApplication9876.WebForm1", myjs);
}

protected string RegisterClientScriptFromClass()
{
WebApplication9876.myJS js = new WebApplication9876.myJS();
return js.getjs();
}

protected string RegisterClientScriptFromAssembly()
{
string js = "";
Assembly asm = Assembly.GetExecutingAssembly();
if (asm != null)
{
// create stream to the embedded file
//Stream stm = asm.GetManifestResourceStream(asm.GetName().Name + "." +
_scriptName);
Stream stm = asm.GetManifestResourceStream(asm.GetName().Name +
".myJS.js");
StreamReader reader = new StreamReader(stm);
// read it in
js = reader.ReadToEnd();
// clean up
reader.Close();
stm.Close();
}
return js;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
----------------------------------------------------------------------------
--------------------------------------------------------------

In addition you will need to create a .js file and make sure you have set it
to EMBEDDED RESOURCE.
In that file put
<script>
alert('hello dan from assembly');
</script>


Just switch from true to false to get the script from teh different places.

//true to get from class above, false to get from assembly
bool getfromclass = false;//true;

Share: