Adding a condition in the url - asp.net

I would line to add a test condition in an asp.net form such that:
1) From page1.aspx I manually add a query string parameter so that I can trigger the rest of the process in test mode like so: page1.aspx?test=true . This flag must be added in the query string.
2) When I click on a asp.net button in page1.aspx, I am redirected to page2.aspx in test mode
because of teh attached querystring
It seems that I have to work around the postback model of asp.net this is not very straight forward.
Any idea how I can achieve the above behavior?
Thanks

It sounds like you're using a form that posts, but you want to stay in "test" mode. That is, you're not using HTTP-GET so it's not realistic to pass QS variables around.
What I'd do is stash a variable in your Session to set the user's session test mode. So adding &test=true would trigger a Session["TestMode"] = true; before you move to the next page.

Try this (in server-side code)
Response.Redirect("Page2.aspx?Test=" + Request.QueryString["Test"]);

Related

Send query string parameter from a non-web application

Ok, I've been bugging with this for too long.
I need to call my website from a rough VB.net app. Then only thing I need is to attach a query string parameter to the calling url, so I can distinguish the pages to be shown to different VB app users.
So I want to click a button and launch that website, giving this parameter.
First I was bugging with adding the system.web libraries. Now I can't use Request/Response.QueryString as well.
I tried getting some example help from this post. but as I said before - I cannot make use of Request.QueryString as I cannot import it.
I am stuck here:
Process.Start("http://localhost:56093/WebSite1?id=")
I need to attach a query string parameter to the url and then open the website with that url.
Can someone just give me a sample code for my problem.
Query parameters are parsed by the web server/http handler off the URL you use to call the page. They consist of key and value pairs that come at the end of the URL. Your code is nearly there. Say you needed to pass through the parameters:
ID = 1234
Page = 2
Display = Portrait
Then you'd turn them into a URL like this:
http://localhost:56093/WebSite1?ID=1234&Page=2&Display=Portrait
Therefore in your code you'd have:
Process.Start("http://localhost:56093/WebSite1?ID=1234&Page=2&Display=Portrait");

Pass parameter from 1 page to another using post method

I have a page "Demo.aspx". I need to set some parameters using post method and redirect the page to "DemoTest.aspx".
Is there any way to set parameters in post method in asp.net? I don't want to set "Querystring" due to security propose.
Also I need server side code for this. I am not able to use "Javascript" or "Jquery" for the same.
Here is some more description for the same.
Right now I am using Response.Redirect("www.ABC.Com/DemoTest.aspx?P1=2"). So the page is simply redirect to the given URL.
Now I don't want to pass that "P1" in "Querystring". Instead of query string I want to use Post method.
Please note that redirection page is not in my own application. So I cant maintain session or "Viewstate".
Thanks in advance.
Use a session variable and response.redirect to the next page.
Session["MyVariable"] = "someThing";
Response.Redirect("DemoTest.aspx");
The value stored in Session variables will be accessible across application.
you can store in session like this :
Session["id"] = "anyID";
To get values On another page you need to write
string id = Convert.ToString(Session["Id"]);
However
By default, in .NET pages post() do the things automatically.
You will need to do sumthing like this:
Server.Transfer("DemoTest.aspx", True)

Classic ASP HTTP Post from another server

I am litte confused, i want to protect one page in classic asp from being accessed by Get Method.
Is it possible that someone can post data from another server to my page?
If Yes, how to detect that and allow only post from my server.
Thanks for help.
If you are currently using Request("ParameterName") to retrieve parameters then you should change to Request.Form("ParameterName") which will only get the parameter if it was POSTed.
Alternatively you can lookup the method used to access the page from the Request.ServerVariables collection and end the script if it is not POST. Here's an example:
If Request.ServerVariables("REQUEST_METHOD") <> "POST" Then Response.End
I noticed that you also said that you want to accept posts only from your server. The above changes will still allow another webpage to be set up to POST to your page. If you want to ensure that only your web page can post then you will need to add some more protection. Here's one way of doing it.
1) When you render your form create a random numbers and create a session variable named by the random number with a value to check for later.
Randomize
strVarName = Int((999999 - 100000 + 1) * Rnd() + 100000)
Session(strVarName) = "Authorised"
2) In your form add a hidden field with the value of the random number.
<input type="hidden" name="varname" value="<%= strVarName %>" />
3) In the script that processes the posted form get the value of the hidden field.
strVarName = Request.Form("varname")
4) Check that the session variable is set and has a value of True.
If Session(strVarName) <> "Authorised" Then
'Failed! Either show the user an error message or stop processing
Response.End
End If
5) Remove the session variable so that the same form cannot be resubmitted.
Session.Items.Remove(strVarName)
You don't need the random number but using it means that the same user can have multiple forms open in different windows/tabs and each one will work.

adding/setting a cookie vb.net

I know how to add / set cookies in VB.Net , usually I check if it is null ( or Nothing in VB) if it is nothing I set a new cookie , if it is not then I set the value of the previous cookie. My question is there any thing wrong , or any down side to just adding a cookie every time like this:
HttpContext.Current.Response.Cookies.Add(New HttpCookie("Lat", dt(0)(1).ToString().Trim()))
I'm hoping that this will just override the previous Cookie("Lat") if it exists , and set a new one if it doesn't , if this works it will really make my code a lot shorter and make things easier. I don't see why this wouldn't work - but every tutorial and example online normally checks if it exists first.
If you want to add (duplicate) cookie into the collection then use Add method and want to update an existing cookie then call the Cookies.Set method. (Reference MSDN)

ASP.NET ReportViewer Parameter Prompt doesn't show

I've been looking for this answer for a couple days now and I've been getting little bits of information that make it seem that you can have ReportViewer Control automatically prompt for the report's parameters. Just everything that I've tried and found doesn't seem to work. I've gotten the Parameter Prompts to work on a Windows Form but I just cannot get it to work in ASP.NET
I guess I'm simply asking can you get Report-viewer's Parameter Prompts to work in ASP.NET? if so, How?
I know you can do it manually, it's just, I feel if you can make ReportViewer Prompt automatically why program it yourself?
Edit: this is for local processing btw.
Prompting for parameters is not supported in local processing mode.
In article Report Parameters Dialog Box (Visual Studio Report Designer), which is invoked by clicking the Help button on that dialog, it says in the introductory text that:
The parameters properties that you specify in the Report Parameters
dialog box become part of the report definition. Some properties are
intended for programmatic use only. In contrast with reports that are
processed on a remote report server, a locally processed report does
not have a parameter input area used for selecting or typing parameter
values.
A little testing shows me that the default values specified for the parameters will be used, unless you modify them programmatically. I could not find an explanation on this design decision. If you want to use local processing, and prompt for user input, I would recommend to follow this solution:
I you embed the reports in a ReportViewer Control, you can put it on a page or form and add custom input controls to that page or form to gather report parameters. In the code-behind files, you will then pass the parameter values using code like this:
List<ReportParameter> parameterList = new List<ReportParameter>();
List<string> selectedProductTypes = listboxProductTypes.GetSelectedValues();
ReportParameter productTypes = new ReportParameter("ProductTypes", selectedProductTypes.ToArray(), false);
ReportParameter username = new ReportParameter("Username", "<current user>", false);
parameterList.Add(productTypes);
parameterList.Add(username);
reportViewer.LocalReport.SetParameters(parameterList);
In this example, you can see how to pass a multi-valued parameter, the values of which are taken from a multi-select ListBox.
You can also create a page that has the controls to gather the parameter input, put them in a set session variables and then transfer to the page that has the report viewer.
use object data source and set the parameter source as Session.
The only code you need to write is filling the session variables.

Resources