Opening new tab using a link with querystring variable? - c#

First of all, I am entirely new to web development, so I apologize in advance if this question is overly simple (although I did do the prerequisite googling before I posted).
The problem I am having is that I would like to open a new tab via a link button or link on my page. I need to append a query string variable from the page onto the end of the reference, because it is passing a parameter to a report.
I've successfully passed the parameter and opened the report in the same tab using this code:
protected void lbSummary_OnClick(object sender, CommandEventArgs e)
{
Response.Redirect("http://myreportserverURL&rs:Command=Render&Year="+YearID);
}
And I've successfully opened the report in a new tab without passing the parameter with this code:
Report Name
I would prefer to do both. One important note is that opening a new window, instead of a new tab, is not what I need. I do understand that this is somewhat dependant on browser use, but for this project I can assume that users will be on IE8.
Is this possible? Any suggestions would be greatly appreciated.

You can use asp.net HyperLink control where you can set it's NavigateUrl in code-behind to whatever link you want including your querystring in there.
You can set it's Target Property as per you need.

Set target="_tab" this will open link in new tab

Related

Holding on to data passed from page to page - WP7 and C#

I'm creating a WP7 application using C#, and I require to pass data from one page to the other.
I found solutions on SO, but I'm still running into problems.
On 'Page 1', I wish to display a list, that can be populated by the user, using input from 'Page 2'.
I used the following statement in 'Page 2' while navigating back to 'Page 1': NavigationService.Navigate(new Uri("/MainPage.xaml?text="+WhoBox.Text, UriKind.Relative));
WhoBox is a Text Box.
On 'Page 1', I have the following:
protected override void OnNavigateTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("text"))
ListBlock.Text = ListBlock.Text + NavigationContext.QueryString["text"];
}
Now, this works, but in a limited fashion. If I try adding something from 'Page 2' for a second time, it replaces what is present in ListBlock (which is a Text Block) with the newly added text instead of appending it.
Shouldn't ListBlock.Text = ListBlock.Text + NavigationContext.QueryString["text"]; cause the new text to be appended, rather than to entirely replace the older text?
EDIT: I may have found the solution. For whatever reason, no changes in the XAML or .cs file are reflected when I run the program using F5. Am I doing something wrong? For example, even if I delete a button, it still appears when I Debug (F5) the program. Is there some setting I need to change? Or am I supposed to use some other command? I'm relatively new to Visual Studio, so please excuse me.
The problem is that the moment you again leave your page 1 it is basically disposed of. Meaning any text that was set in the Listbox is also removed. You will , in other words, need to save the state of that page before leaving it.
There several possibilites here:
Use AppSettings (see Windows phone 7 config / appSettings? )
Write the state to a local database
Do a quick'n dirty fix by saving the Text in the App.xaml.cs which all pages can work with: First you need to create the application-wide variable (and initialize if needed) inside the app.xaml.cs file. For example:
public partial class App : Application
{ public string myText;
From now on you can reach any App-variable through the Application.Current object. So if you need to access bigVar from some page in you application (e.g. MainPage) you simply type:
string Text = (Application.Current as App).myText;
Consider using sessions and datatable: Storing and retrieving datatable from session

create hyperlink from text

I am attempting to create a hyperlink from an existing url that I would like to 'share' with others. What I mean to say is that I am creating a 'share page' option for my phone app and I pass the current url via querystring to my SharePage.xaml, whereby the user may select an option to share the current url that the webbrowser control is on. For instance, in my SharePage.xaml.cs my code is as follows:
SharePage.xaml.cs
string urlToShare;
public SharePage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//base.OnNavigatedTo(e);
NavigationContext.QueryString.TryGetValue("curUrl", out urlToShare);
}
private void SocialNetworks_Click(object sender, RoutedEventArgs e)
{
ShareLinkTask shareLinkTask = new ShareLinkTask();
Uri shareUrl = new Uri(urlToShare);
shareLinkTask.Title = "Shared Link!";
shareLinkTask.LinkUri = shareUrl;
shareLinkTask.Message = "Check out this link!";
shareLinkTask.Show();
}
As of now this works although the LinkUri part of the message shows up as plain text instead of a hyperlink (which is what I would like to give as an option). The purpose would be to simply facilitate more efficient, quicker navigation to a url so that the user does not have to copy and paste the url into a web browser manually (something I've found annoying on the Windows Phone). Is there any way to do this in code behind in my SocialNetworks_Click event? Any code help or suggestions would be greatly appreciated, I have never messed with the Hyperlink option in C# as I am new to the language (and cannot find anything online about doing this in code behind if thats possible). Thanks in advance!
I think you're confused about what the ShareLinkTask is supposed to do.
This isn't meant to be displayed as a link in your app, or even in the task UI.
On the "Post a link" page this will be just text (and not tappable).
When the link appears in Twitter or Facebook or LinkedIn or whatever else you're sharing to then it will be a valid link that can be tapped/clicked.

Opening a browser and filling in some data

i want to know how i can open a browser to a specific web page and then fill out some of the content of the boxes on that page.
My idea is for someone to be able to order a particular item from our internal ordering system. The barcodes for these items are what will populate the fields on the page i want to open.
I no i can open a new instance of ie using Process.Start("IEXPLORE.EXE", url); howver how do i get a handle on that exact ie instance window so i can begin to add the required data to the fields?
Is this even possible?
Thanks very much
WatiN should help with this. I've generally used it for acceptance testing of web apps, but the principle is the same. Open a browser instance, reference stuff in the DOM, manipulate form elements, etc.
In addition to WatiN (as was suggested in another answer), you might consider a load testing package like Web Performance Load Tester. They have a free version that lets you run up to 10 virtual users at a time, which will perform scripted actions.
Another option would be to use a standard WebBrowser object to load your website. The WebBrowser object allows you to access and alter certain web parts. Below is sample code that automatically searches Bing:
private void LoadPage()
{
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate("http://www.bing.com");
//Wait for document to load...
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
//Set the text of the search input
HtmlElement txtTextField = webBrowser1.Document.GetElementById("sb_form_q");
txtTextField.InnerText = "My test text";
//Perform a click on the search button
HtmlElement btnSubmit = webBrowser1.Document.GetElementById("sb_form_go");
btnSubmit.InvokeMember("click");
}

navigation menu doesn't redirect to the given value

the problem i encounted concerns redirecting in the navigation menu. I'd like to dynamicly create a navmenu. Depending on what role the user has we get to see the required navigation menu items.
At the moment i use:
if (found)
{
if (admin == true)
{
NavigationMenu.Items.Add(new MenuItem("Agenda", "/AdminPages/Agenda.aspx"));
NavigationMenu.Items.Add(new MenuItem("Add Product", "/AdminPages/ProductToevoegen.aspx"));
}
else if (user == true)
{}
This code I have placed in my Site.master.cs, but I also have a control in my login.aspx.cs code which does a Response.Redirect("~/AdminPages/Agenda.aspx"); to a certain page depending on admin or user once logged in. Now the problem I have is that when I log in, a part works, so it controls the role and adds the required navigation menu items. But when I click for example on the Add Product link it doesn't redirect me to the page. It keeps redirecting me to :
http://localhost:52853/AdminPages/Agenda.aspx
In the url bar it actually shows the url followed with a # when clicked and than redirect to the Agenda page.
Any ideas on how to fix this problem? I tried finding a way to put the navigation links in the login.aspx.cs code aswell but couldn't find the correct way to refer to the NavigationMenu, don't know if that could be off any help. Thank you in advance.
Look at the parameters for creating a new MenuItem.
When only passing 2 parameters, you are filling the text and value parameters. What you want to pass is a navigateUrl parameter. This is only available when passing an imageURL parameter (which can be empty).
Something like the following should fix your problem.
NavigationMenu.Items.Add(new MenuItem("Agenda", "", "", "/AdminPages/Agenda.aspx"));
Hope this helps.
I would suggest using a standard siteMap object and RoleProvider. And read a little bit about security trimming (you can specify on your sitemap nodes which roles can access which sites).
Hm might not be the correct way but i fixed it making another menu in the site.master.aspx file called MenuAdmin for example. Than in the code behind i've set the
NavigationMenu.visible = false;
and
MenuAdmin.visible = true;
Seems to work fine, does what it needs to do for now ;)

ASP.Net Hyperlink navigation URL not including server name

This might be a ridiculously easy question, but it has me stumped. I have a web form where I'm trying to create a hyperlink in the code behind to a file server share, e.g. file://myServer/Shared/, but when the page is rendered, the link doesn't include the server name, i.e. file:///Shared/. I don't know why this happens. Any help or insight is appreciated.
UPDATE:
Sure, here is the snippet where the link is being set.
//The link is embedded in a table
HyperLink link = (HyperLink)e.Row.Cells[1].Controls[0];
link.NavigateUrl = #"file://myServer/Shared/";
As a test, I assigned the link to a string value and the link prints the expected url.
string foo = link.NavigateUrl;
//Displays this
"file://myServer/Shared/"
I don't know why this doesn't appear when the link is rendered in the final page.
UPDATE 2:
Ok, so I know I have to set the absolute path in the code-behind, I thought that's what I was doing, but it still won't render correctly.
UPDATE 3:
I followed pjacobs suggestion about setting the test property and it was actually a step in the right direction. I have the following:
link.Text = "link text";
Now the link gets rendered as follows: file:///myServer/Shared. I'm almost there except it gives the extra '/' in front of the server name. I'll keep playing with it, this seems like it should be so simple, I don't understand why ASP.Net renders the URL differently.
You have to set the Text property of the HyperLink... link.Text = "whatever"
Are the resources inside the project? If so:
you need to use ResolveUrl to resolve the "web location" of the resource.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx
if you're using an asp.net control you shouldn't need to use the resolve url, but you need to refer to the location of the file relative to the path of the project.
If not:
Did you give the proper read account to ASP.NET process?
Use a virtual directory?
http://www.dotnetspider.com/tutorials/AspNet-Tutorial-86.aspx

Categories