TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Cally K
1.2k
507
15.6k
Retain values of List between page postbacks
Jan 24 2019 2:14 AM
I am working on this code and I realised that the List
gets initialized everytime from a different event handler. I do not want the List to be initialized to empty, I need the value to be retained. I have placed the declaration of the list in the class instead of in the method. Please go through my code and correct me.
Hi, I have edited my post, did some reading and realised that it is very much connected to retaining values between page postbacks.
public
partial
class
WebForm4 : System.Web.UI.Page
{
#region List of Weeks
public
List
A_Week_1 =
new
List
();
public
List
A_Week_2 =
new
List
();
public
List
A_Week_3 =
new
List
();
public
List
A_Week_4 =
new
List
();
public
List
A_Week_5 =
new
List
();
public
List
A_Week_6 =
new
List
();
#endregion
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
btnUpload_Click(
object
sender, EventArgs e)
{
if
(FileUpload1.HasFile)
{
try
{
string
filename = Path.GetFileName(FileUpload1.FileName);
//FileUpload1.SaveAs(Server.MapPath("~/Uploaded Files/") + filename);
string
fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string
fileLocation = Server.MapPath(
"~/Uploaded Files/"
+ filename);
FileUpload1.SaveAs(fileLocation);
lblMessage.Text =
"Upload status: File uploaded to "
+ fileLocation;
readfile();
}
catch
(Exception ex)
{
lblMessage.Text =
"Upload status: The file could not be uploaded. The following error occured: "
+ ex.Message;
}
}
}
protected
void
readfile()
{
//datasource
string
connString = ConfigurationManager.ConnectionStrings[
"testexcel"
].ConnectionString;
//object represents a unique connection to a data source.
OleDbConnection oledbConn =
new
OleDbConnection(connString);
try
{
// Open connection
oledbConn.Open();
DataTable dtsheet =
new
DataTable(
"storesheettable"
);
//because the oledbConn.GetOlddbSchemaTable returns datatale
dtsheet = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
null
);
//initialize and call constructor
if
(dtsheet ==
null
)
{
Label1.Text =
"No sheet to display"
;
}
else
{
//String[] excelSheets = new String[dtsheet.Rows.Count];
List<
string
> import_excel_weeks_list =
new
List<
string
>();
//int i = 0;
char
[] charsToTrim = {
'$'
};
for
(
int
i = 0; i < dtsheet.Rows.Count; i++)
{
//excelSheets[i] = dtsheet.Rows.ToString();
import_excel_weeks_list.Add(dtsheet.Rows[i][
"TABLE_NAME"
].ToString().Trim(charsToTrim));
}
Repeater1.DataSource = import_excel_weeks_list;
Repeater1.DataBind();
}
}
catch
(Exception ex)
{
catchexception.Text = ex.ToString();
}
finally
{
// Close connection
oledbConn.Close();
}
}
protected
void
checkcalendarweek(
string
weekname)
{
if
(weekname ==
"Week2"
)
{
int
storemonth;
DateTime today =
new
DateTime();
storemonth = today.Month;
Response.Write(storemonth);
}
}
public
void
monthsddl_SelectedIndexChanged(
object
sender, EventArgs e)
{
int
total_no_days;
int
selected_month = Convert.ToInt32(monthsddl.SelectedValue);
//total number of days in the month for the year
total_no_days = DateTime.DaysInMonth(DateTime.Today.Year, selected_month);
Label1.Text = String.Format(
"Total number of days in the month of {0} is {1}"
, monthsddl.SelectedItem,total_no_days);
DateTime firstOfMonth =
new
DateTime(Convert.ToInt32(DateTime.Now.Year), selected_month, 1);
int
startdayofthemonth = (
int
)DayOfWeek.Monday;
//total weeks in a month
int
weeksInMonth = (
int
)Math.Ceiling((startdayofthemonth + total_no_days) / 7.0);
int
weeknum = 0;
//dates are of course from 1 to the total_no_days
for
(
int
i = 1; i <= total_no_days; i++)
{
//create the date based on the selected month with i starting as 1
DateTime date1 =
new
DateTime(DateTime.Today.Year, selected_month, i);
DateTime dateonly = date1.Date;
//get the local culture timeformat
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
//gets the calendar for the local timeformat and assign it to a type of calendar, gregorian
System.Globalization.Calendar cal = dfi.Calendar;
int
num = cal.GetWeekOfYear(date1, dfi.CalendarWeekRule, (DayOfWeek.Monday));
//week in a month
DateTime test =
new
DateTime(date1.Year, date1.Month, 1);
int
num2 = cal.GetWeekOfYear(test, dfi.CalendarWeekRule, (DayOfWeek.Monday));
weeknum = num - num2 + 1;
//input dates for the week
if
(weeknum == 1)
{
A_Week_1.Add(date1);
}
else
if
(weeknum == 2)
{
//week2.Add(new Week() { datefortheweek = date1 });
A_Week_2.Add(date1);
}
else
if
(weeknum == 3)
{
//week3.Add(new Week() { datefortheweek = date1 });
A_Week_3.Add(date1);
}
else
if
(weeknum == 4)
{
//week4.Add(new Week() { datefortheweek = date1 });
A_Week_4.Add(date1);
}
else
if
(weeknum == 5)
{
//week5.Add(new Week() { datefortheweek = date1 });
A_Week_5.Add(date1);
}
else
{
//week6.Add(new Week() { datefortheweek = date1 });
A_Week_6.Add(date1);
}
Response.Write(weeknum);
Response.Write(
" "
);
Response.Write(dateonly.ToString(
"dd/M/yyyy"
));
Response.Write(
" "
);
Response.Write(dateonly.DayOfWeek);
Response.Write(
"
"
);
}
//displaying the list
foreach
(DateTime text
in
A_Week_5)
{
Response.Write(text);
Response.Write(
"
"
);
}
//Response.Write("Test" + A_Week_5.Last().datefortheweek);
Response.Write(
"Test"
+ A_Week_5.Last());
//createtable(weeksInMonth, week1.First().datefortheweek, week1.Last().datefortheweek);
createtable(weeknum, A_Week_1, A_Week_2, A_Week_3, A_Week_4, A_Week_5, A_Week_6);
}
protected
void
createtable(
int
pass_in_weeksInMonth, List
week_1, List
week_2, List
week_3, List
week_4, List
week_5, List
week_6)
{
DataTable createweek_dates =
new
DataTable();
DataColumn column_createweek_dates;
DataRow rows_createweek_dates;
//Column 1
column_createweek_dates =
new
DataColumn();
column_createweek_dates.DataType = Type.GetType(
"System.Int32"
);
column_createweek_dates.ColumnName =
"Week Number"
;
column_createweek_dates.ReadOnly =
true
;
column_createweek_dates.Unique =
true
;
// Add the Column to the table
createweek_dates.Columns.Add(column_createweek_dates);
//Column 2
column_createweek_dates =
new
DataColumn();
column_createweek_dates.DataType = Type.GetType(
"System.DateTime"
);
column_createweek_dates.ColumnName =
"Start Date"
;
column_createweek_dates.ReadOnly =
true
;
//column_createweek_dates.Unique = true;
// Add the Column to the table
createweek_dates.Columns.Add(column_createweek_dates);
//Column 3
column_createweek_dates =
new
DataColumn();
column_createweek_dates.DataType = Type.GetType(
"System.DateTime"
);
column_createweek_dates.ColumnName =
"End Date"
;
column_createweek_dates.ReadOnly =
true
;
//column_createweek_dates.Unique = true;
// Add the Column to the table
createweek_dates.Columns.Add(column_createweek_dates);
for
(
int
i = 1; i <= pass_in_weeksInMonth; i++)
{
//new row for every creation
rows_createweek_dates = createweek_dates.NewRow();
rows_createweek_dates[
"Week Number"
] = i;
for
(
int
j = i; j <= i; j++)
{
if
(j == 1)
{
rows_createweek_dates[
"Start Date"
] = week_1.First();
rows_createweek_dates[
"End Date"
] = week_1.Last();
}
else
if
(j == 2)
{
rows_createweek_dates[
"Start Date"
] = week_2.First();
rows_createweek_dates[
"End Date"
] = week_2.Last();
}
else
if
(j == 3)
{
rows_createweek_dates[
"Start Date"
] = week_3.First();
rows_createweek_dates[
"End Date"
] = week_3.Last();
}
else
if
(j == 4)
{
rows_createweek_dates[
"Start Date"
] = week_4.First();
rows_createweek_dates[
"End Date"
] = week_4.Last();
}
else
if
(j == 5)
{
rows_createweek_dates[
"Start Date"
] = week_5.First();
rows_createweek_dates[
"End Date"
] = week_5.Last();
}
else
{
rows_createweek_dates[
"Start Date"
] = week_6.First();
rows_createweek_dates[
"End Date"
] = week_6.Last();
}
}
createweek_dates.Rows.Add(rows_createweek_dates);
}
rptItemsInCart.DataSource = createweek_dates;
rptItemsInCart.DataBind();
}
public
void
DoSend(
object
sender, EventArgs e)
{
AllLists create1 =
new
AllLists();
for
(
int
i = 0; i < rptItemsInCart.Items.Count; i++)
{
//Retrieve the state of the CheckBox
CheckBox cb = (CheckBox)rptItemsInCart.Items[i].FindControl(
"CheckBox1"
);
if
(cb.Checked)
{
//Retrieve the value associated with that CheckBox
HiddenField hiddentext = (HiddenField)rptItemsInCart.Items[i].FindControl(
"HiddenField1"
);
//Now we can use that value to do whatever we want
Label2.Text = hiddentext.Value;
create1.user_select_weeks_list.Add(hiddentext.Value);
}
}
selected_weeks_repeater.DataSource = create1.user_select_weeks_list;
selected_weeks_repeater.DataBind();
foreach
(DateTime dates1
in
A_Week_1)
{
Response.Write(dates1);
}
}
public
void
Week_button_Click(
object
sender, EventArgs e)
{
DataTable excel_data_gd =
new
DataTable();
DataColumn edg_column;
DataRow edg_row;
//get the data from repeater control
AllLists create1 =
new
AllLists();
for
(
int
i = 0; i < selected_weeks_repeater.Items.Count; i++)
{
Label pass_as_label = (Label)selected_weeks_repeater.Items[i].FindControl(
"store_user_sheet"
);
create1.user_select_weeks_list.Add(pass_as_label.Text);
}
//for(int j = 0; j < create1.user_select_weeks_list.Count; j++)
//{
// Response.Write(create1.user_select_weeks_list[j]);
//}
// Column1 - Machine Name
edg_column =
new
DataColumn();
edg_column.DataType = System.Type.GetType(
"System.String"
);
edg_column.ColumnName =
"Machine Name"
;
edg_column.ReadOnly =
true
;
edg_column.Unique =
true
;
// Add the Column to the DataColumnCollection.
excel_data_gd.Columns.Add(edg_column);
string
[] machinename_array1 = Import_xml_file();
for
(
int
i = 0; i < machinename_array1.Length; i++)
{
edg_row = excel_data_gd.NewRow();
edg_row[
"Machine Name"
] = machinename_array1[i];
//import from excel the value
excel_data_gd.Rows.Add(edg_row);
}
//check if the user has selected any value or not
if
(create1.user_select_weeks_list.Contains(
"Week2"
))
{
//check got how many dates within the week
int
count_of_dates = A_Week_2.Count;
// The dates column, create a new column for every date within the week
for
(
int
i = 0; i < count_of_dates; i++)
{
edg_column =
new
DataColumn();
edg_column.DataType = System.Type.GetType(
"System.DateTime"
);
edg_column.ColumnName = A_Week_2[i].ToString();
edg_column.ReadOnly =
true
;
edg_column.Unique =
true
;
// Add the Column to the DataColumnCollection.
excel_data_gd.Columns.Add(edg_column);
}
}
GridView1.DataSource = excel_data_gd;
GridView1.DataBind();
}
protected
string
[] Import_xml_file()
{
XmlDocument doc =
new
XmlDocument();
doc.Load(Server.MapPath(
"~/App_Data/List_of_machines.xml"
));
//Display all the Activities alone in the XML
XmlNodeList elemList = doc.GetElementsByTagName(
"machine_name"
);
string
[] machinename_array =
new
string
[elemList.Count];
for
(
int
i = 0; i < elemList.Count; i++)
{
machinename_array[i] = elemList[i].InnerXml;
}
return
machinename_array;
}
}
What is the best way of retaining the values, ViewState or Session, I read that viewstate if used with large data can cause the page to load slower, how can I go about solving this.
Reply
Answers (
1
)
How to get values from repeater control rows?
Get maxid for different sub models in Asp.Net