locked
Composite Control - RadioButtonList - RequiredFieldValidator - Possible .Net Bug? RRS feed

  • Question

  • User-778114168 posted

    Hi All,

    My first post (of no doubt, many).

     I'm very new to the .Net enviroment but have managed to create an ASP.Net page along with some C# CompositeControls, however i've have spent all of today trying to fix a problem i'm having.

    The problem...

     I created a Textbox and RequiredFieldValidator CompositeControl which works fine and complied this into a working .dll file for my ASP.Net page.

     Using the same methods, in the same class, I created a second Public Class, a RadioButtonList and RequiredFieldValidator Composite Control. Unfortunately this control does not work.

     The RadioButtonList is declared as a private variable and the List Items are added dynamically inside of the CreateChildControls function where the RadioButtonList control and RFV is added to the this.Controls collection.

     When validating the page it is as if the RFV for the RadioButtonList is missing and unless I call the validate method manually isValid stays set on its defualt value.

     I tried adding the RFV to the Page.Validators collection but this made no difference. I then tried adding it to Page.Controls collection but couldn't get that to work as you can't edit the collection during certain parts of the page life cycle.

     After 9 hours of messing around I found a strange solution... By adding a extra Textbox/HiddenField to this.Controls before of after the RFV, the RFV starts to work as expected.

     Can anyone tell me how I can fix this problem, or if it is a known bug? I'm sort of guessing it's a bug as the other CompositeControl works fine and in the ASP.Net page I am able to use a Radio Button List and Required Field Validator together, I just can't use them together in a Composite Control.

     I will try and get some code on here tomorrow if that is of any help.

     Thanks for your time,

     Daniel

    Monday, August 11, 2008 5:17 PM

Answers

  • User-16411453 posted

    Sounds like it a questionaire, for quality checking.

    I know on the pandaexpress website, they use asp.net, and have radio button list with validation that works.

    I know that selectedindex > -1 is the key to some sort of validator.  Perhaps the custom one. Value Validator

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 13, 2008 2:57 PM

All replies

  • User-778114168 posted

    Hi Again,

    Below is an example of the source code, I have kept what I beleive are the relevant parts and removed the rest.

    The second public class does not function correctly unless lines 43, 62, 63 and 70 are uncommented. Then it functions as expected.

    Does anyone have any suggestions?

     Cheers,

     Daniel

    1    //Some Imports Here
    2    
    3    namespace LBBD.Controls
    4    {
    5        [DefaultProperty("Text")]
    6        [ToolboxData("&lt;{0}:LBBD_TextBox runat=server></{0}:LBBD_TextBox>")]
    7        public class LBBD_TextBox : CompositeControl, INamingContainer
    8        {
    9            private TextBox _myTextBox = new TextBox();
    10           private RequiredFieldValidator _myRFV = new RequiredFieldValidator();
    11   
    12           //Some Getter and Setter Methods Here
    13   
    14           protected override void OnInit(EventArgs e)
    15           {
    16               base.OnInit(e);
    17               _myTextBox.ID = this.UniqueID;
    18   
    19               _myRFV.ControlToValidate = _myTextBox.ID;
    20               _myRFV.EnableClientScript = false;
    21           }
    22   
    23           protected override void CreateChildControls()
    24           {
    25               this.Controls.Add(_myTextBox);
    26               this.Controls.Add(_myRFV);
    27           }
    28   
    29           protected override void Render(HtmlTextWriter writer)
    30           {
    31               _myTextBox.RenderControl(writer);
    32               if (_myRFV.IsValid == false) {writer.WriteBreak();}
    33               _myRFV.RenderControl(writer);
    34           }
    35       }
    36   
    37       [DefaultProperty("isRequired")]
    38       [ToolboxData("&lt;{0}:LBBD_RadioButtonYesNo runat=server></{0}:LBBD_RadioButtonYesNo>")]
    39       public class LBBD_RadioButtonYesNo : CompositeControl, INamingContainer
    40       {
    41           private RadioButtonList _myRadioButtonList = new RadioButtonList();
    42           private RequiredFieldValidator _myRFV = new RequiredFieldValidator();
    43           //private HiddenField _test;
    44   
    45           //Some Getter and Setter Methods Here
    46   
    47           protected override void OnInit(EventArgs e)
    48           {
    49               base.OnInit(e);
    50               _myRadioButtonList.ID = this.UniqueID + "test";
    51               _myRadioButtonList.Items.Add(new ListItem("Yes", "Yes"));
    52               _myRadioButtonList.Items.Add(new ListItem("No", "No"));
    53   
    54               _myRFV.ControlToValidate = _myRadioButtonList.ID;
    55               _myRFV.EnableClientScript = false;
    56           }
    57   
    58           protected override void CreateChildControls()
    59           {
    60               this.Controls.Add(_myRadioButtonList);
    61               this.Controls.Add(_myRFV);
    62               //_test = new HiddenField();
    63               //this.Controls.Add(_test);
    64           }
    65   
    66           public override void RenderControl(HtmlTextWriter writer)
    67           {
    68               _myRadioButtonList.RenderControl(writer);
    69               _myRFV.RenderControl(writer);
    70               //_test.RenderControl(writer);
    71           }
    72       }
    73   }
    
     
    Tuesday, August 12, 2008 6:20 AM
  • User-778114168 posted

    Post Double Submitted

    Tuesday, August 12, 2008 6:20 AM
  • User-16411453 posted

    If you have a radio button list, why not just set one of them  using selected value as the default selection.  Then validation is not needed.

     

    Tuesday, August 12, 2008 6:52 PM
  • User-778114168 posted

    Hi jkirkerx,

    Thanks for the reply!

     I don't really want to set a default value in case a user accidently skips/misses a control and leaves it as an incorrect, default value.

     I was hoping to leave the controls unselected when the page loads and to use the RFV to make sure the user selects a value.

     Thanks for the suggestion though, any other suggestions are very much appreciated...

    Daniel

    Tuesday, August 12, 2008 7:50 PM
  • User-778114168 posted

    Hi Again,

    Just an update and a bit of info in case anyone ever looks at this post.

    I have used a work around which involved extending RadioButtonList and adding the RFV to that. The work around can still be seen below.

    It would still be nice to find out why the original problem was occuring though.

    Oh, and if anyone has any opinions/suggestions for my code below... Please go ahead...

    Cheers,

    Daniel

    1        [DefaultProperty("Value")]
    2        [ToolboxData("&lt;{0}:LBBD_RadioButton runat=server></{0}:LBBD_RadioButton>")]
    3        public class LBBD_RadioButton : RadioButtonList, INamingContainer
    4        {
    5            private RequiredFieldValidator _myRFV = new RequiredFieldValidator();
    6    
    7            public bool isRequired
    8            {
    9                get { return _myRFV.Enabled; }
    10               set { _myRFV.Enabled = value; }
    11           }
    12   
    13           public String ErrorMessage
    14           {
    15               get { return _myRFV.ErrorMessage; }
    16               set { _myRFV.ErrorMessage = value; }
    17           }
    18   
    19           public String Value
    20           {
    21               get { return this.SelectedValue; }
    22               set { this.SelectedValue = value; }
    23           }
    24   
    25           protected override void OnInit(EventArgs e)
    26           {
    27               base.OnInit(e);
    28   
    29               _myRFV.ControlToValidate = this.ID;
    30               _myRFV.Display = ValidatorDisplay.Dynamic;
    31               _myRFV.EnableClientScript = false;
    32               Controls.Add(_myRFV);
    33           }
    34   
    35           public override void RenderControl(HtmlTextWriter writer)
    36           {
    37               base.RenderControl(writer);
    38               _myRFV.RenderControl(writer);
    39           }
    40       }
    
     
    Wednesday, August 13, 2008 9:04 AM
  • User-16411453 posted

    Sounds like it a questionaire, for quality checking.

    I know on the pandaexpress website, they use asp.net, and have radio button list with validation that works.

    I know that selectedindex > -1 is the key to some sort of validator.  Perhaps the custom one. Value Validator

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 13, 2008 2:57 PM