Passing querystring with HyperLinkField - c#

I have a gridview where one column is going to be a hyperlink that will open a new browswer window and display a file so upon clicking this link I am currently using the NavigateUrl and calling a new page and setting the target to blank. My problem is that in this new page I will need to know the ID of the row that was selected. How can I pass this information? Querystring doesn't seem to work?

Check out HyperLinkField's NavigateUrlFormatString property here.

Instead of using NaviagteURL use a click event, add a session variable, and response redirect to the page. Look for it in the page load on the new page.

Related

Is it possible to reload page Url without change request Url?

I have a page with a button that if clicked on it, a function is called that creates a pdf and it is opened in a new tab.
Now after clicking this button, I want to reload the current page then pdf file open in a new window.
How can I do this action without change request url?
I try to use below code but it change the url of new tab:
if (Request.UrlReferrer != null) Response.Redirect(Request.UrlReferrer.ToString());
I think you can use a simple JavaScript to reload current page completely
Have a look at this answer:
How to reload a page using JavaScript
You can use Request.UrlReferrer.ToString()

Passing Hidden Fields to another aspx page

I want to be able to pass data from one aspx page to another using hidden fields.
On pageone.aspx, in the page_load, I am saving data into a hidden field:
ClientScript.RegisterHiddenField("var1", "hello");
Then, the user clicks on a button on pageone.aspx, which redirects to pagetwo.aspx
Response.Redirect(Constant.AdminUser, true);
Then, in the page_load of pagetwo.aspx, I grab pageone's hiddenfield by:
Request.Params["var1"]
But nothing is returned.
For some reason, when I replace Response.Redirect with Server.Transfer, I get data in pagetwo.aspx, which is what I want. But the browser's URL does not change.
I want to be able to pass in data from pageone to pagetwo, without storing the data in the session variable, url, database, cache, etc. I would like to send it using hidden field.
MSDN has a full article on handling cross-page posting.
http://msdn.microsoft.com/en-us/library/ms178139.aspx
TL;DR: You have to register the previous page using the #PreviousPageType declaration, then call against the PreviousPage property.
I do something very similiar and all I did was erasing my Server.Transfer line and keep PostBackUrl on my button.
If you have them both, your URL won't change.

LinkButton or HyperLink?

I have a situation where I'm not sure if I should use a HyperLink or LinkButton. When a user clicks on a list of links I want to trigger a click event where I save some information to session (should use LinkButton) but I also want these links to open up new tabs (should use HyperLink).
A LinkButton will postback, it's essentially a button that renders like a link. You could set a response.redirect(url) in the event handler to set a new tab.
Can you add more information, of what you want to do in the handler, maybe this could be achieved with Jquery calling a server-side method?
Difference between Hyperlink and LinkButton
Click Api with Jquery and Jquery post.
You will need to use a LinkButton which causes a PostBack. To open additional tabs, emit JavaScript.
protected void MyLinkButton_Click(object sender, EventArgs e)
{
Session["MyData"] = 123;
Page.ClientScript.RegisterStartupScript(Page.GetType(),
"newWindow",
"window.open('http://myurl','_blank');",
true);
}
I would say a HyperLink that hits a specific URL to store the necessary session data, then use Response.Redirect to redirect to the following page after storing the information.
The HyperLink's URL would point to your server so that you can store the information, and then you would use a redirect to point the user to the correct endpoint after storing the necessary data.
Example
HyperLink's URL points to ~/yourpage.aspx?state=NY, with target="_blank"
Server responds to URL and checks querystring.
If query string exists, store data (if (Request.QueryString["state"] != null) Session["state"] = Request.QueryString["state"])
Redirect the user to the appropriate URL (Response.Redirect("http://www.ny.gov"))
If the data is at all confidential, then you would want to use the LinkButton methods pointed out in other answers. Opening up a new tab is tricky, so you would probably have to write out some Javascript as outlined in #andleer's answer since I don't believe there is an easy way to pop open a new window from the server side otherwise.
You need to use LinkButton.
The difference between the two is that LinkButton postbacks your page to the server allowing you to make your logic while HyperLink does not postbacks - just redirects you to the specified link, therefore, use HyperLink when you want to navigate.
The LinkButton control is used to create a hyperlink button. This control looks like a HyperLink control but has the same functionality as the Button control.
with LinkButton you also get the facility of Web Control Standard Properties and Control Standard Properties .
You're right, can't do both, since a LinkButton will trigger a postback, and a hyperlink will simply navigate to a new page.
In your case use you can use a LinkButton and the server code will have to to do a redirect (if you want to navigate to another page) or handle the Tabs and return the page with the Tab opened if you are using a tabs element. (so the tab navigation will not be done front end)
In such cases you can use button for saving some information and you can also use it as a like option. But if you want to add a link then you have to use hyperlink.. You can also use javascript to link a url with the button so that when user clicks on that button the session information will get stored and then he will redirected to the new webpage.

Reload a Web Page Using C#

I have a web page that prompts for user input via DropDownLists in some table cells. When a selection is made the selection replaces the DropDownList so that the action can only be performed once. In the case that a change needs to be made I want to be able to click a button that reloads the page from scratch. I have Googled and Googled but I have not managed to find a way to do this.
Any advice is appreciated.
Regards.
Put a link on the page with the text "Reload" and the url the url of the page. It's perfectly valid to have a page with a link to itself.
If you don't like the link idea, use a standard Button and in the click event, use Response.Redirect to redirect to the current page.
You can set an OnClick for your button that resets each DropDownList's SelectedIndex to 0 instead of reloading the page from scratch. Alternatively, you can set a Response.Redirect([the page's url]) into the OnClick as is suggested here.

how to find selected hyperlink in asp.net using C#

I have a list of 10 hyperlink on default1.apx. On selecting any hyperlink it redirects to another page and all hyperlinks redirects to the same page default2.aspx. But how can i now which hyperlink is clicked from 10 hyperlinks list in asp.net using C#.
There are a couple of ways, all depending on how you're doing your redirect.
I'm guessing you're using Response.Redirect(), which means you must be raising a server side event when clicking. In this case all you need to do is check the sender argument which will give you details of which control was clicked.
Another way is to append the controls' name onto the URL in a GET request. So that each link is slightly different. For example link one could point to default2.aspx?linkthatwasclicked='link1' where you can substiute link1 accordingly, this value can then be retreived on default2.aspx through the Request.QueryString object.
m.edmondson has got it, another way I've seen is having a "hyperlink.aspx" page which then requests an id:
Link 1
then in the hyperlink.aspx code:
int id = int.Parse(Request.QueryString["id"].ToString());
switch(id)
{
case 1:
// Add a count to a table maybe
// Get the url from the DB here...
string url = GetUrlById(id);
Response.Redirect(url);
break;
}

Categories

Resources