Adding script/web resources for ScriptControl implementation - asp.net

I'm not quite familiar with asp.net ajax, so my question might be a silly one.
I'm working on the asp.net ajax control library for self-educational purposes and it is not quite clear to me how to add script/web resources to my class library project.
I've added the following lines to the AssenblyInfo.cs
[assembly: WebResource("BlackGoat.UI.Controls.TooltipControl.Tooltip.js","text/javascript")]
[assembly: ScriptResource("BlackGoat.UI.Controls.TooltipControl.Tooltip.js","BlackGoat.UI.Tooltip", "BlackGoat.UI.Resource")]
The relative path to the javascript file is Controls\TooltipControl\Tooltip.js, however the directories Controls and TooltipControl are not namespace providers so I'm not sure how should I specify the the js file 'namespace' in the AssemblyInfo.cs
Any clarification would be appreciated.
Thanks in advance.

Embedded resources do use folder names as namespaces, so if your default namespace is BlackGoat.UI, and folder is Controls\TooltipControl, it would be namespaced as BlackGoat.UI.Controls.TooltipControl... that's how the framework does it internally.
Check this out: http://www.codeproject.com/KB/dotnet/embeddedresources.aspx
To reference it in your AJAX control, setup the ScriptReference to use the full name as the type, and the name of the assembly (rather than using the path option).

Related

Access Resource File

Can anyone tell how to access resource file in class library asp.net mvc4? I'm building a generic Class Library that I will use it in all my projects. I tried with following code:
HttpContext.GetGlobalResourceObject();
Can anyone tell any other method is there to access the resource file in class library?
Edit based on the comments, including best practices :
To create a Resource file in MVC4 :
In the solution explorer, right click on your project.
Then, you click on "Add", then on "Add ASP.Net folder", and then click on App_GlobalResources.
On the new folder, you right click. Then you add a new Resource File.
You open then this Resource file and can add new resources.
The lefter column is where you set the keys, and the righter one is for the values you have to insert inside it.
Then; it is really easy, you just have to write the following parts of code to access the values you want.
On the c# side, it is :
Resources.NameOfYourResFile.NameOfYourResKey
On the ASP side, assuming that you're using razor, it is :
#Resources.NameOfYourResFile.NameOfYourResKey
Example :
If you have created a a file named "Global.resx", and created the key "ApplicationTitle", which has "My super website" as value, you just have to write the following line of code to put it for example into a string :
string siteTitle = Resources.Global.ApplicationTitle;
and that's it
When you create your resource you have to set the access level from internal (by default) to public. After this you simply can access your resources (if you reference the assembly) by the name of the resource and the static properties generated into them.
I would suggest you to create a new library for Resources
Once you are done making resources key value pairs then add the reference of your Resource Library in to your project. and then you are able to access the resource library in this way
Namespace.ReourceLibraryName.Key
just include System.Web.dll in your class library project and access the resource file just like you would have accessed in the MVC project
HttpContext.GetGlobalResourceObject();
when you add a refrence to this class library project inside your MVC project and run the project this code will be executed in the context of the MVC project and you will get the correct instance of HttpContext.
Note:- directly using HttpContext like this would create problems when you will try to test this application
You can add your resource file any where in your class library, but I think your question is how to use it, If your resource files name is Res1.resx and there is a key in it called message then you can call
Res1.message
from your classes in that library. But you I am sure you would like to use it event from out side the library for that you need to make your resource file public, please refer Visual Studio - Resx File default 'internal' to 'public'
please do let me know if it is the one you need or some thing else?

Can't call static class from different class library?

I'm using the .NET 4 framework, and I have a static class with static functions in an asp.net web application.
I have a second class library project. The class library project wants to call the static method in the web application. Intellisense works, but then the compiler reports that
"The name [MyClassName] does not exist in the current context".
Can I make this call, or this not allowed?
PS, the static class is in the /App_Code folder.
Thanks!
Is it not possible to refactor the class out of the web project? Referencing a web project from a class library sounds awkward. If your class does not contain web-specific code then you could pop it into that other class library you mention or create a new one. Should it reference web-specific libraries something like MyProject.MyLib.Web could do the trick.
You need to add a reference to the project containing the class you want to access.
You need to ensure that you are using the full name of the class (including the namespace).
Alternatively add a using directive with the namespace to your code file.
If your target framework is .Net Framework 4.0 Client Profile change it to .NET Framework 4 and rebuild the solution.

User Control cannot be accessed by another CS file

There is a UserControlA which was already developed when I joined on a project. It is in a Project which I will call MyProjectWeb and it's namespace is MyProjectWeb.Common.
namespace MyProjectWeb.Common
{
public partial class UserControlA : System.Web.UI.UserControl
{
...
There is another WorkFlowManager project that contains a class file which accesses this UserControl1.
MyProjectWeb.Common.UserControlA myUserControlA = (MyProjectWeb.Common.UserControlA)WizardControl.FindControl("TabContainer5$tpSomething$UserControlID");
Note: WorkFlowManager and MyProjectWeb are in the same solution.
This works completely fine. And now I want to create another UserControl which is UserControlB.
I followed the exact same thing.
namespace MyProjectWeb.Common
{
public partial class UserControlB : System.Web.UI.UserControl
{
But to my surprise, I cannot create an instance of UserControlB in the same code file in WorkFlowManager. MyProjectWeb.Common namespace does not even contain a UserControlB. When I compile I get obviously get a
The type or namespace name 'UserControlB' does not exist in the namespace 'MyProjectWeb.Common' (are you missing an assembly reference?)
Why is that I can reference UserControlA but not UserControlB which are in the same namespace from the WorkFlowManager cs file? If I access the MyProjectWeb.Common namespace anywhere inside the MyProjectWeb, I can see both the user controls. Anywhere to look for errors?
I am using Visual Studio 2005 with ASP .NET 2.0
It would depend upon how WorkFlowManager project references MyProjectWeb dll. If both projects are in same solution, then it should have been a project reference but you need to verify it. If the reference is made to dll file then you need to see where exactly the referenced file is stored - my guess is that WorkFlowManager project is referencing a private copy of MyProjectWeb dll that obviously does not get updated when you rebuild MyProjectWeb.
I'd have to look at the project to know for sure, what you're saying above doesn't tell me quite enough.
Quick ideas:
Make sure that UserControlB.cs is actually in the right project.
Make sure that UserControlB.cs is marked as "Compile" for the build type.
Make sure that UserControlB is actually "B" not "A" because, since they're partial, a spelling error could cause them to combine together.
Those user controls are "partial" did you also copy the other part?
Make sure the web project doesn't have any errors, it's possible that the project isn't being built correctly and the error you're getting is because of a stale assembly.
Clean + Rebuild. Delete bin and obj directories manually.

Bulk Move ASP.NET Pages Into A New Namespace?

I have an C# ASP.NET Web Application Project where all the pages are in the global/default/top-level namespace. (I have no explicit namespace declarations. And when I look at my compiled web application's DLL in Red Gate's .NET Reflector, I can verify that all the classes are in the top-level .NET namespace.) Is there any good, automated way to move all the pages and custom controls into a new namespace, say "MyWebApplication"? Ideally it'd be nice to do it with just Visual Studio, but I'd be open to considering a commercial refactoring tool if necessary.
I thought maybe by setting the "Default namespace" property in the project's Application properties I could get the compiler to implicitly put all pages into the specified namespace, but this appears not to be the case; this "Default namespace" setting seems to make Visual Studio insert explicit namespace declarations into new pages, rather than implicitly affecting the namespace of any existing pages.
If it matters, my immediate motivation here is to try to run a static analysis tool (CAT.NET) on my web application; the tool seems to have a quirk or two with code in the global namespace.
Chris it may look silly as you are ready to pay for the refactoring tool to do it. But consider this:
The namespace is declared in the code-behind file by default as the project/application name. So if you project name is MyWeb the default namespace will be
namespace MyWeb
{
public partical class MyWebPage....
Now this is refered in ASPX page as follows:
<%Page ... CodeBehind="MyWebPage.aspx.cs" Inherits="MyWeb.MyWebPage"
You only need to change these two things to put into effect the namespace migration. And frankly this can be achieved using Find/Replace dialog. Please make a copy of your complete solution and try it!!
Cheers

Linq-to-SQL datacontext not generating an object

I am working in a .NET 2.0, recently upgraded to .NET 3.5 environment (VS2008, VB.NET) on an existing ASP.NET website project. I am able to generate a Linq-to-SQL Class (also called a DataContext?) in the App Code folder, drag over tables from an active connection, and save it. Let's call it MyDB. When I go to the code-behind file for my page and try to declare an object of "MyDBDataContext" it is not in the intellisense, indicating that it is not accessible.
I checked the references, and that has to be set correctly because I made the .dbml file.
I made a new test windows app project and it behaved exactly as expected, and I could follow this blog without a problem.
Is there something inherent to web projects that doesn't allow for these auto-generated objects to be usable? Is App Code the right place to declare it?
If you can't tell from the above, I am new to the industry and really new to LINQ.
thanks for your help.
Try expanding the dbml file and open the designer.cs file underneath, and make sure the DataContext class is in the same namespace as the codebehind class. If not, either change the namespace or include it with a using Namespace statement the top.
If it your dbml is in a folder inside of '/App_Code/' it may pick up the folder's name as a namespace. Eg: '/App_Code/DAL' would have the namespace 'DAL'. Give it a namespace in the designer or just use the namespace it is given, if this is the case.

Resources