So I have made a log in system and now I wan't to allow the users to update their information. I thought that this code would work but it did not, here it is:
public partial class Account_Update : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MembershipUser usr = Membership.GetUser();
if (usr.IsApproved == false)
{
Response.Redirect("~/Login.aspx");
}
var p = Profile.GetProfile(usr.UserName);
/* Displays all current profile information once the page loads */
FirstName.Text = p.fName;
LastName.Text = p.lName;
Address.Text = p.Address;
Email.Text = usr.Email;
Company.Text = p.Company;
}
/* Simple button to take you to the home screen */
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("~/default.aspx");
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
MembershipUser usr = Membership.GetUser();
var p = Profile.GetProfile(usr.UserName);
/* Update all information that the user has changed */
p.fName = FirstName.Text;
p.Save();
p.lName = LastName.Text;
p.Save();
p.Address = Address.Text;
p.Save();
usr.Email = Email.Text;
Membership.UpdateUser(usr);
p.Company = Company.Text;
p.Save();
Success.Text = "User Information has been updated";
/* Redisplaying the updated user information */
FirstName.Text = p.fName;
LastName.Text = p.lName;
Address.Text = p.Address;
Email.Text = usr.Email;
Company.Text = p.Company;
}
}
The problem seems to be that whatever I change the text to in the text-box is not changing from what was originally in the text-box. So if initially the users first name was Bob and I change it to Robert when I hit the update button it does not change it to Robert. this seems like a simple fix but I'm sort of lost. So to summarize the main question is how do I update the users information to the new text that the user enters in the textbox.
put !Ispostback at the page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MembershipUser usr = Membership.GetUser();
if (usr.IsApproved == false)
{
Response.Redirect("~/Login.aspx");
}
var p = Profile.GetProfile(usr.UserName);
/* Displays all current profile information once the page loads */
FirstName.Text = p.fName;
LastName.Text = p.lName;
Address.Text = p.Address;
Email.Text = usr.Email;
Company.Text = p.Company;
}
}
If you don't use it, alwyas when the info going to the server you will get the olds value.
cheers.
Related
This is where the registration is checked
protected void BtnLogin_Click(object sender, EventArgs e)
{
this.user = new User();
DAL dal = new DAL();
user = dal.GetUserDetails(this.a.Text, this.b.Text;);
Session["Login"] = user; //here
Session.Timeout = 25;
Response.Redirect("Default.aspx");
}
The routing goes here
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Login"] == null)//Sometimes it is NULL and sometimes it is full
{
Response.Redirect("login.aspx");
}
}
}
*If I close and reopen VS, it works (vs 2022)
Why does the SESSION suddenly not work on another page?
On the same page it does exist and has not been deleted
I have a "listview" for showing list of employee and a "dropdownlist" to select department. The following error occurs when I use "DropDownlist" for 3rd or second time:
"Failed to load viewstate".
The control tree into which viewstate is being loaded must match the control tree that was used to save "viewstate" during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request."
This is asp.net Webform and I have to use this technology and have no other choice .
namespace .Presentation.general
{
public partial class Listg : PageBase
{
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/App_MasterPages/empty.Master";
}
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDepartmentsDropDownList();
GeneralObjectDataSource.SelectParameters["Department"].DefaultValue = "";
decimal presence = Convert.ToDecimal(Data.EmployeeDB.Create().GetCountEMPOnlineToday());
decimal visibles = Convert.ToDecimal(Data.EmployeeDB.Create().GetCountVisiblesEmployees());
visibles = (visibles == 0 ? 1 : visibles);
PresenceLabel.Text = System.Math.Round((presence / visibles) * 100, 1).ToString() + "% " + string.Format(" ({0})", presence);
}
}
public void Search(object sender, EventArgs e)
{
GeneralObjectDataSource.SelectParameters["name"].DefaultValue = Common.Converter.ConvertToFarsiYK(NameTextBox.Text.Trim());
if (PresenceRadioBottonList.SelectedValue == "1")
{
GeneralObjectDataSource.SelectParameters["onlyPresence"].DefaultValue = "true";
}
else
{
GeneralObjectDataSource.SelectParameters["onlyPresence"].DefaultValue = "false";
}
DataListView.DataBind();
}
public void select_department_SelectedIndexChanged(object sender, EventArgs e)
{
GeneralObjectDataSource.SelectParameters["name"].DefaultValue = "";
GeneralObjectDataSource.SelectParameters["Department"].DefaultValue = select_department.SelectedItem.Text;
GeneralObjectDataSource.DataBind();
DataListView.DataBind();
}
private void PopulateDepartmentsDropDownList()
{
select_department.DataSource = Biz.EmployeeBO.GetDepartments();
select_department.DataTextField = "Name";
select_department.DataValueField = "ID";
select_department.DataBind();
select_department.Items.Insert(0, new ListItem("", "0"));
select_department.SelectedValue = Biz.Settings.SelectedDepartmentID;
}
}
}
I have created a link button in aspx form which check availablity of login email address and its functionality is as.
protected void lnkCheckAvailable_Click(object sender, EventArgs e)
{
SystemUserBL bl = new SystemUserBL(SessionContext.SystemUser);
ds = new DataSet();
bl.FetchForLoginEmailAddress(ds, txtLoginEmailAddress.Text);
if (ds.Tables[0].Rows.Count > 0)
{
valDuplicatePassword.Visible = true;
valDuplicatePassword.Text = "<b>This User Name is already in use by another user.</b>";
}
else
{
valDuplicatePassword.Visible = true;
valDuplicatePassword.Text = "<b>Congratulations! " + txtLoginEmailAddress.Text + " is available.</b>";
}
}
It's working fine when user will click on check availability link button. There is another button "Save" which saves the user information in the table. Now my issue is that if it displays "This User Name is already in use by another user." message the information is still saved in the database. Please tell me how to prevent this!!!
You can return true or false based on user name exists in database or not. You can create a method which will check user availability.
When user press save button you will call that method if method returns true it means user exists.
private bool CheckUserAvailability()
{
SystemUserBL bl = new SystemUserBL(SessionContext.SystemUser);
ds = new DataSet();
bl.FetchForLoginEmailAddress(ds, txtLoginEmailAddress.Text);
if (ds.Tables[0].Rows.Count > 0)
{
valDuplicatePassword.Visible = true;
valDuplicatePassword.Text = "<b>This User Name is already in use by another user.</b>";
return true;
}
else
{
valDuplicatePassword.Visible = true;
valDuplicatePassword.Text = "<b>Congratulations! " + txtLoginEmailAddress.Text + " is available.</b>";
return false;
}
}
You can also call this method on link click.
protected void lnkCheckAvailable_Click(object sender, EventArgs e)
{
CheckUserAvailability();
}
You will call this method on Save button if user don't exist than save information in database.
protected void Savebtn_Click(object sender, EventArgs e)
{
if(CheckUserAvailability() == false)
{
SaveUserInfoToDataBase();
}
}
I'm designing a CheckOut page and I want to automatically load the signed in user's information with data from the database using linq. I'm using a method FillPage which I call in PageLoad and so far it looks like this:
void FillPage(int id)
{
using (DatabaseContext db=new DatabaseContext()
{
var query = (from user in db.[tblUser]
where user.ID == id
select user
).First();
if (query != null)
{
txtName.Text = query.Username;
txtEmail.Text = query.Email;
txtAddress.Text = query.PostalAddress;
ddProvice.SelectedValue = query.Province;
lblPassword.Text = query.Password;
lblDate.Text = query.DateRegistered.ToString();
}
}
}
Why does nothing happen when I load the page?
You must insert more of your code .your problem is not clear
May be in your load event of your page you forget to add
If (! IsPostback)
{
}
And may be you have reset your fields
public void MyPage_load( object sender , EventArgs e)
{
//Reset fields
}
This will fix your problem
public void MyPage_load( object sender , EventArgs e)
{
If (! IsPostback)
{
//Reset fields
}
}
Okay here I have some code from my project at the moment.
Basically the user goes onto the page and their current password is in the password textbox, and their avatar is selected in the droplist.
They can change their password by editing the text in the textbox and change their avatar by selecting a new one from the list. This is then written to the file where this information is kept. It writes to the file okay, but it writes what was initially in the textbox and droplist.
If i comment out these lines in the page load:
avatarDropList.SelectedValue = Session["avatar"].ToString();
newPasswordTextbox.Text = Session["password"].ToString();
It updates the file properly, but this is not what I want as I want the old password displayed there initially as well as the avatar to be selected in the dropbox.
Code below:
public partial class TuitterProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
avatarImage.ImageUrl = "~/images/avatars/" + Session["avatar"] + ".gif";
avatarDropList.SelectedValue = Session["avatar"].ToString();
userNameTextbox.Text = Session["user"].ToString();
newPasswordTextbox.Text = Session["password"].ToString();
}
protected void okButton_Click(object sender, ImageClickEventArgs e)
{
string username = Session["user"].ToString();
string password = Session["password"].ToString();
string newPassword = newPasswordTextbox.Text;
string newAvatar = avatarDropList.SelectedValue;
int allDataReference = -1;
//Each element in the array is a string(Username Password Avatar)
string[] allData = File.ReadAllLines(Server.MapPath("~") + "/App_Data/tuitterUsers.txt");
for (int i = 0; i < allData.Length; i++)
{
string[] user = allData[i].Split(' ');
if (user[0] == username && user[1] == password)
{
allDataReference = i;
}
}
if (allDataReference > -1)
{
Session["avatar"] = newAvatar;
Session["password"] = newPassword;
allData[allDataReference] = username + " " + newPassword + " " + newAvatar;
File.WriteAllLines(Server.MapPath("~") + "/App_Data/tuitterUsers3.txt", allData);
}
}
}
In the ASP.Net page event lifecycle, Page_Load is called on every request. What you are doing here is resetting the value of the TextBox every time the page loads, which will include the time that you press the button to save the profile.
All you need to do is check for the current request being a postback when you set your initial values:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
avatarImage.ImageUrl = "~/images/avatars/" + Session["avatar"] + ".gif";
avatarDropList.SelectedValue = Session["avatar"].ToString();
userNameTextbox.Text = Session["user"].ToString();
newPasswordTextbox.Text = Session["password"].ToString();
}
}