|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Server Control - object not saved when button clickedI am trying to create a server control with some radiobuttons (will be a survey control). If a radiobutton is clicked (SelectedChanged) then the attribute "selectedValue" should be set to the id of the radiobutton.... so far, there is no problem..... but when i press the submit button at the end of my aspx the set attribute in my control class is not there.... here is the code of my class: ------------------------------------------------------ using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CompositionSampleControls { public class Composition3 : Control, INamingContainer { private String selectedvalue; public int Value { get { this.EnsureChildControls(); return Int32.Parse(((TextBox)Controls[3]).Text); } set { this.EnsureChildControls(); ((TextBox)Controls[3]).Text = value.ToString(); } } public virtual String SelectedValue { get { this.EnsureChildControls(); return selectedvalue; } set { this.EnsureChildControls(); selectedvalue = value.ToString(); } } protected override void CreateChildControls() { Label LeftHandLabel = new Label(); LeftHandLabel.Text = LeftHandText; this.Controls.Add(LeftHandLabel); this.Controls.Add(new LiteralControl("     ")); for (int i = 1; i <= NumberOfRadio; i++) { RadioButton myRadio = new RadioButton(); myRadio.AutoPostBack = true; myRadio.ID = (Convert.ToString(i)); myRadio.GroupName = "klaus"; myRadio.CheckedChanged += new EventHandler(this.RadioButton_CheckedChanged); this.Controls.Add(myRadio); } this.Controls.Add(new LiteralControl("</h3>")); } private void RadioButton_CheckedChanged(Object sender, EventArgs e) { if (((RadioButton)sender).Checked) { this.SelectedValue = ((RadioButton)sender).ID; this.Controls.Add(new LiteralControl(this.selectedvalue)); } } } } -------------------------------- and the code an my aspx: ---------------------------------- protected void Button1_Click2(object sender, EventArgs e) { Response.Write("Eins:" + Composition3_1.SelectedValue+ "<br>"); Response.Write("Zwei " + Composition3_2.SelectedValue);} ------------------------------- The problem again: I do not get an value when I call Composition3_1.SelectedValue. But I should get one, because my class sets the value, when I click on a radiobutton. Perhaps you have an idea. Perhaps I lose the changes in my object (myobject.selectedvalue) when I click the button on my aspx (postback problem?). Thanks a lot. Gellert Hi Gellert,
Is your control "Composition3" being loaded during the page initialization stage? Dynamically loading controls must be done during the Page.Init event handling (page initialization stage) otherwise they would lose they ViewState and their events would not fire. For a simple demo on this concept: http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx For a clear explanation of the page life cycle: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp Show quoteHide quote "Gellert" wrote: > Hello, > > I am trying to create a server control with some radiobuttons (will be > a survey control). > If a radiobutton is clicked (SelectedChanged) then the attribute > "selectedValue" should be set to the id of the radiobutton.... so far, > there is no problem..... but when i press the submit button at the end > of my aspx the set attribute in my control class is not there.... > > here is the code of my class: > ------------------------------------------------------ > > using System; > using System.Web; > using System.Web.UI; > using System.Web.UI.WebControls; > > namespace CompositionSampleControls > { > > public class Composition3 : Control, INamingContainer > { > private String selectedvalue; > > > public int Value > { > get > { > this.EnsureChildControls(); > return Int32.Parse(((TextBox)Controls[3]).Text); > } > set > { > this.EnsureChildControls(); > ((TextBox)Controls[3]).Text = value.ToString(); > } > } > > public virtual String SelectedValue > { > get > { > this.EnsureChildControls(); > return selectedvalue; > > } > set > { > this.EnsureChildControls(); > selectedvalue = value.ToString(); > } > } > > protected override void CreateChildControls() > { > Label LeftHandLabel = new Label(); > LeftHandLabel.Text = LeftHandText; > this.Controls.Add(LeftHandLabel); > > this.Controls.Add(new LiteralControl(" ")); > > for (int i = 1; i <= NumberOfRadio; i++) > { > RadioButton myRadio = new RadioButton(); > myRadio.AutoPostBack = true; > myRadio.ID = (Convert.ToString(i)); > myRadio.GroupName = "klaus"; > myRadio.CheckedChanged += new > EventHandler(this.RadioButton_CheckedChanged); > this.Controls.Add(myRadio); > } > > this.Controls.Add(new LiteralControl("</h3>")); > > } > > private void RadioButton_CheckedChanged(Object sender, > EventArgs e) > { > if (((RadioButton)sender).Checked) > { > this.SelectedValue = ((RadioButton)sender).ID; > this.Controls.Add(new > LiteralControl(this.selectedvalue)); > } > } > } > } > -------------------------------- > > and the code an my aspx: > > ---------------------------------- > protected void Button1_Click2(object sender, EventArgs e) > { > Response.Write("Eins:" + Composition3_1.SelectedValue+ "<br>"); > Response.Write("Zwei " + Composition3_2.SelectedValue); > > } > ------------------------------- > > > The problem again: I do not get an value when I call > Composition3_1.SelectedValue. But I should get one, because my class > sets the value, when I click on a radiobutton. > > > > Perhaps you have an idea. > Perhaps I lose the changes in my object (myobject.selectedvalue) when I > click the button on my aspx (postback problem?). > > Thanks a lot. > Gellert > > Dear Phillip,
Thanks for your answer. There is no problem to fire the event. It's allready working. The problem is to save the selectedvalue state in my object class so that I can use: Response.Write(Composite3_1.SelectedValue); in my aspx page, when a button is clicked. It is not working yet because I do not get an Value if I type Composite3_1.SelectedValue. it seems to be empty, but I set an SelectedValue here when the event had been fired: --------------- private void RadioButton_CheckedChanged(Object sender,EventArgs e) { if (((RadioButton)sender).Checked) { this.SelectedValue = ((RadioButton)sender).ID; } } ---------------- Like it works with a standard dropdown list: mydropdown.selectedValue Thanks for the articles as well, I will go through them, perhaps I have not considered something. Perhaps you have an idea again ?!? ;-) Gellert You are welcome Gillert. I did not notice in your code that you are posting
back twice; first postback was on the RadioButton.Click and the second on the Button1.Click. Had you disabled the autopostback for the RadioButton your code would have worked just fine. Show quoteHide quote "Gellert" wrote: > Dear Phillip, > > Thanks for your answer. > > There is no problem to fire the event. It's allready working. > The problem is to save the selectedvalue state in my object class so > that I can use: > Response.Write(Composite3_1.SelectedValue); > in my aspx page, when a button is clicked. > > It is not working yet because I do not get an Value if I type > Composite3_1.SelectedValue. it seems to be empty, but I set an > SelectedValue here when the event had been fired: > > --------------- > private void RadioButton_CheckedChanged(Object sender,EventArgs e) > { > if (((RadioButton)sender).Checked) > { > this.SelectedValue = ((RadioButton)sender).ID; > } > } > ---------------- > > > Like it works with a standard dropdown list: mydropdown.selectedValue > > Thanks for the articles as well, I will go through them, perhaps I have > not considered something. > > Perhaps you have an idea again ?!? ;-) > > Gellert > > -- > Sent via .NET Newsgroups > http://www.dotnetnewsgroups.com > Hello all,
I could solved the Problem with an article "Extending a TextBox Control in ASP.NET" by Anthony Hart. Here is the important part is to use the ViewState: get { this.EnsureChildControls(); //This was the old code: return selectedvalue; return ((string)ViewState["something"]); } set { this.EnsureChildControls(); //This was the old code: selectedvalue = value.ToString(); This was the old code ViewState["something"] = value; Now it is working. here is the Link to the article: http://www.eggheadcafe.com/articles/pfc/extendservercontrol.asp Look at the "ViewState" section ! Gellert
custom control client values gone with postback
Cool Web Parts? Newbie - question concerning page behaviour Mouseover, mouseout, click etc on datagrid. DataList Issue Problems with Custom Web Controls in VS 2005 Time textbox control Mailto Link in Gridview TableAdapter Scalar or "Single Value" Queries regular expression |
|||||||||||||||||||||||