Maintain State of Previous Page After Clicking LinkButton - c#

in linkbtn
protected void lnkBtnSun_Click(object sender, EventArgs e)
{
Session["employeeName"] = txt_EmpName.Text;
Session["Projectstaus"] = ddownList.SelectedValue;
Session["Startdate"] = txt_StartDate.Text;
Session["EndDate"] = txt_EndDate.Text;
Session["lblsun"] = lbl_sun.Text;
Session["lblmon"] = lbl_Mon.Text;
}
in timesheet.aspx
protected void Page_Load(object sender, EventArgs e)
{
string employeeName = (Session["employeeName"] != null) ? Session["employeeName"] : "";//cursor moves from textbox dropdown data is disappering
string projectStatus = (Session["Projextstaus"] != null) ? Session["Projextstaus"] : "";//maintains value when retun back from another webform
string startDate = (Session["Startdate"] != null) ? Session["Startdate"] : "";
string endDate = (Session["EndDate"] != null) ? Session["EndDate"] : "";//String was not recognized as a valid DateTime.
string lblsun = (Session["lblsun"] != null) ? Session["lblsun"] : "";
string lblmon = (Session["lblmon"] != null) ? Session["lblmon"] : "";
}
maintain state of previous page when I click on linkbutton
I have two webforms in on webform I contross when i click on linkbutton it goes to nextpage ther if im done while retunrsn to main page im loosing data in mainpage ,I tried session but not worked.
txt_EmpName.Text = (Session["employeeName"] != null) ? Session["employeeName"].ToString() : "";
ddownList.SelectedValue = (Session["Projectstaus"] != null) ? Session["Projectstaus"].ToString() : "";

Session is the typical solution in these scenarios. Are you writing those properties to the Session object? Otherwise, they will always be null and you'll see the behavior you're describing.

Related

Cookies viewstate are not maintaining in AjaxControlToolkit

I have cookies and viewstate in below control .
This ajax Control is used to upload multiple file images.
protected void OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
int userid = 25;
DAL_Cart objdalcart = new DAL_Cart();
if (Viewstate["Imagestringname"] == null)
{
objdalcart.InsertTempImage(userid, ImageName, 1);
}
else
{
objdalcart.InsertTempImage(userid, ImageName, 0);
}
Response.Cookies["JewelleryUserCookiesUserId"].Value = Convert.ToString(userid);
Response.Cookies["JewelleryUserCookiesUserId"].Expires = DateTime.Now.AddYears(1);
Viewstate["Imagestringname"] = ImageName + ",";
}
The issue is when I try to retrive view state value or Cookies value on different click event of button in same page I am not able to retrive the value
protected void lnkcheckout_Click(object sender, EventArgs e)
{
if (Request.Cookies["JewelleryUserCookiesUserId"] == null || Request.Cookies["JewelleryUserCookiesUserId"].Value == "")
{
}
if (Viewstate["Imagestringname"] != null)
{}
}
For both the case it is going in if condition. for viewstate I have placed Enableviewstate=true on master page .Any idea why?
Review
Want ajax multiple file upload on my button click event
var c = new HttpCookie("JewelleryUserCookiesUserId");
c.Value = Convert.ToString(userid);
c.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(c);
Just note: this is insecure. the client can manipualte the cookie...

ASP.Net List View becomes too slow when we load or update bulk of records. How can we improve its speed or any other alternative?

I'm trying to load and bind records with ListView but i'm facing these two issues
It is taking long time to load the records
When i save records after update, it gives error Maximum request length exceeded.
Below is the first method which calls on button click.
private void LoadItems()
{
string warehouseCode = txtWarehouseCode.Text;
if (string.IsNullOrEmpty(warehouseCode))
{
ShowMessage(GetLocalResourceObject("1208").ToString(), Messages.Red);
return;
}
DataTable dt = invManager.ItemCardStockManager.SelectOpeningStock(warehouseCode);
lsvItems.DataSource = dt;
lsvItems.DataBind();
}
And here is the ItemDataBound method which calls during binding.
protected void lsvItem_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
HiddenField hdnHigherUnitQty = e.Item.FindControl("hdnHigherUnitQty") as HiddenField;
HiddenField hdnBaseUnitQty = e.Item.FindControl("hdnBaseUnitQty") as HiddenField;
HiddenField hdnOpeningCostPrice = e.Item.FindControl("hdnOpeningCostPrice") as HiddenField;
HiddenField hdnUnitFraction = e.Item.FindControl("hdnUnitFraction") as HiddenField;
HiddenField hdnHigherUnitType = e.Item.FindControl("hdnHigherUnitType") as HiddenField;
decimal uq1 = (hdnHigherUnitQty.Value == "" ? decimal.Parse("0") : decimal.Parse(hdnHigherUnitQty.Value));
decimal uq2 = (hdnBaseUnitQty.Value == "" ? decimal.Parse("0") : decimal.Parse(hdnBaseUnitQty.Value));
decimal cst = (hdnOpeningCostPrice.Value == "" ? decimal.Parse("0") : decimal.Parse(hdnOpeningCostPrice.Value));
decimal fr2 = (hdnUnitFraction.Value == "" ? decimal.Parse("0") : decimal.Parse(hdnUnitFraction.Value));
int ut2 = (hdnHigherUnitType.Value == "" ? int.Parse("0") : int.Parse(hdnHigherUnitType.Value));
TextBox lbHigherUnitQty = e.Item.FindControl("lbHigherUnitQty") as TextBox;
TextBox lbBaseUnitQty = e.Item.FindControl("lbBaseUnitQty") as TextBox;
TextBox lbOpeningCostPrice = e.Item.FindControl("lbOpeningCostPrice") as TextBox;
TextBox lbUnitFraction = e.Item.FindControl("lbUnitFraction") as TextBox;
TextBox lbHigherUnitType = e.Item.FindControl("lbHigherUnitType") as TextBox;
lbHigherUnitQty.Text = uq1.ToString();
lbBaseUnitQty.Text = uq2.ToString();
lbOpeningCostPrice.Text = cst.ToString();
lbUnitFraction.Text = fr2.ToString();
lbHigherUnitType.Text = ut2.ToString();
}
}

Session not getting created/used

I wrote this code in which I made a session but I am not sure if the session is not getting created or is not getting used
Session["user"] = this.txtUser.Text.Trim();
The page I am trying to get the session value from :
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
txtName.Text = Session["user"].ToString();
}
Change your code from
if (Session["user"] == null)
{
txtName.Text = Session["user"].ToString();
}
to this
if (Session["user"] != null)
{
txtName.Text = Session["user"].ToString();
}
in Page_Load
Because you are setting txtName's Text property when Session is null. It is not the case because session is having previous value of TextBox.
While fetching the session value, the code should be as following,
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] != null)
{
txtName.Text = Session["user"].ToString();
}

Pass variables using Session throwing an exception

I have a form for clients to fill out, so I decided to make it an digital form. I have three pages subscriber_details, Package_Selection and Bank_Details. When the user has filled in all fields in the first and clicks next the page progresses onto the next till all three has been filled, when all three is filled they direct to a final page where all their details are presented to them for one last time, so that they can make sure its correct... on my subscriber_details.aspx I have the following code to store their details into sessions
protected void btnNext_Click(object sender, EventArgs e)
{
Session["FullName"] = txtFullName.Text;
if (txtCompanyName.Text == String.Empty)
Session["CompanyName"] = "N/A";
else
Session["CompanyName"] = txtCompanyName.Text;
if (txtVAT.Text == String.Empty)
Session["VAT"] = "N/A";
else
Session["VAT"] = txtVAT.Text;
Session["ContactNumber"] = txtContactNumber.Text;
if (txtFax.Text == String.Empty)
Session["Fax"] = "N/A";
else
Session["Fax"] = txtFax.Text;
if (txtDistrict.Text == String.Empty)
Session["District"] = "N/A";
else
Session["District"] = txtDistrict.Text;
Session["City"] = txtCity.Text;
Session["Street"] = txtStreet.Text;
Session["Code"] = txtPostal.Text;
if (txtTrading.Text == String.Empty)
Session["Trading"] = "N/A";
else
Session["Trading"] = txtTrading.Text;
Session["ID"] = txtID.Text;
Session["ContactPerson"] = txtContactPerson.Text;
if (txtEmail.Text == String.Empty)
Session["Email"] = "N/A";
else
Session["Email"] = txtEmail.Text;
}
then on my final.aspx I have the following code to use the sessions and replace the text in labels
protected void Page_Load(object sender, EventArgs e)
{
lblFullName.Text = Session["FullName"].ToString();
lblCompanyName.Text = Session["CompanyName"].ToString();
lblVat.Text = Session["VAT"].ToString();
lblContactNumber.Text = Session["ContactNumber"].ToString();
lblFax.Text = Session["Fax"].ToString();
lblDistrict.Text = Session["District"].ToString();
lblStreet.Text = Session["Street"].ToString();
lblCity.Text = Session["City"].ToString();
lblCode.Text = Session["Code"].ToString();
lblTrading.Text = Session["Trading"].ToString();
lblID.Text = Session["ID"].ToString();
lblContactPerson.Text = Session["ContactPerson"].ToString();
lblMail.Text = Session["Email"].ToString();
}
for some reason I get an "Object reference error", is it because my final.aspx page isn't my next page, because I have to pass through my package.aspx and bank_details.aspx first?
I have required field validators on the sessions that doesn't have an if statement, so the text wont be empty
You are not setting all of the Session variables. For example, you have not set Session["Email"] so the call to lblMail.Text = Session["Email"].ToString(); will throw the exception.
You should populate all Session variables you want to use and also check they are not null before doing .ToString(). This should catch it more gracefully.

Silverlight: Get RowGroupHeader value in DataGridRowGroupHeader event

I am grouping datagrid upto one sub-level.
Like this:
CollectionViewSource pageView = new CollectionViewSource();
pageView.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
pageView.GroupDescriptions.Add(new PropertyGroupDescription("SubCategory"));
tasksDataGrid.ItemsSource = pageView.View;
In my case some records doesn't have Subcategory value.Those records will display under empty row group header of Subcategory in datagrid.
I would like to display directly under Category row group header instead of empty header.
private void TaskDataGrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
{
string RowGroupHeader = // how to get currently loading header value
if(RowGroupHeader == string.Empty)
{
e.RowGroupHeader.Height = 0;
}
}
I can't get currently loading RowGroupHeader value.How can i get RowGroupHeader value in LoadingRowGroup event.
Help me on this.
This solved the problem.
private void TaskDataGrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
{
var RowGroupHeader = (e.RowGroupHeader.DataContext as CollectionViewGroup);
if (RowGroupHeader != null && RowGroupHeader.Items.Count != 0)
{
MasterTask task = RowGroupHeader.Items[0] as MasterTask;
if (task != null && task.SubCategoryName == null)
e.RowGroupHeader.Height = 0;
}
}
Thanks djohnsonm for your help.
Try this, but insert the name of your VM and Property that would correspond to the Header value.
private void TaskDataGrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
{
string RowGroupHeader = (e.RowGroupHeader.DataContext as ParentVM).VMProperty
if(RowGroupHeader == string.Empty)
{
e.RowGroupHeader.Height = 0;
}
}

Categories

Resources