Hello,
Why do not all three radiobuttons display when I drop the control into the
gridview's templated field?
Based on my understanding, if you don't set any TextForXXX property, it
just show only one RadioButton in GridView. Right?
I tried in my site, if I don't set any Text, it will get empty as Text for
each radiobutton. That will present only three alone radiobutton without
any text beside it. I think you used some css style and block the other
two
radiobutton.
To resolve it, we can set the default value for three TextForXXX property.
So it will never get empty Text on the control.
[Browsable(true)]
[DefaultValue("")]
[Category("Appearance")]
[Description("")]
public string TextForOn
{
get { return (string)(ViewState["TextForOn"] ?? "On"); }
set { ViewState["TextForOn"] = value; }
}
[Browsable(true)]
[DefaultValue("")]
[Category("Appearance")]
[Description("")]
public string TextForOff
{
get { return (string)(ViewState["TextForOff"] ?? "Off"); }
set { ViewState["TextForOff"] = value; }
}
[Browsable(true)]
[DefaultValue("")]
[Category("Appearance")]
[Description("")]
public string TextForNull
{
get { return (string)(ViewState["TextForNull"] ?? "Null"); }
set { ViewState["TextForNull"] = value; }
}
Why does the page have to run before the text that is set via the
properties
browser show up?
Property TextForXXX is allowing you set the default value for it.(I think
you had better say it as DefaultTextForXXX) In databound event, items have
been created, so you can't modify it directly by TextForXXX unless you
loop
the items and set the text for each item.
If you want to modify TextForXXX at anytime, you can build onPreRender
phase to rebind the Text to update it.
protected override void OnPreRender(EventArgs e)
{
this.Items[0].Text = TextForOn;
this.Items[1].Text = TextForOff;
this.Items[2].Text = TextForNull;
base.OnPreRender(e);
}
So the entire codes about it is as below:
public class myRadioButton : RadioButtonList
{
public myRadioButton()
{
}
private List<string> _valueList=new List<string> {
"on","off","null"};
[Browsable(true)]
[DefaultValue("")]
[Category("Appearance")]
[Description("")]
public string TextForOn
{
get { return (string)(ViewState["TextForOn"] ?? "On"); }
set { ViewState["TextForOn"] = value; }
}
[Browsable(true)]
[DefaultValue("")]
[Category("Appearance")]
[Description("")]
public string TextForOff
{
get { return (string)(ViewState["TextForOff"] ?? "Off"); }
set { ViewState["TextForOff"] = value; }
}
[Browsable(true)]
[DefaultValue("")]
[Category("Appearance")]
[Description("")]
public string TextForNull
{
get { return (string)(ViewState["TextForNull"] ?? "Null"); }
set { ViewState["TextForNull"] = value; }
}
[Browsable(true)]
[DefaultValue("")]
[Category("Appearance")]
[Description("")]
public override string SelectedValue
{
get
{
EnsureChildControls();
return base.SelectedValue;
}
set
{
EnsureChildControls();
if (string.IsNullOrEmpty(value))
base.SelectedValue = _valueList[2];
else
if (value.ToString().Substring(0, 1).ToLower() == "t")
base.SelectedValue = _valueList[0];
else if (value.ToString().Substring(0, 1).ToLower() ==
"f")
base.SelectedValue = _valueList[1];
else
throw new Exception("SelectedValue bound is
invide.");
}
}
protected override void CreateChildControls() //
OnDataBinding(EventArgs e) you can use DataBinding instead.
{
this.Items.Clear();
this.Items.Add(new ListItem(TextForOn, _valueList[0]));
this.Items.Add(new ListItem(TextForOff, _valueList[1]));
this.Items.Add(new ListItem(TextForNull, _valueList[2]));
base.CreateChildControls();//base.OnDataBinding(e);
}
protected override void OnPreRender(EventArgs e)//that will refresh
the text before render
{
this.Items[0].Text = TextForOn;
this.Items[1].Text = TextForOff;
this.Items[2].Text = TextForNull;
base.OnPreRender(e);
}
}
Hope it can help you.
Sincerely,
Vince Xu
Microsoft Online Support