I have a custom TextBoxlist which inherits the listcontrol but the problem is that
the items in the listcontrol which are textboxes donot maintain viewstate after postback and the initial values are being loaded everytime.I have overridden the
loadviewstate and saveviewstate methods and saved the listitem values that is text in textboxes in saveviewstate method but those values donot seem to have
saved.
Please try to give me a solution
The complete code in code-behind is below.
Expand|Select|Wrap|Line Numbers
- public class TextBoxList:ListControl,IRepeatInfoUser
- {
- private TextBox controlToRepeat;
- public TextBoxList()
- {
- this.controlToRepeat = new TextBox();
- this.controlToRepeat.ID = "0";
- this.controlToRepeat.EnableViewState = false;
- this.Controls.Add(this.controlToRepeat);
- }
- protected override object SaveViewState()
- {
- object[] state = new object[this.Items.Count];
- for (int i = 0; i < this.Items.Count; i++)
- {
- state[i] = this.Items[i].Text;
- }
- return state;
- }
- protected override void LoadViewState(object savedState)
- {
- if (savedState == null) return;
- if (savedState is object[])
- {
- object[] state = (object[])savedState;
- for (int i = 0; i < state.Length; i++)
- {
- this.Items[i].Text = state[i].ToString();
- }
- }
- else
- base.LoadViewState(savedState);
- }
- protected override void Render(HtmlTextWriter writer)
- {
- RepeatInfo info1 = new RepeatInfo();
- Style style1 = base.ControlStyleCreated ? base.ControlStyle : null;
- info1.RepeatColumns = 1;
- info1.RepeatDirection = RepeatDirection.Vertical;
- info1.RepeatLayout = RepeatLayout.Table;
- info1.RenderRepeater(writer, this, style1, this);
- }
- public bool HasHeader
- {
- get
- {
- return false;
- }
- }
- public bool HasSeparators
- {
- get
- {
- return false;
- }
- }
- public bool HasFooter
- {
- get
- {
- return false;
- }
- }
- public void RenderItem(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
- {
- this.controlToRepeat.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
- this.controlToRepeat.Text = this.Items[repeatIndex].Text;
- this.controlToRepeat.RenderControl(writer);
- }
- public Style GetItemStyle(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex)
- {
- return null;
- }
- public int RepeatedItemCount
- {
- get
- {
- return this.Items.Count;
- }
- }
- }
ThankYou
vijay