Ok so while back I asked question Beginner ASP.net question handling url link
I wanted to handle case like this www.blah.com/blah.aspx?day=12&flow=true
I got my answer string r_flag = Request.QueryString["day"];
Then what I did is placed a code in Page_Load()
that basically takes these parameters and if they are not NULL, meaning that they were part of URL.
I filter results based on these parameters.
It works GREAT, happy times.... Except it does not work anymore once you try to go to the link using some other filter.
I have drop down box that allows you to select filters.
I have a button that once clicked should update these selections.
The problem is that Page_Load is called prior to Button_Clicked function and therefore I stay on the same page.
Any ideas how to handle this case.
Once again in case above was confusing.
So I can control behavior of my website by using URL, which I parse in Page_Load()
and using controls that are on the page.
If there is no query in URL it works great (controls) if there is it overrides controls.
Essentially I am trying to find a way how to ignore parsing of url when requests comes from clicking Generate button on the page.
Maybe you can put your querystring parsing code into IsPostBack control if Generate button is the control that only postbacks at your page.
if (!IsPostBack)
{
string r_flag = Request.QueryString["day"];
}
As an alternative way, at client side you can set a hidden field whenever user clicks the Generate button, then you can get it's value to determine if the user clicked the Generate button and then put your logic there.
Related
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.
I have a button in my aspx page, which in code behind the value of postbackurl gets set. I updated the code to add click event handler and added Response.Redirect code to redirect user to the page once the button is clicked. The only reason i ended up adding click event was because i had to add some extra logic when the button was clicked including redirecting to same url, that was used in postbackurl. The page does get redirected on click however it seems like all the hidden fields from the field gets lost once the form get redirected to the url.
Is there anyway i can submit the form without loosing the hidden data.?
Maybe one way you can solve this problem is to use the server side Transfer() call.
http://msdn.microsoft.com/en-us/library/540y83hx(v=vs.85).aspx
In most cases I've seen what you really want to do is pass the information for the new page using URL parameters. Take all the stuff the new page needs and put it into the url (encrypt if it is security sensitive).
If the only issue you are having is when you want to stay on the same page, this is simple, just have the click event handler return without doing a response.redirect or a transfer.
Post the code if this is not working for you.
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.
I'm having trouble implementing a functionality on my c#/asp.net app.
I have a form with a RadioButtonList and a submit button.
The RadioButtonList is generated on Page_Load() from a list of objects I retrieve from the database.
I would like to automatically submit the form if there is only 1 object in the list.
I have access to my Form object, to the submit button etc... but I can't seem to find a solution (in the end I'm looking for a kind of form.Submit() ) method.
Does anyone have an idea of how I could do this ?
Thanks in advance !
EDIT >> Here is the code :
.aspx : http://pastebin.com/0E6T7dqH
.aspx.cs : http://pastebin.com/54payZJP
EDIT2 >>>
As it seems there is no way to do what I wanted to do at first, I ended up using a session variable with a response.redirect()
Source :
http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx
Post happens in the client side. As in Page_Load you are currently executing in the server side, just call the code you want to execute on post.
Edit: For actually going to another aspx
public void Page_Load(object sender, EventArgs e) {
if(!IsPostback && OnlyOneItem) {
Server.Transfer("TheOtherPage.aspx");
}
}
Server.Transfer will maintain the entire request, so your post data will be available.
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transfer.aspx
Try something like this
In your Page_Load
if(!IsPostBack)
{
if(check for only one object)
{
//your submit code
}
}
I actually had to do something similar, once. Here is a way you can do it.
Asp.Net buttons have a property called PostBackUrl, and it does exactly what you would expect - it controls where the form will post if you click the button.
You can also use the RegisterStartupScript function to render javascript on the page.
Now, with these two pieces, you can achieve your goal.
if(!IsPostBack)
{
if(results == 1)
{
button.PostBackUrl = "next page's url"
//Register script to click the button using RegisterStartupScript
}
}
Now, having shown you this, I will warn you it may not make for the best user experience. When I did it, it was for a very specific case that had no other solution. The page will actually post back to the user, and they will see the page for a moment before the javascript to click the button takes effect. Additionally, when you set a button's PostBackUrl, that means that when it is clicked, your entire form will be posted to the page specified. The code behind for the current page will not fire at all, so if you have any validation, it won't run.
There's nothing wrong with letting the user click the button to submit the form even if they only have one choice. In my experience, users like to feel like they are in control on the system; they don't like it when pages just do things without their input.
Also, there is not really anything wrong with putting the information the next page needs into the session or even a database table, and using Response.Redirect. It's a fairly common practice and works reliably in most scenarios.
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;
}