I am getting hard time using required field validator in the radio buttons which are under the gridview. My codes is like this:
<asp:GridView ID="gViewEval" runat="server" AutoGenerateColumns="False" Visible="true">
<Columns>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="lblQHeader" runat="server" Text="Columns/Row" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblQDetails" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem,"text") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="lbltxt1" runat="server" Text = 'text1'/>
</HeaderTemplate>
<ItemTemplate >
<asp:RadioButtonList ID="radA" runat="server" >
<asp:ListItem Value = "1" Text="" />
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="text2" runat="server" Text = 'text2'/>
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButtonList ID="radA" runat="server">
<asp:ListItem Value = "2" Text="" />
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="text3" runat="server" Text = 'text3'/>
</HeaderTemplate>
<ItemTemplate >
<asp:RadioButtonList ID="radA" runat="server">
<asp:ListItem Value = "3" Text="" />
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="text4" runat="server" Text = 'text4'/>
</HeaderTemplate>
<ItemTemplate >
<asp:RadioButtonList ID="radA" runat="server">
<asp:ListItem Value = "4" Text="" />
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="text5" runat="server" Text = 'text5'/>
</HeaderTemplate>
<ItemTemplate >
<asp:RadioButtonList ID="radA" runat="server">
<asp:ListItem Value = "5" Text="" />
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="radAns"
ErrorMessage="atleast one radio buttons should be selected in each row" CssClass="valError"
Display="Dynamic" ForeColor="">!</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I wanted to add requiredFieldValidator on those radiobuttons. So I put "rfv" as requiredFieldValidator but it gave error when i ran the program. Error is:
Exception Details: System.Web.HttpException: Multiple controls with the same ID 'radA' were found. FindControl requires that controls have unique IDs.
If that is not the right way, how can we put requiredFieldValidator in this scenario?
The error youre getting is because you have multiple radiobutton lists with the same ID. Simple rename the ID's of each radiobutton list in your templates with a unique name. Or if you need to create them dynamically, remove the ID field and let Visual
studio create them for you, specify the ClientIDMode = Predictable.
I am using the gridview and it is pulling data from the database. The number of rows in the gridview is unknown and since it is based on the database value, the radio button is dynamic. So i am using one radio button and the values are in the group. And
the way I can use radiobutton in the different cells of the gridview is by using radio button with the same IDs. but how would i be able to use required field validator in this case?
This is a snippet on a project I created with dynamic controls. See if it applies to you
publicvoid CreateAnswers(UserControl userControl, QUESTION question, IEnumerable<ANSWER> answers, Control control, string extension = "")
{
var controlType = question.QUESTION_TYPE;
var getTypeByStringName = TypeMapper(controlType);
if (getTypeByStringName != null)
{
var newControl = Activator.CreateInstance(getTypeByStringName);
var castedControl = newControl asControl;
if (castedControl != null)
{
if (castedControl isDropDownList)
{
var dropDown = castedControl asDropDownList;
//in cases where the relationship is 1:N e.g a user is to its employment history//dynamically set the controlID with an extension string//e.g 24-2, 24-2, 24-2 (24 is the control id plus the extension)//e.g 25-3. 25-3, 25-3 (25 is the control id plus the extension)//else use the default naming scheme which is the question ID only
dropDown.ID = string.IsNullOrEmpty(extension)
? question.Q_ID.ToString()
: string.Format("{0}{1}", question.Q_ID, extension);
foreach (var answer in answers)
{
dropDown.Items.Add(newListItem {Text = answer.ANSWER_TEXT, Value = answer.VALUE});
}
dropDown.ClientIDMode = ClientIDMode.Static;
dropDown.Items.Insert(0, newListItem { Text = "Select", Value = "" });
dropDown.Items.FindByText("Select").Selected = true;
dropDown.CssClass = "answerCss";
control.Controls.Add(dropDown);
if (question.REQUIRED == "Y") CreateValidator(dropDown, control,userControl,extension);
if (question.CONTINGENT_Q.HasValue) dropDown.Visible = false;
}
elseif (castedControl isRadioButtonList)
{
var radioButtonList = castedControl asRadioButtonList;
radioButtonList.ID = string.IsNullOrEmpty(extension)
? question.Q_ID.ToString()
: string.Format("{0}{1}", question.Q_ID, extension);
radioButtonList.SelectedIndexChanged += rbl_SelectedIndexChanged;
radioButtonList.AutoPostBack = true;
///<summary>/// This method creates the required field validator based on the REQUIRED field of a given question///</summary>///<param name="controlToValidate">the dynamically created control that needs to be validated</param>///<param name="parentControl">the parent container control</param>privatestaticvoid CreateValidator(Control controlToValidate,Control parentControl, UserControl userControl, string extension)
{
var rfv = newRequiredFieldValidator
{
ClientIDMode = ClientIDMode.Static,
ID = String.Format("rfv{0}", controlToValidate.ClientID),
ControlToValidate = controlToValidate.ID,
Display = ValidatorDisplay.Static,
Text = "*Required Field",
ErrorMessage = "Required field: " + controlToValidate.ID + " is empty",
ForeColor = System.Drawing.Color.Red,
//set the validation group as the name of the usercontrol if there is no extension, otherwise append the extension
ValidationGroup = string.IsNullOrEmpty(extension) ? userControl.ClientID : string.Format("{0}{1}",userControl.ClientID,extension)
};
parentControl.Controls.Add(rfv);
}
Why do you use several RadioButtonLists for only one option each? I think you can just use one RadioButtonList with 5 options in ListItems. Then the error will go.
SuchiSayami
Member
54 Points
102 Posts
required field validator on group of checkbox
May 29, 2012 05:53 PM|LINK
Hi,
I am getting hard time using required field validator in the radio buttons which are under the gridview. My codes is like this:
<asp:GridView ID="gViewEval" runat="server" AutoGenerateColumns="False" Visible="true"> <Columns> <asp:TemplateField > <HeaderTemplate> <asp:Label ID="lblQHeader" runat="server" Text="Columns/Row" /> </HeaderTemplate> <ItemTemplate> <asp:Label ID="lblQDetails" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem,"text") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField > <HeaderTemplate> <asp:Label ID="lbltxt1" runat="server" Text = 'text1'/> </HeaderTemplate> <ItemTemplate > <asp:RadioButtonList ID="radA" runat="server" > <asp:ListItem Value = "1" Text="" /> </asp:RadioButtonList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField > <HeaderTemplate> <asp:Label ID="text2" runat="server" Text = 'text2'/> </HeaderTemplate> <ItemTemplate> <asp:RadioButtonList ID="radA" runat="server"> <asp:ListItem Value = "2" Text="" /> </asp:RadioButtonList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField > <HeaderTemplate> <asp:Label ID="text3" runat="server" Text = 'text3'/> </HeaderTemplate> <ItemTemplate > <asp:RadioButtonList ID="radA" runat="server"> <asp:ListItem Value = "3" Text="" /> </asp:RadioButtonList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField > <HeaderTemplate> <asp:Label ID="text4" runat="server" Text = 'text4'/> </HeaderTemplate> <ItemTemplate > <asp:RadioButtonList ID="radA" runat="server"> <asp:ListItem Value = "4" Text="" /> </asp:RadioButtonList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField > <HeaderTemplate> <asp:Label ID="text5" runat="server" Text = 'text5'/> </HeaderTemplate> <ItemTemplate > <asp:RadioButtonList ID="radA" runat="server"> <asp:ListItem Value = "5" Text="" /> </asp:RadioButtonList> <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="radAns" ErrorMessage="atleast one radio buttons should be selected in each row" CssClass="valError" Display="Dynamic" ForeColor="">!</asp:RequiredFieldValidator> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>I wanted to add requiredFieldValidator on those radiobuttons. So I put "rfv" as requiredFieldValidator but it gave error when i ran the program. Error is:
If that is not the right way, how can we put requiredFieldValidator in this scenario?
Please help.
amosCabanban...
Member
441 Points
142 Posts
Re: required field validator on group of checkbox
May 29, 2012 06:04 PM|LINK
The error youre getting is because you have multiple radiobutton lists with the same ID. Simple rename the ID's of each radiobutton list in your templates with a unique name. Or if you need to create them dynamically, remove the ID field and let Visual studio create them for you, specify the ClientIDMode = Predictable.
SuchiSayami
Member
54 Points
102 Posts
Re: required field validator on group of checkbox
May 29, 2012 06:08 PM|LINK
I am using the gridview and it is pulling data from the database. The number of rows in the gridview is unknown and since it is based on the database value, the radio button is dynamic. So i am using one radio button and the values are in the group. And the way I can use radiobutton in the different cells of the gridview is by using radio button with the same IDs. but how would i be able to use required field validator in this case?
amosCabanban...
Member
441 Points
142 Posts
Re: required field validator on group of checkbox
May 29, 2012 06:27 PM|LINK
SuchiSayami
Member
54 Points
102 Posts
Re: required field validator on group of checkbox
May 29, 2012 06:59 PM|LINK
Is there any way I can do it using gridview?
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: required field validator on group of checkbox
May 31, 2012 08:57 AM|LINK
Hi,
Why do you use several RadioButtonLists for only one option each? I think you can just use one RadioButtonList with 5 options in ListItems. Then the error will go.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework