get special value in url - asp.net

url:http://localhost:51806/fair/PersonPage/personalPages.aspx?idCompany=1338006699#Site/AboutAs
request["idCompany"];
this code return null
how can get value idCompany
EDIT
Request.UrlReferrer.Query
this return ?idCompany=1338006699
this Request.UrlReferrer.Query.Split('=')[1] return 1338006699
but i think this way does not good way
#Site/AboutAs is a tab aboutAs in full tab component

Try this instead:
string id = Page.PreviousPage.Request.QueryString["idComapny"];
If no luck then your method of splitting is the best you can achieve, as you're trying to read the query string of the referrer page.
One work around though is to store the value in that previous page.
To do this, store the value of Request["idComapny"] in the previous page, where it should be available, in Session then you can read the Session value in any other page.

Related

IndexOutOfBounds when executing a NamedQuery twice

I'm trying to make a web application for the first time, and I use all kinds of tutorials and help of any kind, but I don't get why this happens. Everything worked all right until now:
I'm trying to transmit a "User" attribute between servlets, and I'm doing so by sending part of it as an attribute (using RequestDispatcher or HTML forms), and looking up the rest of it in a database, like this:
String user = (String) request.getAttribute("txt");
Users info = (Users) emf.createEntityManager().createNamedQuery("Users.findByUsername").setParameter("username",user).getResultList().get(0);
Username is Unique key, and the code for the NamedQuery is
#NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username)
The first time I use this, it works and I get the expected result, but, if I come back to the same servlet or I use the same code again in other servlet, I get java.lang.IndexOutOfBoundsExpcetion: Index: 0, Size: 0
How can this happen if I didn't modify the database at any moment?
Any help would be appreciated.
Seems like your request attribute "txt" is null the second time. It's a request attribute, so it will be only valid during the request. If you don't store it or submit it every time it will be null.
A null as username will produce an empty list. The attempt to read the first element of an empty list produces the IndexOutOfBoundsException.
Use a session attribute or resubmit the attribute every time and it will work.

Pager numbers do not match URI string in drupal 7?

How can I make the pager numbers match the URI string?
For example : When I click on number 3 in pager to turn to page 3 but the the URI is "?page=2" instead of "?page=3". This pagination isn't normal as any other pagination I've met before. How can I fix it?
Thanks!!!
You could do it by implementing hook_url_outbound_alter(), and hook_url_inbound_alter().
With the first hook, you alter the links that are being output from Drupal; you would increase the value of $options['query']['page'].
With the second hook, you alter the links being received from Drupal; you should find the '?page=' string in the URL, and decrease the number after that string.
Keep in mind that $_GET['page'] can be a string like '1,2,4,5,6' if the page has more than one pager. See the code of pager_find_page().
Also, hook_url_outbound_alter() receive the query parameters in $options, while hook_url_inbound_alter() receives just a string for the URL, which also contains the query part.
The page attribute in the URL is used directly in the query, so if ommitted, page will get 0 as a value. It's normal because Drupal do pagin things like this.
Now if you need to change this, you have to work arround query alters for every query, to add +1 to the limit() or range() methods.

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)

Cleanest way to hide password input fields?

We have some error reporting code that, when an unhandled exception occurs, we send everything over in an email to our groups. This is great except if an unhandled exception occurs on a page with a password field then it's sent over as plain text.
Is there a way to iterate through Request.Form and figure out which item(s) are passwords? This is done at a low level so we can't look for specific controls.
Naturally, we could check to see what type the input box is but I'm not sure if that's the cleanest way. Advice?
Use a whitelist of field names that you want to email.
There could be hundreds of field names that get POSTed to your server. And password isn't the only field that is sensitive. Depending on your application, there could be other things that should be treated with a little respect.
So, make a list of field names that will assist in you in debugging. These are typically unique identifiers / database keys and such. If you have any parameter names in this list, you can include it in the email.
I've suggested a different solution earlier, but I thought you were going to handle this on the client side. Following your comments I now understand that you need to take care of this on the server side. There may be a way for you to do it, which is not really elegant, but it should work:
Add to all pages a script that collects all password field names into a new client-generated field, like so:
function collectPasswordFields() {
var inputs = document.getElementsByTagName('input'), list = [];
for (var i = 0; i < inputs.length; ++i)
if (inputs[i].type == 'password') list.push(inputs[i].name);
var field = document.createElement('input');
field.name = '__password_fields';
field.value = list.join(',');
document.getElementsByTagName('form')[0].appendChild(field);
}
Then intercept the additional field in the server-side error handler, and remove the named fields from the email.
Can something like this work for you?
The cleanest way is to check the type attribute of the input element.
The HTML5 specification has this to say about input type=password:
The input element with a type attribute whose value is "password" represents a one-line plain-text edit control for entering a password.
Data type: Text with no line breaks (sensitive information)
Control type: Text field that obscures data entry
This is a mandatory requirement from all User Agent implmentations, and it has been so since HTML 2. So this is indeed the cleanest way to do what you want.
If you want to do it on the client side (you talked about sending the data to the server) then it is relatively easy:
function hidePasswords() {
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; ++i)
if (inputs[i].type == 'password') input[i].value = '*****';
}
As Jerome already pointed out in the comments, just keep track of the names of your password input fields and filter them before sending the error/exception report. This is the best solution as the type of the input field is not submitted.
A few solutions, though I'm not sure how bright any of them is:
1) Maintain in the page a List of input control IDs that are passwords, pass this list to the exception handler with the expectation to ignore these fields.
2) Keep a resource file in the website that lists a page name, a field id and have the exception handler check against this resource file (may not work if the exception is related to the ResourceManager)
3) Keep a database table as with idea 2. Same problems exist.

How do I identify the referrer page in ASP.NET?

In VS2003, I am trying to find out the particular page where the request is coming from. I want to identify the exact aspx page name.
Is there a way to only get the page name or some how strip the page name?
Currently I am using the following instruction...
string referencepage = HttpContext.Current.Request.UrlReferrer.ToString();
and I get the following result...
"http://localhost/MyPage123.aspx?myval1=3333&myval2=4444;
I want to get the result back with out any query string parameters and be able to identify the page MyPage123.aspx accurately...
How do I do that??
Instead of calling .ToString on the Uri, use the AbsolutePath property instead:
string referencepage = HttpContext.Current.Request.UrlReferrer.AbsolutePath;
This should get you "/MyPage123.aspx" in your case.
Edit: Had LocalPath instead of AbsolutePath by mistake
Look at the Segments property of the URI class (which is what HttpContext.Current.Request.UrlReferrer returns).
Something like HttpContext.Current.Request.UrlReferrer.Segments[1] (changing the 1 indexer to get the correct segment you require).

Resources