|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Checkbox state after postbackI have a pge on which is a placeholder. I fill the placeholder at runtime with a number of dynamically created controls. Most of these work just fine, except my checkbox. When I first load the page the chekbox displays correctly either checked or not. After a postback however (the controls are all rebuilt) IF the checkbox was checked before postback - it is checked again after postback - which is fine. BUT if the checkbox was NOT checked - after the postback it shows up as checked. I have debugged the code and it is definitely 'Checked = False - even after adding to the placeholder. What I see on the page is a new attribute : checked="checked". NB I even tried forcing the output of an attribute : checked="false" - which is ALSO output but has no effect. I have tried placeholder1.controls.clear(). I have tried Viewstete.clear() No effect. Anyone got an idea ????. NB The actual controls are in a module on a DotNetNuke page :-O. Module written in C#. CheckBox ck = new CheckBox(); ck.EnableViewState = true; ck.Checked = false; if (Convert.ToBoolean(ds.Tables[tablename].Rows[0][ctl.field].ToString())) { ck.Checked = true; ck.Enabled = true; ck.InputAttributes.Add("checked", "true"); } else { ck.Checked = false; ck.Enabled = true; ck.Attributes.Remove("checked"); // ck.InputAttributes.Add("checked", "false"); } //ck.ID = ctl.name; ck.TextAlign = TextAlign.Left; ck.Text = ctl.text; ck.CssClass = ctl.cssclass; ph.Controls.Add(ck); Thanks - hopefully Graham Actually - I stated the case a little incorrectly. The checkbox state is
controlled by a database lookup. If it was checked on the page, but when read from the database should be unchecked, this is when it goes wrong. IE Checked on the page. Database says should be unchecked, so set up as unchecked when creating the checkbox, but it still displays on the page as checked. Thus once its been shown as checked on the page, no matter how it is recreated it shows as checked, until I refresh the whole page from scratch :-O. Show quoteHide quote "Oxns" <oxns@community.nospam> wrote in message news:u32sAoLMGHA.1760@TK2MSFTNGP10.phx.gbl... > Hi, > > I have a pge on which is a placeholder. I fill the placeholder at runtime > with a number of dynamically created controls. Most of these work just > fine, except my checkbox. > > When I first load the page the chekbox displays correctly either checked > or not. > > After a postback however (the controls are all rebuilt) IF the checkbox > was checked before postback - it is checked again after postback - which > is fine. BUT if the checkbox was NOT checked - after the postback it shows > up as checked. > > I have debugged the code and it is definitely 'Checked = False - even > after adding to the placeholder. > > What I see on the page is a new attribute : checked="checked". > > NB I even tried forcing the output of an attribute : checked="false" - > which is ALSO output but has no effect. > > I have tried placeholder1.controls.clear(). > > I have tried Viewstete.clear() > > No effect. > > Anyone got an idea ????. > > NB The actual controls are in a module on a DotNetNuke page :-O. Module > written in C#. > > CheckBox ck = new CheckBox(); > > ck.EnableViewState = true; > > ck.Checked = false; > > if (Convert.ToBoolean(ds.Tables[tablename].Rows[0][ctl.field].ToString())) > > { > > ck.Checked = true; > > ck.Enabled = true; > > ck.InputAttributes.Add("checked", "true"); > > } > > else > > { > > ck.Checked = false; > > ck.Enabled = true; > > ck.Attributes.Remove("checked"); > > // ck.InputAttributes.Add("checked", "false"); > > } > > //ck.ID = ctl.name; > > ck.TextAlign = TextAlign.Left; > > ck.Text = ctl.text; > > ck.CssClass = ctl.cssclass; > > ph.Controls.Add(ck); > > > Thanks - hopefully > > Graham > > Hi Graham,
Welcome to the ASP.NET newsgroup. As for the dynamically created webcontrols, we should always create and add them into the container control in page's init or load event(init is prefered). After the control is added into container control in page, if it is during postback, the control will try restore its state from ViewState. Therefore, we should not set any state onto dynamic created controls before they're added into container control. Also, for your scenario, I suggest you consider adding the dynamic controls in page init event and do other data binding or load operations in Page_load, e.g: =========(this is an ASP.NET 2.0 page)========= public partial class DynaControlPage : System.Web.UI.Page { private CheckBox _chk; protected void Page_Init(object sender, EventArgs e) { _chk = new CheckBox(); _chk.ID = "chkDynamic"; _chk.Text = "Dynamic CheckBox"; ph1.Controls.Add(_chk); } protected void Page_Load(object sender, EventArgs e) { _chk.Checked = CheckBox1.Checked; } } ============== In addition , here is a kb article describing dynamically creating webcontrol on page: #HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET http://support.microsoft.com/kb/317794/en-us Hope this helps. Regards, Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
radiobuttonlist.selectedIndex always -1.........
Checkbox in datagrid.... How to read value of a dynamically created radiobuttonlist control? asp.net control to run in web page like just COM Trying to set stored procedure parameter for sqldatasource in formview (edit mode) refresh gridview asp:Repeater + no viewstate + DataBind() ?? Where are the events of the aspx? Looking for graphic control to use in .NET FileUpload Control |
|||||||||||||||||||||||