Problem with Profiles in ASP .NET - c#

I made an ASP .net application which use the asp .net login system. I use the a class which gets some details of the logged in user such as Name, address etc. In the page that the user can change his details i have those commands. If i don't use the commands in the page_load the address changes in the database successfully, but if i use them the database doesnt make the changes in the address. How is it possible? The profileC class uses the Inherits from ProfileBase class
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
classes.ProfileC pr = classes.ProfileC.GetProfileC(HttpContext.Current.User.Identity.Name);
TxtAddress.Text = pr.UserAddress;
}
}
protected void BtnAdd_Click(object sender, EventArgs e)
{
classes.ProfileC pr = classes.ProfileC.GetProfileC(HttpContext.Current.User.Identity.Name);
pr.UserAddress = TxtAddress.Text;
pr.Save();
}
}

You need an If !IsPostback in your Page_Load with your current logic inside the if.
Don't forget when you press a button Page_Load will fire before the BtnAdd_Click

Related

Detect Browser Refresh to Prevent a Method from Triggering in C# ASP.NET

So I have a Method that is setup to send out an email after the user reaches to this page. However, I want to prevent it from sending out again when the user accidentally refresh the page. Down below is my code:
protected void Page_Load(object sender, EventArgs e)
{
Session["ID"] = "50";
if (string.IsNullOrEmpty(Session["ID"] as string))
{
Response.Redirect("./default");
}
if (!Page.IsPostBack)
{
email();
}
}
I realized that refreshing the page isn't going to post back, it's more of a GET than a post.

ASP.NET C# Set OnSelectedIndexChanged from Code Behind

Im trying to set the SelectedIndexChanged from the code behind of a Web Forms application. I have a variable amount of dropdowns being added onto the page from the database and need a method to trigger each time one of the dropdowns change.
Im currently trying:
ddlProductCause.SelectedIndexChanged += new EventHandler(ddlProductCause_Changed);
ddlProductCause.ID = "ddlProductCause_" + row["item_id"].ToString();
ddlProductCause.AutoPostBack = true;
and...
public void ddlProductCause_Changed(object sender, CommandEventArgs e)
{
// do stuff
}
But I have no luck.
Any ideas?
Event Argument may cause this, use EventArgs
protected void ddlProductCause_Changed(object sender, EventArgs e){
//to get id
DropDownList ddl=sender as DropDownList;
//ddl.Id <---Access property like this.
}

Button click event error

Having just added a new button in my web application, I get an error when clicking on it, and I'm wondering if this is related to misplaced code. I will describe what/where I did, briefly. Thanks very much.
In ascx file:
<asp:Button ID="btn_rezerv" runat="server" Text="Reserve film" OnClick="btn_rezerv_Click"/>
In the ascx.cs file:
namespace CinProj.UserControls
{
public partial class FilmsList : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
PopulateControls();
}
private void PopulateControls()
{
string categId = Request.QueryString["CategID"];
string filmId = Request.QueryString["FilmID"];
....
if (categId != null)
{
.....
}
if (filmId != null)
{
......
Button btn_rezerv = (Button)item.FindControl("btn_rezerv");
}
}
protected void btn_rezerv_Click(object sender, EventArgs e)
{
string fid = Request.QueryString["FilmID"];
ShoppingCartAccess.AddItem(fid);
}
}
}
"Server Error in '/' Application.
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. "
Another problem could be because your PopulateControls method should probably only be called when during the Page Load when it's not a PostBack. I can't tell from above, but to me it looks like it only needs done on Load. Try wrapping that call with this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateControls();
}
}
It's likely the result of making some sort of client change that the server doesn't know about. Many times this is the result of changing values in a dropdown in JavaScript, for example.
To fix, you could:
Do away with using JavaScript for said modification
Use an UpdatePanel and add your control to it. If the client needs to make a change, trigger the UpdatePanel's update in order for the control's viewstate to update.

Only add items to a dropdown list once

I have a dropdown list that is populated when the page loads, like this:
protected void Page_Load(object sender, EventArgs e)
{
ddlCars.Items.Add("Ford");
ddlCars.Items.Add("Chevy");
ddlCars.Items.Add("BMW");
ddlCars.Items.Add("Jeep");
ddlCars.Items.Add("Nissan");
}
It works fine, but when a user changes the selection of the dropdown list, the list is repopulated and I see the same items twice, because the page is reloading and these values are being re-added.
What would be the best way to stop this from happening? It is important that AutoPostBack stays enabled, so that I change information based on user selection.
Thanks
You need to put that code in a !Page.IsPostBack block.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlCars.Items.Add("Ford");
ddlCars.Items.Add("Chevy");
ddlCars.Items.Add("BMW");
ddlCars.Items.Add("Jeep");
ddlCars.Items.Add("Nissan");
}
}
This will allow that code to run once (when the page loads the first time), and then be ignored whenever the page is posting back to the server due to user interaction, etc.
For more information, see Page.IsPostBack on MSDN.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
ddlCars.Items.Add("Ford");
ddlCars.Items.Add("Chevy");
ddlCars.Items.Add("BMW");
ddlCars.Items.Add("Jeep");
ddlCars.Items.Add("Nissan");
//ddlCars.datasource=?
//ddlCars.databind();
}
}

ASP.net master page/content page simple design problem

My master page:
public partial class MasterPages_Main : System.Web.UI.MasterPage
{
public bool IsLoggedIn;
protected void Page_Load(object sender, EventArgs e)
{
// Check login
LoggedInUser ThisUser = new LoggedInUser();
IsLoggedIn = ThisUser.IsLoggedIn;
Response.Write("Master" + IsLoggedIn.ToString());
}
This outputs 'True', we are logged in.
On my content page I do:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("CONTENT:" + Master.IsLoggedIn.ToString());
}
But this outputs 'False'.
So the actual page output is:
Content:False
Master:True
On my content page I need to redirect if the user is logged in, but this value is always false from the content pages point of view! How can I resolve this?
Content Page load event occurs before Master Load (from here). So you probably need to change the logic, and maybe call some content page's methods from master Page_Load. Or set IsLoggedIn inside Master Init event handler.
Change Master Page_Load to Page_Init, this will force it to execute before the content page.
The master page is called after your code for Page_Load(). Try this:
Protected void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender,e);
Response.Write("CONTENT:" + Master.IsLoggedIn.ToString());
}

Categories