Override loadviewstate is not firing

Posted by Divs99 under C# on 7/18/2012 | Points: 10 | Views : 1419 | Status : [Member] | Replies : 2
protected



void Page_Load(object sender, EventArgs e)



{



if (!IsPostBack)



{

lbCollection.Items.Clear();



ListItem lItem = new ListItem("Test1", "1");



lItem.Attributes.Add(

"D", "1");



lbCollection.Items.Add(lItem);





}

}

------------------------------------------------------------



protected override object SaveViewState()



{



// Create an object array with one element for the CheckBoxList's





// ViewState contents, and one element for each ListItem in skmCheckBoxList





object[] state = new object[this.lbCollection.Items.Count + 1];





object baseState = base.SaveViewState();



state[0] = baseState;



// Now, see if we even need to save the view state





bool itemHasAttributes = false;





for (int i = 0; i < this.lbCollection.Items.Count; i++)



{



if (this.lbCollection.Items[i].Attributes.Count > 0)



{

itemHasAttributes =

true;





// Create an array of the item's Attribute's keys and values





object[] attribKV = new object[this.lbCollection.Items[i].Attributes.Count * 2];





int k = 0;





foreach (string key in this.lbCollection.Items[i].Attributes.Keys)



{

attribKV[k++] = key;

attribKV[k++] =

this.lbCollection.Items[i].Attributes[key];



}

state[i + 1] = attribKV;

}

}



// return either baseState or state, depending on whether or not





// any ListItems had attributes





if (itemHasAttributes)





return state;





else





return baseState;



}





--------------------------------------------------------------------------------



protected



override void LoadViewState(object savedState)



{



if (savedState == null) return;





// see if savedState is an object or object array













if (savedState is object[])



{



// we have an array of items with attributes













object[] state = (object[])savedState;





base.LoadViewState(state[0]); // load the base state-----------------------not able to load base state ..getting null in state[0]












for (int i = 1; i < state.Length; i++)



{



if (state[i] != null)



{



// Load back in the attributes













object[] attribKV = (object[])state[i];





for (int k = 0; k < attribKV.Length; k += 2)





this.lbCollection.Items[i - 1].Attributes.Add(attribKV[k].ToString(),attribKV[k + 1].ToString());---iam getting error as index out of range becoz on postback iam not getting listbox items...iam getting listbox items count as zero



}

}

}



else













// we have just the base state













base.LoadViewState(savedState);



}




Responses

Posted by: Jayakumars on: 9/30/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

stored object value and use them or use session value.

viewstate only current page
session whole project



Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Divs99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/30/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
Please mark as answer if it helpful to you.

Mark as Answer if its helpful to you

Kumaraspcode2009@gmail.com

Divs99, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response