I think that this is something very easy, but I cannot get this to work.
I have a form with a button "Delete". It calls www.mypage.com/adm/ads.asp?del=12.
So list.asp sees that there is a querystring with del=12 and deletes correspoding item.
After delete I want to refresh this page (something like Response.Redirect www.mypage.com/adm/ads.asp), so that querystring del=12 disappears.
I cannot get it working.
If (Request.QueryString("del").Count > 0) Then
id = Request.QueryString("del")
sql = "delete from Ads where ID = " & id & ""
on error resume next
conn.Execute sql
If err<>0 then
Response.Write("Delete error!")
Else
Response.Redirect http://www.mypage.com/adm/ads.asp
//Call opener.location.reload()
End if
The page is reloaded, but del doesn't disappear from query string.
The parameter to Response.Redirect should be a string - what you have is a syntax error:
Response.Redirect http://www.mypage.com/adm/ads.asp
Should be:
Response.Redirect "http://www.mypage.com/adm/ads.asp"
To make it generic and not mess with raw URLs you can have such code instead:
Response.Redirect(Request.ServerVariables("SCRIPT_NAME"))
The SCRIPT_NAME server variable will return the relative path of the currently executing script, no matter what the page is called and where it's located.
Related
Situation:-
There is a Home.aspx page, which can be opened by a unique user ("userName" variable).
This page has a popup window control name 'alertWindow'.
In the pageLoad event of Home.aspx.cs, Welcome.aspx page is opened in the 'alertWindow' using NavigateUrl property.
The querystring passed to Welcome.aspx page contains a parameter "UserName" and this parameter is set to the logged in user's name ("userName" variable).
Now when the code execution comes to Welcome.aspx.cs page, "Request["UserName"]" is used to get\retrieve the current "userName" paramerter existing in the query string.
Issue:-
When a logged-in user's name has space or other non-usual characters, then "Request["UserName"].ToString()" doesn't retrieve the actual and correct value.
For Ex. if the logged in "userName" = "A&T Telecom", then "Request["UserName"].ToString() retrieves only "A" and nothing else.
But if the userName string is a proper value like "micheal", then "Request["UserName"].ToString() retrieves only "Micheal" correctly
Requirement:-
Please provide a way so that I get the correct value from Request["UserName"] for any kind of "userName" string value.
Home.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (user is valid)
alertWindow.NavigateUrl = "Welcome.aspx?userName=" + currentUser.ToString();
}
Welcome.aspx.cs:-
currentUserName = Request["userName"].ToString();
This is logical because you do not Encode your url. Try this:
alertWindow.NavigateUrl = "Welcome.aspx?userName=" + Server.UrlEncode(currentUser.ToString());
To say few more, they are some special characters that used on the URL, like the
: / # ? & # % + (and the space).
All that characters must be encode to a different format, so the url will not break, the UrlEncode do exactly that.
Two notes.
I select the Server to call the UrlEncode because is not depend from the Request, and you can use it inside a thread, or any function that is not called from the Page.
The Request.QueryString make UrlDecode when you use it. To get the encode url you call the Request.RawUrl
You cannot add white spaces within your url, it needs encoding so :
//uses HttpUtility.UrlEncode internally
Server.UrlEncode("something with spaces");
or
HttpUtility.UrlEncode("something with spaces");
I have three ASPX Pages. The ASPX Page which get called from the two other Page is a Page where i can Upload files.
I call that Upload-Page from the other two Pages with JavaScript:
function UploadFax_Click() {
var grid = ISGetObject("WebGrid1");
var curSelObj = grid.GetSelectedObject(); // get selected object
var row = curSelObj.GetRowObject(); // get the WebGridRow object
if (row.Type == "Record") {
var s_id = row.KeyValue
}
window.location = '../Admin/UploadFax.aspx?suppid=' + s_id;
}
Then on the Upload-Page i get the QueryString:
If Not IsPostBack And Len(Request.QueryString("suppid")) > 0 Then
If Not Request.QueryString("suppid") Is Nothing Then
Session("suppid") = Request.QueryString("suppid")
End If
End If
What i need is that if i call the Upload-Page from one of the two Pages the functions of the Upload-Page should be restricted.
For Example: If i call the Upload-Page then Checkbox.Disabled = true and from the other page Checkbox should be enabled.
My idea was to send a second parameter from that page, then get the parameter with Request.QueryString and then use if/else to enable oder disable that checkbox.
Question is, is there another, better possibility to do that what i want? If yes how can i do that?
Using QueryString may cause a security violation , Why not you use Session parameter,
when you Call it from the First Page then set
Session("IsCalled")="1"
in the second page load check
IF Not Session("IsCalled") Is Nothing AndAlso Session("IsCalled")="1" Then
CheckBox1.Enabled=False
End If
and Vise Versa in the first page load.
You can check the Request.UrlReferrer and dance from it:
Dim restrictedAccess As Boolean = Not IsNothing(Request.UrlReferrer) AndAlso
Request.UrlReferrer.AbsolutePath.IndexOf("/Original.aspx", StringComparison.InvariantCultureIgnoreCase) >= 0
Checkbox.Disabled = Not restrictedAccess
Basically I have a page named dvds.asp with a form that goes to action="process.asp". The process.asp script validates the Name, Surname and Email fields. I would like that if the email does not exist, a redirect will occur to the original page (dvd.asp) and also display a custom error.
The validation of the email field and redirect I know how to do. The only problem is how I am going to get the custom error value in the dvds.asp.
Any help please? :)
FYI:
I am using this code in process.asp to validate:
if txtEmail = "" then
Server.Transfer(Request.ServerVariables("HTTP_REFERER"))
else
Now the error I want to show on the dvds.asp page is: "Please fill in your email" .. How I can do that please?
Hard to say without seeing any source, (For instance, how are you validating the email? What is this "Custom error value"?
Assuming you have a function that validates the email and returns false if it fails and the matching error value is "1" then you could simply include this in your redirect:
Dim EmailAddress
EmailAddress = Request.Form("EmailAddress")
If ValidateEmail(EmailAddress) = False Then
Response.Redirect("dvd.asp?ErrorID=1")
End If
and on the Origional Source page, you could just display errors back to the user as follows:
'#### Output errors
If Trim(Request.QueryString("ErrorID")) <> "" Then
Select Case Request.QueryString("ErrorID")
Case "1" ' #### Email Error
Response.Write("<p><span style=""color: red"">Error: Invalid email address</span></p>")
Case "2" ' #### Bad Name
Response.Write("<p><span style=""color: red"">Error: Invalid Name</span></p>")
Case Else
Response.Write("<p><span style=""color: red"">Error: Unknown / Generic Error</span></p>")
End Select
End If
One small suggestion by the way, its often preferable for pages such as this to post to the same page and include any email functionality you need from a class / SSI. That way you cold auto-populate the form again if an error occurred rather than expecting your end user to re-type the whole form if they get an error.
This the end of my code
...
If lblErrMsg.Text = "" Then
Response.Redirect("UserPage.aspx")
End If
I want to pass the value of txtUser(I create It in the current page...) to the UserPage.aspx.
Thank's for helping me ...
This is in VB.net not in c# Please
C# Version
1) Use querystring
Response.Redirect("user.aspx?val="+txtBox.Text);
and in userp.aspx.cs,
string strVal=Request.QueryString["val"];
2)Use Session
Setting session in first page before redirecting
Session["val]=txtBox.Text;
Response.Redirect("user.aspx");
and in user.aspx.cs
String strVal=(string) Session["val"];
EDIT :VB.NET VERSION
1) Use Querystring
Response.Redirect("user.aspx?val=" + txtBox.Text)
and in user.aspx.vb
Dim strVal As String = Request.QueryString("val")
2)Use Session
Setting Session in firstpage
Session("val")=txtBox.Text
Response.Redirect("user.aspx")
and in user.aspx.vb.
Dim strVal As String = DirectCast(Session("val"), String)
You can pass it in the query string, like this:
Response.Redirect("UserPage.aspx?user=" + HttpUtility.UrlEncode(txtUser.Text));
And then retrieve it via:
string user = Request.QueryString["user"];
If you're worried about users messing with a query string (be sure to validate it), you could also store a Session variable before doing the redirect.
warning: this is a gross but easy solution
Session("myTextbox")= txtUser.Text
this will persist the value so on the page_load of the next page you can say
txtUser.Text=Session("myTextBox")
What are you passing form page to page? Is it a list of things. You could have an object with different properties and could then pass it through a session. If you have multiple pages I would suggest doing this if you could end up reusing it else where. Passing it through the url, you would then need to validate it, because if someone types the url with the correct information or something that is being directly input into a database they could cause problems and/or unexpected results.
For some reason when the user click on the submit button and he re-fresh the page the same data get's uploaded again to my SQL Server 2005 database. I do not what this to happen........... Why is this happening? I am making use of a SQL Data Source!!
My code
Try
'See if user typed the correct code.
If Me.txtSecurity.Text = Session("Captcha") Then
If Session("NotifyMe") = "Yes" Then
SendEmailNS()
End If
RaterRate.Insert()
RaterRate.Update()
DisableItems()
lblResultNS.Text = "Thank you for leaving a comment"
LoadCompanyList()
LoadRateRecords()
txtCommentNS.Text = ""
txtSecurity.Text = ""
lblResultNS.Focus()
Else
Session("Captcha") = GenerateCAPTCHACode()
txtSecurity.Text = ""
txtSecurity.Focus()
Validator10.Validate()
End If
Catch ex As Exception
lblResultNS.Visible = True
lblResultNS.Text = ex.Message.ToString
lblResultNS.Focus()
End Try
When user visiting your page, generating new captcha. after when post or else, its checking captcha but not generating new. Move 'captcha set' code to outer scope.
If Me.txtSecurity.Text = Session("Captcha") Then
...
Else
...
End If
Session("Captcha") = GenerateCAPTCHACode()
This is happening because of the nature of WebForms relying on the PostBack model. Every time you click on a button the single form you have inside the page is posted to the server along with the viewstate. When you press F5 you normally get a warning that the form will be reposted and if you click Yes the same action is executed on the server. One way to avoid this is to perform a redirect after you save to the database:
Response.Redirect("~/success.aspx");