Declare Global/Site Wide Variable in ASP.Net VBHTML - asp.net

After about a thousand years of building websites in ASP Classic, I am trying to learn ASP.Net using Razor. I am using Visual Studio 2012. Google seems to be of no help on some of these basic things (but, I probably just don't know the right question to ask). One of the absolute basic things I am struggling with right now is how to declare a variable that I can use across my website.
For example:
DIM DefaultColor AS String = "Green"
I have that in the _AppStart.vbhtml page and I try to access it using
<p>#DefaultColor</p>
in the Default.vbhtml, but I get an error saying that DefaultColor is not declared.
How and or where do I declare a variable that I can access across the website?
If the _AppStart.vbhtml page is the right page, do I need to add something to the default.vbhtml or _template.vbhtml page to load it?

First of all I am not sure if ASP.NET Core is available for VB.NET. At least I tried to create a new project for VB.NET but the ASP.NET Core is not available. I will tell you how you can do it using the C# template.
You can use the TempData object in order to store and retrieve data. You can set a value like this:
#{
TempData["Value"] = "Hello World";
}
Now you can display this value like this.
#TempData["Value"]
An other option is to create a static class which will contain the variable which will host the value.
namespace Sample
{
public class MyStaticClass
{
public static string Value = "Hello World!";
}
}
Now you can display the contents of the value like this:
#Sample.MyStaticClass.Value
You can also set a value and use it later.
Sample.MyStaticClsas.Value = "New Value";
I hope it helps.

Related

Converting my existing code to PageObject design pattern with PageFactory

I'm creating tests using Selenium 2 Web Driver with C#.Net. After reading through a lot of the Selenium documentation, I am not sure if I'm followign the correct design pattern and feeling unsure on how to go about testing using PageObject design patterns.
here is my current code that I'm using on my page and its working
WaitForElement(By.CssSelector("input#ctl00_ctl00_signinControl_txtUsername")).SendKeys("abc123");
WaitForElement(By.CssSelector("input#ctl00_ctl00_signinControl_txtPassword")).SendKeys("password");
SelectElement select;
IWebElement selElement = WaitForElement(By.CssSelector("select#ctl00_ctl00_ddlGoTo"));
select = new SelectElement(selElement);
select.SelectByText("Homepage");
*<more code .....>*
also I have told that I can not use Select page element using pageFactory.
Do I need to change my code the way I have coded? any feedback would be great.
The idea of the page object pattern is to have an object that represents the page. You are essentially writing an API for how to interact with the page.
For example a login page object may have the following methods:
enterUserName(String userName);
enterPassword(String password);
clickLoginButton();
The person using the page object to interact with the page does not need to care about how selenium finds elements and interacts with them. If the id on a field changes you would just need to change the locator on the page object and would not need to change all tests that call the associated page object public method.

ASP.NET: Can you use server tags to embed C# or VB.NET expressions into a Javascript function?

I have an enum called SiteTypes that contains several values that are all bound to a dropdown list. On the client side, I need to check this dropdown to see if the selected value is one of those enum values. I don't want to hardcode the value of the enum in the script in case it needs to change, so I want to use a server tag to get it directly from the enum itself. Conecptually, I would like to do this:
function SiteIdChanged() {
var x = "<%=SiteTypes.Employee %>";
}
The way I am doing it now is created a protected property in the codebehind that returns that specific enum value and am doing this:
function SiteIdChanged() {
var x = "<%=EmployeeSiteTypeValue %>";
}
I don't like that, though, because I have to create a special property on every page that I need to do such a check.
Is there a way to do what I want here?
Are you getting a "xxx is inaccessible due to its protection level" error when you compile or run the page? enums are public by default, classes are not. My guess is that you've defined your enum inside your page's class and you aren't explicitly marking it with the 'public' access modifier. Explicitly mark it as public or move it outside of the class and see what happens. If you're planning on using it on lots of pages you should stick the enum definition in in a file in the App_Code folder of your project.
If you don't like your current implementation I would consider using a PageMethod to compare the dropdown selection to the enum value. This approach will probably be cleaner, as you can do most of the logic server-side.
Here's a tutorial on PageMethods:
http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx
As long as your enum is marked public, you can just go with your first option. There's no need to put a property on every single page you want to retrieve the value from.
That approach is really the simplest solution for writing out server side values in your JavaScript.
You can use the Enum.IsDefined Method this well tell you if the selected value from the dropdown is actually part of your enum.
Enum.IsDefined(typeof(MyEnum), myValue)
http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx

ASP.NET: Custom dynamically populated site map (SiteMapProvider)

I'm trying to write my first very own SiteMapProvider subclass. It is meant to be populated dynamically using a bunch of different database lookups, much like all of the examples I've found on the web.
However, there are a whole bunch of things that are quite unclear to me. Here are my two first questions:
Why is StaticSiteMapProvider used in virtually everyone's project instead of SiteMapProvider? Since the class contains the name "static", I'm getting the impression that it's not as...well, dynamic as I want it.
Can someone provide me with a super-minimalistic SiteMapProvider subclass which populates the map using only static data, i.e. no database access, etc.?
SiteMapProvider can be tottaly dynamic. For example it can make dynamic lookup just for nodes. In contrast with StaticSiteMapProvider you should know whole structure. So this for you to decide what to choose.
You can look at the XmlSiteMapProvider, this is good example of "static" map provider.
public class CoolMapProvider : StaticSiteMapProvider
{
public override SiteMapNode BuildSiteMap()
{
var root = new SiteMapNode(this, "test", "~/test.aspx");
base.AddNode(root, null);
base.AddNode(new SiteMapNode(this, "test-child", "~/test_child.aspx"), root);
return root;
}
}
I did not checked this, but should work.

ASP.NET Localized web site -- updating on the fly

I think I have a solution to this, but is there a better way, or is this going to break on me?
I am constructing a localized web site using global/local resx files. It is a requirement that non-technical users can edit the strings and add new languages through the web app.
This seems easy enough -- I have a form to display strings and the changes are saved with code like this snippet:
string filename = MapPath("App_GlobalResources/strings.hu.resx");
XmlDocument xDoc = new XmlDocument();
XmlNode xNode;
xDoc.Load(filename);
xNode = xDoc.SelectSingleNode("//root/data[#name='PageTitle']/value");
xNode.InnerText = txtNewTitle.Text;
xDoc.Save(filename);
Is this going to cause problems on a busy site? If it causes a momentary delay for recompilation, that's no big deal. And realistically, this form won't see constant, heavy use. What does the community think?
I've used a similar method before for a very basic "CMS". The site wasn't massively used but it didn't cause me any problems.
I don't think changing a resx will cause a recycle.
We did something similar, but used a database to store the user modified values. We then provided a fallback mechanism to serve the overridden value of a localized key.
That said, I think your method should work fine.
Have you considered creating a Resource object? You would need to wrap your settings into a single object that all the client code would use. Something like:
public class GuiResources
{
public string PageTitle
{
get return _pageTitle;
}
// Fired once when the class is first created.
void LoadConfiguration()
{
// Load settings from config section
_pageTitle = // Value from config
}
}
You could make it a singleton or a provider, that way the object is loaded only one time. Also you could make it smart to look at the current thread to get the culture info so you know what language to return.
Then in your web.config file you can create a custom section and set restartOnExternalChanges="true". That way, your app will get the changed when they are made.

Getting project web page names as an enumeration in asp.net

I know that in markup view Visual Studio will provide you with an enumeration of all the page names in your project (add an element and see what you get from Intellisense when specifying the ImageUrl attribute).
My question is: how do I get to that enumeration?
If that's not possible, what would be the best way in asp.net to get the names of your pages without having to hard code strings all over the place? E.g., I'd like to be able to do something like this:
Response.Redirect(PageNames.Default);
(Where PageNames is an enum of some sort)
Is this possible? Thanks in advance!
Here is one suggestion...
Define a class that includes the pages you want, either manually or by reading a Site Navigation file:
static class PageNames
{
public static string Default = "~/Default.aspx";
public static string Contact = "~/Contact.aspx";
public static string About = "~/About.aspx";
}
You can use the class by calling the property name:
Response.Redirect(PageNames.Default);
Another option, that I have looked at but not tried out yet (might next week tho'):
http://blog.devarchive.net/2008/01/auto-generate-strong-typed-navigation.html
Looks very cool, uses T4 templating to generate a strongly typed navigation hierarchy.

Resources