Using an IF statement in my SiteMaster.cs file - c#

In my SiteMaster.cs file I want to be able to run an IF statement based on the current page the user is on.
I'm currently getting the file name using: currentPage.Text = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx"; but I'd like to then use this to run an IF statement.
It would basically be like (if currentPage == "default.aspx") { // do this }
I'm very new to .NET and taking on a existing project.
Can anyone point me in the right direction to achieve this?
Many thanks

You need to get the page name from the Request.Url.AbsoluteUri.
if(Request.Url.AbsolutePath.Contains("Default.aspx"))
Or, below will return Default.aspx
Request.Url.AbsolutePath.Substring(Request.Url.AbsolutePath.LastIndexOf('/')+1)

Its alot better to compare type. Every page is a class.
if(Page is _Default)
{
// do work
}
If you have to import a namespace and you have visual studio 2010 you can highlight _Default and holding CTRL + .

Related

How to fix variable does not exist in current context in SharePoint WebPart code behind?

I am a complete noob when it comes to developing webparts for SharePoint 2010. I have created a webpart that has button on it. When that button is clicked, I would like it to redirect the user to another page. That would seem like an easy operation to do but I have run into roadblocks.
In my MyWebPart.ascx.cs file, I have the following bit of code:
protected void Button_Click(object sender, EventArgs e)
{
// Get the values the user inputted
try
{
String category = SearchBySelector.SelectedValue;
String keyword = SearchField.Text;
SPWeb root = SPContext.Current.RootWeb.Url;
String url = root + "/path/to/site.aspx?category=" + category + "&keyword=" + keyword;
SPUtility.Redirect(url);
}
catch (Exception ex)
{
StatusLabel.ForeColor = "#FF0000";
StatusLabel.Text = "Exception encountered." + ex.Message;
}
}
The problem I am having is that SPContext (and SPUtility) is not being recognized in this context. How do I fix this? I assume I need some sort of include somewhere? I am not a C# expert either which is probably evident by my code.
Any help would be appreciated.
You'll need the fully qualified namespaces for both SPContext and SPUtility, which are Microsoft.SharePoint.SPContext and Microsoft.SharePoint.Utilities.SPUtility respectively.
You can either use those fully qualified class names in your code, or tack two using statements onto the top of your code file:
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
If Visual Studio still barks at you about not recognizing these types or namespaces, you'll want to be sure the Microsoft.SharePoint assembly is added to the references in your project. You can add references from the Solution Explorer window in Visual Studio.

End page background working, WP8, C#

I dont know if it is even possible, but is there some way how to "end" page in Windows Phone 8 app?
My problem is that i am using one delegate (to know when is my xml downloaded) on multiple pages. It works fine, but when i open one page, she initialize herself, i go on other page (trough back button) and new page initialize herself too. Everything is fine, but the previous page is still listening to the delegate and it is really big problem. So i need to get the previous page (that closed) into a same state like she was not ever opened.
I will be thankful for any advice (maybe i am thinking in wrong way now, i dont know, maybe the page just have to be de-initialize).
PS: If its necessary i will post the code, but i think it is not. :)
Okey here is some code:
In class whis is downloading XML i have delegate like this:
public delegate void delDownloadCompleted();
public static event delDownloadCompleted eventDownloadCompleted;
This class is downloading few different xml files depends of constructor in run(int number) method.
After is download complete and all information from xml are saved in my local list i call delegateCompled. if (eventDownloadCompleted != null)
{
eventDownloadCompleted();
}
Then i have few different pages. All pages are used for display specific data from downloaded xml. So on this specific page I have method that is fired when "downloadClass" says it is complet.
XML_DynamicDataChat.delDownloadCompleted delegMetoda = new XML_DynamicDataChat.delDownloadCompleted(inicialiyaceListu);
XML_DynamicDataChat.eventDownloadCompleted += delegMetoda;
This is that "inicializaceListu" method:
private void inicialiyaceListu()
{
Dispatcher.BeginInvoke(() =>
{
model = new datka();
// object model is just model where i am saving all specific list of informations that i got from xml files.
chatList9 = model.getChat(1);
gui_listNovinky.ItemsSource = chatList9;
gui_loadingGrid.Visibility = Visibility.Collapsed;
});
}
All of these works fine, but when i go back (with back button) and open other specific page with other specific information from other downloaded xml, previous page is still listening for the delegate and inicialiyaceListu() method is still fired everytime i complete download of xml.
So i need to say previous page something like: "hey page, you are now closed! Can you shut the **** up and stop work?!?"
I think that specific delegate for each pages could solve this, but it is not correct programing way.
I solved it nice and easy. It is really simple solution. I just created bool variable and set it false when i go back. In inicializaceListu() i have condition if it is true. If it is true do that stuffs when false do nothing.

Highlighting code window text from Visual studio tool window using VSPackage

I'm kind off new to the Visual Studio Extensions. Is there a way to interact to code window from Visual Studio 2010 tool window.
I have a Datagrid hosted on the VisualStudio tool window. DataGrid contains ClassName, MethodName e.tc. On the click of className/MethodName need to acheive the following
Open the particular .cs file containing className/MethodName
HighLight the particular className/MethodName.
I know this acheivable using "IWpfTextView" class, but not sure how. Did a lot of googling but in vain.Even the link below remains to be un-answered link.
Any help on the above will be greatly appreciated.
I actually did almost the same thing. You can see complete source code on Visual Localizer.
Basically you need to do two things:
Obtain IVsTextView instance for the file
call its SetSelection() method which takes range (start line, end line, start column, end column) as parameters. You may also want to call the EnsureSpanVisible() to scroll down.
Obtaining IVsTextView is quite easy as well. In my project (Visual Localizer) there's a class called DocumentViewsManager, located in VLlib/components that has fairly straightforward method called GetTextViewForFile(), which takes only file path as an argument. It does the following:
use VsShellUtilities.IsDocumentOpen method to obtain IVsWindowFrame for given file path
pass this to the VsShellUtilities.GetTextView method, which returns IVsTextView
Hope it helps.
Thanks cre8or.
I found an alternative to do the same.
You need to use "TextSelection" class to highlight the above code line.
foreach (CodeElement codeElement in projectItem.FileCodeModel.CodeElements)// search for the function to be opened
{
// get the namespace elements
if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
{
foreach (CodeElement namespaceElement in codeElement.Children)
{
// get the class elements
if (namespaceElement.Kind == vsCMElement.vsCMElementClass || namespaceElement.Kind == vsCMElement.vsCMElementInterface)
{
foreach (CodeElement classElement in namespaceElement.Children)
{
try
{
// get the function elements to highlight methods in code window
if (classElement.Kind == vsCMElement.vsCMElementFunction)
{
if (!string.IsNullOrEmpty(highlightString))
{
if (classElement.Name.Equals(highlightString, StringComparison.Ordinal))
{
classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
// get the text of the document
EnvDTE.TextSelection textSelection = window.Document.Selection as EnvDTE.TextSelection;
// now set the cursor to the beginning of the function
textSelection.MoveToPoint(classElement.StartPoint);
textSelection.SelectLine();
}
}
}
}
catch
{
}
}
}
}
}
}

Automating Website/Web forms using C# in Firefox, using Visual Studio 2010?

If anyone can help me I'd appreciate it.
I'm working on a C# file in Visual Studio 2010 that I need to be able to test a website with multiple form pages, for the purpose of an example we'll refer to them all as 1.aspx, 2.aspx etc.
I've my code that fills out the first page (1.aspx) fine, and click the "continue" button to load the next page, but when it gets to 2.aspx it won't continue to fill out the form.
We'll say an element on the 2.aspx page is called "DOB". On trying to run from the start (I've all the pages form data in the one .cs file) I get an error like "DOB does not exist in the current context".
Anyone's insight into this would be really appreciated!
In all honesty, it sounds like you might be better off using the WatiN Framework. I have been writing automation with it for years and the way that it is implemented and its ease-of-use make it worth the slight learning curve.
Just to add a bit more to the answer; and yes, this is pseudo-code:
[Test]
public void Should_attach_to_browser()
{
ExecuteTest(browser =>
{
browser.GoTo(NewWindowUri);
browser.Link(Find.First()).Click();
var findBy = Find.ByTitle("New window");
var newWindow = Browswer.AttachTo(browser.GetType(), findBy);
newWindow.Close();
});
}
In the code above, note the Browser.AttachTo(browser.GetType(), findBy); method. Based on what I have understood of your question, the .AttachTo() method would work well since you would be able to take the focus off the current form and assign it to the next in your work/execution flow.

How to open chm file from a button in VS Lightswitch

What sort of code do i need to put in a button's can_execute() method in order to open a .chm help file i made for my lightswitch application and how will i make sure that even after publishing the whole application... the .chm file is transferred into the user's computer automatically and it stays in the right directory? ..... Thanks!
partial void Help_Execute()
{
// Write your code here.
if (AutomationFactory.IsAvailable)
{
dynamic shell = AutomationFactory.CreateObject("Shell.Application");
shell.ShellExecute("C:/Users/Thuto/Documents/Visual Studio 2010/Projects/ElectricalContructors/ElectricalContructors/Client/Resources/SparkHelpDocumentation.chm", "", "", "open", 1);
}
else
{
this.ShowMessageBox("Automation not available");
}
}
After a hours of trying to find a better way i tried this and it worked but i want to know the proper way of doing it, thanks!
Here is an article about making images embedded resources on the Client side. I haven't tried it with a .chm but I don't see why it shouldn't work.
http://blogs.msdn.com/b/bethmassi/archive/2011/06/15/adding-static-images-and-text-on-a-lightswitch-screen.aspx

Categories

Resources