Server Control - object not saved when button clicked

G

Gellert

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("&nbsp &nbsp &nbsp"));

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
 
P

Phillip Williams

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

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Gellert said:
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
 
G

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
 
G

Gellert

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
 
P

Phillip Williams

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top