I want to reload all my user changes when he want to go back on previous page and I tried to use LoadViewState() and SaveViewState() methods like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(Session["savedViewState"] != null)
{
Object saved = (Object)Session["savedViewState"];
LoadViewState(saved);
}
}
else
{
Session["savedViewState"] = SaveViewState();
}
}
but it did't work. The session savedViewState is null.
But I still do not know if these methods can solve the problem of reloading my all view state.
you need to switch the code between the if else conditions
protected void Page_Load(object sender, EventArgs e)
{
if(Session["savedViewState"] != null && SaveViewState()!=null)
{
Object saved = (Object)Session["savedViewState"];
LoadViewState(saved);
}
else
{
Session["savedViewState"] = SaveViewState();
}
}
Related
When i store an item, i can store it and read it. But when i try to store another item, the first one disappear.
I'm using a masterpage for my default site dont know if it affects it.
my code is very simple and looks like this.
List<kurvliste> kurv = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv.Add(new kurvliste(1,1,1, "Produktnavn"));
Session["kurv"] = kurv;
}
In my masterpage it looks like this
List<kurvliste> kurv = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
Repeater1.DataSource = kurv;
Repeater1.DataBind();
}
Because you have a bug - you declare the same name outside and inside the Page_load, so on the click the outside is used - so you actually use two different lists.
I write your code adding 1 & 2 to understand the bug.
List<kurvliste> kurv_1 = new List<kurvliste>();
protected void Page_Load(object sender, EventArgs e)
{
List<kurvliste> kurv_2 = (List<kurvliste>)Session["kurv"];
if (kurv_2 == null)
{
kurv_2 = new List<kurvliste>();
Session["kurv"] = kurv_2; // Store the new list in the session object!
}
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv_1.Add(new kurvliste(1,1,1, "Produktnavn"));
Session["kurv"] = kurv_1;
}
How to solve it
But if i remove the outside, i get a null reference just by leaving it
to be List kurv.
No you need to remove the inside of Page_Load declaration that hides the outside one
protected void Page_Load(object sender, EventArgs e)
{
// on this line, just remove the declaration
// List<kurvliste> kurv = (List<kurvliste>)Session["kurv"];
// do it like
kurv = Session["kurv"] as List<kurvliste>;
if (kurv == null)
{
kurv = new List<kurvliste>();
Session["kurv"] = kurv; // Store the new list in the session object!
}
}
Alternative
I suggest to move the list on the session to a property like that
// using this const you avoid bugs in mispelling the correct key.
const string ckurvlisteNameConst = "kurvliste_cnst";
public List<kurvliste> kurv
{
get
{
// If not on the Session then add it
if (Session[ckurvlisteNameConst] == null)
Session[ckurvlisteNameConst] = new List<kurvliste>();
// this code is not exist on release, but I check to be sure that I did not
// overwrite this Session with a different object.
Debug.Assert(Session[ckurvlisteNameConst] is List<kurvliste>);
return (List<kurvliste>)Session[ckurvlisteNameConst];
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Unnamed_ServerClick(object sender, EventArgs e)
{
kurv.Add(new kurvliste(1,1,1, "Produktnavn"));
}
a similar answer : How to store list of object into ViewState
i'm requesting data from database through a D A L file. I want to data bind and show a drop down menu's single option selected against that of the database. How should i do it?
here's my code:
Pages pg = new Pages();
public static string pgId;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Bind_ddlParentPage();
Bind_ddlPageModel();
Bind_ddlArticleModel();
if (!IsPostBack)
{
pgId = Request.QueryString["pageId"];
if (pgId != null)
{
GetData();
}
}
}
}
public void GetData()
{
ddlParentPage.SelectedValue = pg.ParentPage;
//Bind_ddlParentPage();---dropdownlist which is causing problem.
//I want to set this data:: pg.ParentPage to dropdownlist in another
page
ddlPageModel.SelectedValue = pg.PageModel;
//Bind_ddlPageModel();
//All the three drop downs have same table for the source,
'Pages' table and this page is the same page for adding new entry to
Pages table.
ddlArticleModel.SelectedValue = pg.ArticleModel;
//Bind_ddlArticleModel();
}
First, you have a duplication in your conditional statement:
if(!IsPostBack) {
...
if(!IsPostBack) {
...
}
}
Second, you need to bind the data to the dropdown, then set the selected value. Refer: this post
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataBind(); // get the data into the list you can set it
DropDownList1.Items.FindByValue("SOMECREDITPROBLEMS").Selected = true;
}
}
How can Edit SqlDataSource record without FormView?
I knows getting data into TextBox, But can't save changes.
protected void Page_Load(object sender, EventArgs e)
{
DataView dvSql = (DataView)EmployData.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView drvSql in dvSql)
{
E_ID.Text = drvSql["ID"].ToString();
E_LName.Text = drvSql["LName"].ToString();
...
}
}
protected void SaveChanges_Click(object sender, EventArgs e)
{
EmployData.UpdateParameters.Add("ID", E_ID);
EmployData.UpdateParameters.Add("LName", E_LName);
....
EmployData.Update();
}
Thank's.
So simple...
Add on Load_Page before read code:
if (Page.IsPostBack)
{ }
else
{
DataView dvSql = (DataView)EmployData.Select(DataSourceSelectArguments.Empty);
....
....
}
Good Day.
I have a Datalist that has a delete button in it , I want to check if the user is admin, shows this button for each item and if not dont show that. This is my ItemDataBound, my problem is that for each item it connects to database and check if user is admin or not and I am worried about getting slow for large number of items. How can I fix this problem?
protected void GroupMembersDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton deletButton = (LinkButton)e.Item.FindControl("DeletFromGroup");
// here first connecet to database to get group name
int GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
// here connect to database and find groups admin ID and checks that are the same or not ?
BusinessLayer.Group_Table GObject = new BusinessLayer.Group_Table(GroupID);
if (Convert.ToInt32(Session["ID"].ToString()) == GObject.Id)
{
deletButton.Visible = true;
}
else
{
deletButton.Visible = false;
}
}
}
Edit solution:
private BusinessLayer.Group_Table GObject;
private int GroupID;
protected void Page_Load(object sender, EventArgs e)
{
try
{
int ID = Convert.ToInt32(Session["ID"].ToString());
GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
GObject = new BusinessLayer.Group_Table(GroupID);
}
catch( Exception ee )
{
Response.Redirect("~/Home.aspx");
}
if (!IsPostBack)
{
getdata();
}
}
You could initialize a field in the class in Page_Load or the DataList's DataBinding event. Then you can access this field from ÌtemDataBound.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int GroupID = BusinessLayer.Group_Table.GetByUniqName(Page.RouteData.Values["GroupName"].ToString());
GObject = new BusinessLayer.Group_Table(GroupID);
DataBindDataList(); // code that calls DataBind
}
}
private BusinessLayer.Group_Table GObject;
Another way is to store this value in the Session.
I set up a session variable on the Booking form.aspx as so:
protected void confirmImageButton_Click(object sender, ImageClickEventArgs e)
{
Session["confirmBooking"] = "confirm";
Session["beachBach"] = beachBachRadioButtonList.SelectedItem.Text;
}
and I transfer to my other page as so:
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
I'm trying to add 1 to the beachBach session variable whenever user press the confirm button....however, when i start to debug it, instead of adding 1, it add 2 to the label.
Can someone please help me out.. thanks
try this
protected void Page_Load(object sender, EventArgs e) {
{
if(!IsPostBack)
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
Well what happens after we come to this page ?? Do we have any event that causes postback in this page??
If there is than definitely it will add 1 again to your session .
Try using !IsPostBack property .