I have an aspx page with a table that runs at server. I wrote a code to insert a row in the table, as the code bellow shows. I want to save the now updated aspx page (i.e overwriting the current if possible) so that the next time I visit the page, all information shows. I don't want to use a database to store this information because this is only temporary and my hosting package does not support a database. Please help.
Below is my code:
Dim trow As New HtmlTableRow
Dim tcell As New HtmlTableCell
tcell.InnerText = "Code: " & TextBox1.Text & ". Weight: " & TextBox2.Text & ". Date: " & Now
trow.Cells.Add(tcell)
trow.Cells.Add(tcell)
Table.Rows.Add(trow)
TextBox1.Text = ""
TextBox2.Text = ""
'Save the aspx page
Me.SaveControlState()
Me.SaveViewState()
You can save the data in cookies. Try this.
Writing a cookie.
HttpCookie myCookie = new HttpCookie("MyTestCookie");
DateTime now = DateTime.Now;
// Set the cookie value.
myCookie.Value = now.ToString();
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire
// Add the cookie.
Response.Cookies.Add(myCookie);
Response.Write("<p> The cookie has been written.");
Reading a cookie
HttpCookie myCookie = new HttpCookie("MyTestCookie");
myCookie = Request.Cookies["MyTestCookie"];
// Read the cookie information and display it.
if (myCookie != null)
Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
else
Response.Write("not found");
Use session:
to access a value:
System.Web.HttpContext.Current.Session(“MyVariable”).ToString()
to set a value:
System.Web.HttpContext.Current.Session(“MyVariable”) = “NewValue”
Related
Example below is not working to delay a page redirect after 5 sec. - The Header update doesn't seem to work. Is there a pure VB solution for is the header the only way to go? Thought of just running a timer to delay next line of execution?
Tried commented out section as well - still didn't fire the header change.
Dim TF As String = ResetNewPassword(uName, pAnwser, newPassword)
Dim dateUpdated As Boolean = UpdateLastLoginDate(uName)
If TF Then
uEmail = u.Email
Label2.Text = "Users password has been updated"
Training.xMail(uEmail, Label2.Text, "Password Changed")
Response.AppendHeader("REFRESH", "5;URL=Default.aspx")
'Dim meta As New HtmlMeta()
'meta.HttpEquiv = "Refresh"
'meta.Content = "5;url=Page2.aspx"
'Me.Page.Controls.Add(meta)
PassQuestion1.Text = "You will now be redirected in 5 seconds"
Else
Label2.Text = "Users password failed to update please try again!!!"
Training.Mail("mail_user_acct", Label2.Text, "Password Failed - resetPassword")
End If
Could anyone please help, I have an asp application that asks for username and password in the login.aspx page and after logging in with the correct credentials, it prompts me whether to save the credentials. I clicked yes and after some time I logged out, then it takes me to the login.aspx page with the (saved) username and password already filled automatically in the boxes(because I saved previously above). Now my problem is that, now I cleared the username and password that are filled automatically in the boxes and hit submit. Then it should ask for username and password, but now actually it is using the old saved username and password and logging into the application !!!!
*To make it more brief and clear, this is the problem :-
"I am able to login even though I have removed the username and password. I logged out. Erased the content of both fields and then clicked 'Submit'. I was able to get into the Application."
Could anyone help please . Thanks in Advance !!!!
Here's my code for the 'Submit' button 'OnClick' Event :-
Protected Sub SignIn(sender As Object, e As EventArgs)
StatusText.Text = String.Empty
Dim Name As String = UserName.Text
Dim Password As String = UserPassword.Text
If IsValid Then
Try
Dim userStore = New UserStore(Of IdentityUser)()
Dim userManager = New UserManager(Of IdentityUser)(userStore)
userManager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(10)
userManager.MaxFailedAccessAttemptsBeforeLockout = 7
Dim user = userManager.FindByName(Name)
If user IsNot Nothing Then
If userManager.IsLockedOut(user.Id) Then
StatusText.Text = String.Format("Your account is locked. please contact administrator.")
Else
If userManager.CheckPassword(user, Password) Then
userManager.ResetAccessFailedCount(user.Id)
If Not userManager.GetLockoutEnabled(user.Id) Then
userManager.SetLockoutEnabled(user.Id, True)
End If
Dim tkt As FormsAuthenticationTicket
Dim cookiestr As String
Dim ck As HttpCookie
'Add Session to 5 Hours
tkt = New FormsAuthenticationTicket(1, user.UserName, DateTime.Now, DateTime.Now.AddHours(5), RememberMe.Checked, "")
cookiestr = FormsAuthentication.Encrypt(tkt)
ck = New HttpCookie(FormsAuthentication.FormsCookieName, cookiestr)
If RememberMe.Checked Then
ck.Expires = tkt.Expiration
End If
ck.Path = FormsAuthentication.FormsCookiePath
Response.Cookies.Add(ck)
Dim strRedirect As String
strRedirect = Request("ReturnUrl")
If strRedirect Is Nothing Then
strRedirect = "default.aspx"
End If
Response.Cookies.Add(New HttpCookie("adjusterId", New ContextProvider().GetAdjusterId(user.Id)))
Response.RedirectPermanent(strRedirect)
Else
userManager.AccessFailed(user.Id)
If userManager.IsLockedOut(user.Id) Then
StatusText.Text = String.Format("Your account is locked. please contact administrator.")
Else
StatusText.Text = String.Format("Invalid username or password, you have {0} more login attempt(s) left before account is locked out.", (3 - userManager.GetAccessFailedCount(user.Id)))
StatusText.Visible = True
End If
End If
End If
Else
StatusText.Text = String.Format("Invalid username or password.")
StatusText.Visible = True
End If
Catch ex As Exception
StatusText.Text = String.Format("Unable to login, please contact administrator.")
End Try
Else
StatusText.Text = String.Format("Enter username or password.")
End If
End Sub
Likely the authentication cookie is being remembered, after the initial login, and that still exists.
Somehow when you attempt to login with empty fields, it just uses the cookie instead of the empty fields. That would be my guess.
my best guess is that this method is where you will find the culprit code:
userManager.AccessFailed(user.Id)
It could also be a redirect problem. That you simply attempt to redirect the user back to a page on an invalid login, and since the cookie is still set, you are allow to see whatever page you are getting redirected too.
Main problem, your logout function doesn't remove the authentication cookie.
after a lot of banging, I found out a simple change that fulfilled my requirement, I just added the below properties for my 'username' textbox in .aspx as below :-
<asp:TextBox ID="UserName" runat="server" Width="295px" AutoCompleteType="None" EmptyMessage=" "></asp:TextBox>
(added AutoCompleteType and EmptyMessage).
Not sure whether that's the right approach, but that helps !!!! ThankYou.
Good day may i please get help,am currently looking for a way of accessing value from one page to another using asp.net sql language
You can do in following ways:
1. Response.Redirect
Page1.aspx
Response.Redirect("test.aspx?Name="+t.Text);
Page2.aspx
if (Request.QueryString["Name"]!= null)
Label3.Text = Request.QueryString["Name"];
2.Cookies
HttpCookie cook = new HttpCookie("Name");
cook.Value = txtName.Text;
Response.Cookies.Add(cook);
Response.Redirect("WebForm5.aspx");
Page 2
if (Request.Cookies["cook "] != null )
Label3.Text = Request.Cookies["cook "].Value;
3.Session
Session["ses"] = txtName.Text;
Response.Redirect("WebForm5.aspx");
Page 2
if(Session["ses"] != null)
Label3.Text = Session["ses"].ToString();
I am a newbie in using asp.net with code behind of vb.net I just wanna know on how to see the name of the admin on the POS page. it seems that this code doesn't work??
Main.lbl_name.Text = CurName.ToUpper
POS.lbl_cashier.Text = CurName.ToUpper
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd1 As New SqlCommand
Dim rdr As SqlDataReader
cmd1.Connection = cn
cmd1.Connection.Open()
cmd1.CommandText = "SELECT * from UserTable WHERE Username ='" & txt_username.Text & "'"
rdr = cmd1.ExecuteReader
If rdr.HasRows = True Then
rdr.Read()
If txt_username.Text = rdr.Item(0) And txt_password.Text = rdr.Item(3) Then
CurPos = rdr.Item("Type")
CurUser = rdr.Item("Username")
CurName = rdr.Item("EmployeeName")
If rdr.Item(4) = "ADMINISTRATOR" Then
MsgBox("WELCOME! " & rdr.Item(4), MsgBoxStyle.Information)
'Main.lbl_name.Text = CurName.ToUpper
'POS.lbl_cashier.Text = CurName.ToUpper
cmd1.Connection.Close()
Response.Redirect("ACESCHOOLSUPPLIES.aspx")
'Me.Dispose()
You can't just access other pages, ASP.NET runtime is ignorant about other pages, you have access to your current page only!
You can use the Session variable to store some data temporarily for current user session, the Session object is available on every ASP.NET Page.
Session("adminname") = CurName
On other page where you want to show it you just reload it from Session
Dim NewName = Session("adminname")
Take some hidden field and use session.add("username") and store your username or which ever you want and the n retrieve that from your second page.
Session.Add("Username",Username);
does essentially the same as
Session["Username"] = Username;
As Alaudo suggested, storing variables in Session state is an option.
For the sake of completeness other options you have are:
Cookies
QueryString
Hidden fields (for POST requests)
The logged on user name is something I personally would not store in Session state or pass around using any of the alternate techniques I mention above.
Looking at your code it seems you are trying to authenticate some credentials (user name/password).
I recommend you look at MemebershipProvider in ASP.NET. Are you familiar with this? You can then easily access the logged in user.
I'm looking for a good example of storing data in a cookie using ASP.NET. Could someone show me an example?
MSDN is quite your friend : http://msdn.microsoft.com/en-us/library/78c837bd.aspx
Until then :
C#:
Response.Cookie["cookie_name"] = "value";
VB:
Response.Cookie("cookie_name") = "value";
How to Create a cookie
HttpCookie mycookie = new HttpCookie("mycookie");
mycookie.Value = "chocolate chip please.";
Response.Cookies.Add(mycookie);
How to Read a cookie
HttpCookie mycookie = Request.Cookies["mycookie"];
Response.Write("Your cookie is: " + mycookie.Value);
check this out
Save Form Data With Cookies
Google says "How to Write a Cookie"