c# - ViewState is cleared on postback in DataBoundControl -
i'm developing control inherits databoundcontrol. databinding works viewstate cleared on postback reason? i've tried sorts of solutions making implement viewstatemodebyid attribute, nothing works. point me in right direction please?
public class test : databoundcontrol, inamingcontainer { protected override void onload(eventargs e) { base.onload(e); if (this.visible && !_isdatabound) { // if developer wants show on click or have call databind() // if control has been put on page , there no code behind need make data , call databind flagfordatabinding(); databind(); } } protected override void onprerender(eventargs e) { if (!_isdatabound || requiresdatabinding) { databind(); } base.onprerender(e); } private bool _isdatabound = false; public override object datasource { { return enumerable.range(0, 10); } set {} } public virtual void flagfordatabinding() { this.datasource = null; if (_isdatabound) { _isdatabound = false; } else this.requiresdatabinding = true; } protected override void performselect() { if (!isboundusingdatasourceid) { // if using .datasource databinding starts here // (because the data has alredy been retrieved when doing .datasource = ) ondatabinding(eventargs.empty); } getdata().select(createdatasourceselectarguments(), ondataready); requiresdatabinding = false; markasdatabound(); ondatabound(eventargs.empty); } protected virtual void ondataready(ienumerable data) { if (isboundusingdatasourceid) { // if using .datasourceid databinding starts here ondatabinding(eventargs.empty); } performdatabinding(data); } protected override void performdatabinding(ienumerable data) { this.controls.clear(); if (data != null) { outputitems((ienumerable<int>)data); } base.performdatabinding(data); } public override void databind() { this.ensurechildcontrols(); if (!_isdatabound || requiresdatabinding) { base.ondatabinding(eventargs.empty); base.databind(); _isdatabound = true; } } protected virtual void outputitems(ienumerable<int> items) { this.controls.clear(); foreach (var item in items) { var lnk = new linkbutton {text = item.tostring(), commandargument = item.tostring()}; lnk.click += lnk_click; this.controls.add(lnk); } this.controls.add(new literal{text = "total: " + total}); } void lnk_click(object sender, eventargs e) { int toadd = int.parse((sender linkbutton).commandargument); total += toadd; flagfordatabinding(); } public int total { { return (int?) viewstate["total"] ?? 0; } set { viewstate["total"] = value; } } }
try setting enableviewstate="true" , viewstatemode="enabled" on databoundcontrol.
also, make sure enableviewstate="true" page well.
https://msdn.microsoft.com/en-us/library/system.web.ui.control.enableviewstate(v=vs.110).aspx
Comments
Post a Comment