The name 'ResolveUrl' does not exist in the current context - asp.net

i am using asp.net framework 4 and i have created brand new project and below is the code and when i compile the proj i get this below errors... i have already spent good amount of time but not sure whats going on here...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace aspnet_membership_proj
{
public partial class SiteMaster : MyBaseMasterPage
{
public void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl myJs = new HtmlGenericControl();
myJs.TagName = "script";
myJs.Attributes.Add("type", "text/javascript");
myJs.Attributes.Add("language", "javascript"); //don't need it usually but for cross browser.
myJs.Attributes.Add("src", ResolveUrl("~/Scripts/JScript.js"));
this.Page.Header.Controls.Add(myJs);
}
}
}
errors:
The name 'ResolveUrl' does not exist in the current context
'aspnet_membership_proj.SiteMaster' does not contain a definition for 'Page' and no extension method 'Page' accepting a first argument of type 'aspnet_membership_proj.SiteMaster' could be found (are you missing a using directive or an assembly reference?)

Check your MyBaseMasterPage class. That is where the problem lies.
Also, Make sure that your MyBaseMasterPage class derives from System.Web.UI.MasterPage.

Related

Depth-first nested routing in an ASP.NET website

I've been exploring the System.Web.Routing namespace, playing with constraints and such, but I can't see a way to implement this.
I'm working on an ASP.NET website (non-WAP, non-MVC) using the WebPages/Razor framework.
I'm trying to implement a form of "nested routing", where a route can contain child routes that are only attempted if the parent matches; each child attempting to match the "remainder" of the URI. A "depth-first" route match search, if you will.
routes.Add(new ParentRoute("{foo}/{*remainder}", new[] {
new ParentRoute("{bar}/{*remainder}", new[] {
new Route("{baz}"),
new Route("{zip}"),
}),
new ParentRoute("{qux}/{*remainder}", new[] {
new Route("{baz}"),
new Route("{zip}"),
}),
));
I've excluded necessary constraints/handlers (among other parameters) for brevity.
In any case, each step descending through the tree would match the {*remainder} tail of the URI. If a branch fails, it moves up and on to the next, essentially testing something like:
foo
foo/bar
foo/bar/baz
foo/bar/zip
foo/qux
foo/qux/baz
foo/qux/zip
Now, I'm certainly not asking "please to write teh codez", but rather a gesture in the right direction.
Where would I want to be looking in the API in order to begin implementing such a feature? I can find countless tutorials and information on writing routes, constraints, etc., but not on extending the routing engine.
Addendum
I'll just keep adding as warrants
Please note, I am aware that URL generation from a "routing tree" such as this would be complicated; it is not something I intend to implement.
I just realized a sort of iterative route generation could suffice; so I guess I'll post that as a possible answer shortly. Nope, it wouldn't. Too many edge cases.
I have the following code but there is one point I am not sure how you want to handle it: do you know how many child can have a route at the maximum?
In the Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route("test/{path}", new RouteValueDictionary { { "path", string.Empty } }, new TestingRouteHandler()));
}
The TestingRoutHandler class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web;
using System.Web.UI;
using System.Web.Compilation;
namespace WebApplication1
{
class TestingRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
//This is where you should treat the request, test if the file exists and if not, use the parent part of the url
string aspxFileName = string.Format("~/{0}.aspx", requestContext.HttpContext.Request.Url.PathAndQuery.Replace("/", string.Empty));
return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(aspxFileName, typeof(Page)) as Page;
}
}
}

Initialize ResourceManager dynamically

I have this function and it works fine to get a translated value from this specific resource file called OkayMessages.
public static string GetResourceString(string resourceKey){
ResourceManager resourceManager = Resources.OkayMessages.ResourceManager;
return resourceManager.GetString(resourceKey);
}
But i have more than 1 resource file and i want this function to get values from those files as well.. Only, i'm having trouble with dynamically/programmatically selecting the right resource(manager).
I have tried to use the code below, and some variants to that, but i always get an error.
public static string GetResourceString(string resourceFile, string resourceKey){
ResourceManager resourceManager = new System.Resources.ResourceManager("Resources." + resourceFile, Assembly.GetExecutingAssembly());
return resourceManager.GetString(resourceKey);
}
The error i got most of the times was: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Resources.OkayMessages.resources" was correctly embedded or linked into assembly..
Update: I'm using the \App_GlobalResources\ folder for my resource files, and it seems that this is the problem. When i place a resource file in the root of my project, i can initialize a ResourceManager without problems.
After searching in the wrong direction for a while, I just found the most simple answer to this problem. It turns out that there is a method called GetGlobalResourceObject.
So in my case I'm now using this line of code which does all:
GetGlobalResourceObject("OkayMessages", "PasswordChanged").ToString();
Read carefully this article and you'll find that you need to specify correct namespace of the resource. That's your problem. Here is working example if OkayResources.resx resides in project root folder:
using System.Reflection;
using System.Resources;
using System.Web.UI;
namespace WebApplication1
{
public partial class _Default : Page
{
public _Default()
{
var result = GetResourceString("OkayResources", "SomeKey");
}
private static string GetResourceString(string resourceFileName, string key)
{
var resourceName = "WebApplication1." + resourceFileName;
var resourceManager = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());
return resourceManager.GetString(key);
}
}
}
If you put your resource file into Resources folder you'll have to update resource namespace:
var resourceName = "WebApplication1.Resources." + resourceFileName;

Is there a way to do an Ajax Post to an ASPX page that doesn't call a static function?

The question says it all. I am having no trouble making Ajax calls into my ASPX page - as long as I use static methods. However, I'd like to store a result in the session and so need a "live" function call. Any ideas?
Post to a Generic Http handler (.ashx) and have it inherit from IRequireSession interface. Then you can save to session.
I have done quite a bit of research on this problem and, while #latr0dectus' answer may work, I am looking to implement quite a few calls back to the application from within the page. Perhaps I needed to do more research but I did not find a way to call multiple methods within the Generic Http handler nor a way to pass in complex arguments. So, this didn't work very well for me.
A much easier solution is just to use a a WebService and to use the [WebMethod(EnableSession = true)] attribute.
A complete example of a Session-aware web service that you can reach from Ajax is:
using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.SessionState;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class SCMasterService : System.Web.Services.WebService {
public SCMasterService () {
}
[WebMethod(EnableSession = true)]
public string GetSummaryList(string ID)
{
return Session["SomeVal"];
}
}
To call this from a Javascript/JQuery/Ajax "Post" you will need two key lines:
[System.Web.Script.Services.ScriptService]
Makes this web service available to Ajax and
[WebMethod(EnableSession = true)]
enables session management in the called method.

web service in asp.net

I want to create an web service for 'sign up' page or database connectivity..
I am done with database connectivity
but i am unable to get how ca i design an sign up page in web service.. as there is no interface in web service.
Please tell me
my web service code is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace WcfService1
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
string Connection = "Data Source=SHUMAILA-PC;Initial Catalog=kse;User ID=sa;Password=sa";
[WebMethod]
public void SQLconn()
{
SqlConnection DataConnection = new SqlConnection(Connection);
// the string with T-SQL statement, pay attention: no semicolon at the end of //the statement
string Command = "INSERT INTO login VALUES ('hina','me12')";
// create the SQLCommand instance
SqlCommand DataCommand = new SqlCommand(Command, DataConnection);
// open the connection with our database
DataCommand.Connection.Open();
// execute the statement and return the number of affected rows
int i = DataCommand.ExecuteNonQuery();
//close the connection
DataCommand.Connection.Close();
}
}
}
but i am unable to get how ca i design an sign up page in web
service.. as there is no interface in web service.
In fact your reasoning is correct. An ASMX web service implements the SOAP protocol and cannot have any GUI interface whatsoever. Top design an interface of a web application in .NET you could use ASP.NET for example. So you could create an ASP.NET application which will consume this web service. By the way ASX web services are considered as deprecated technology and you should use WCF instead.
There are different techniques. However, its common to utilize Ajax requests to your webmethod via AJAX.
Here is an older walk-through but its still valid - http://dotnetslackers.com/articles/ajax/Using-jQuery-with-ASP-NET.aspx
It demonstrates two approaches using jQuery (which I recommend) and Microsoft Ajax.
Here are some more decent links -
http://www.asp.net/ajaxlibrary/jquery_dibs.ashx

Difference between "using MyNameSpace;" and "namespace MyNameSpace"

Hello I am new to asp.net. I am confused what is the difference between "using MyNameSpace;" and "namespace MyNameSpace". My demo code is as follow...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyNameSpace;
namespace MyNameSpace
{
public partial class DemoPage : System.Web.UI.Page
{
My code here
}
}
In the above code is there any difference between the two highlighted statements or not. If yes then what is it?
Thanks in advance...
Yes, they provide complementary services.
A using directive like this:
using MyNamespace;
Tells the compiler to look in the namespace MyNamespace when resolving simple names - so if you have a type called MyNamespace.Foo, you can just use Foo in your source to refer to it when you've got this using directive.
However, the namespace declaration effectively says, "Anything I declare within this block is in the given namespace". So to declare the MyNamespace.Foo type, you'd use:
namespace MyNamespace
{
public class Foo
{
...
}
}
Do you see? The using directive says that you want to use things in a particular namespace, whereas the namespace declaration is about putting things into a particular namespace.
using is used for creating a "shortcut" to typenames within that namespace. This is only needed when the code you write is within another namespace. namespace is used for defining a namespace:
Example
In file first.cs:
// define the namespace "SomeNamespace"
namespace SomeNamespace
{
// define a type within the namespace
class SomeClass { }
}
In file second.cs:
using SomeNamespace;
// define the namespace "OtherNamespace"
namespace OtherNamespace
{
class OtherClass
{
void SomeMethod()
{
// use the type "SomeClass", defined in the "SomeNamespace" namespace
// note that without the using directive above we would need to write
// SomeNamespace.SomeClass for this to work.
SomeClass temp = new SomeClass();
}
}
}
In the above code sample, the declaration of the temp variable does not need to include the namespace, since it is mentioned in a using directive.
Yes, there is a difference. The namespace statement is used to create a namespace, while the using statement is used to make the compiler regognise an already existing namespace.
In your code the using statement has no effect, as all your code is in that namespace so it already knows about it.
As you have using System.Web.UI, the System.Web.UI.Page identifier could be written as just Page as the compiler knows about the classes in that namespace. If you wouldn't have that using statement, you would need the fully qualified name for the compiler to know where to find the class.

Resources