Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Customvalidator and repeater

Posted on 2010-08-30
2
Medium Priority
?
2,046 Views
Last Modified: 2012-05-10
Part of this works. I have a repeater with a radiobutton and a textbox. If radiobutton is "YES", user has to enter something in the textbox.

I added a CustomValidator and OnServerValidate="test" with ControlToValidate="rbQuestions". I have a server side event for this as well.

For the textbox, i added a RequiredFieldValidator and disabled it.

In the OnServervaidate, I check the Radiobutton value = "Y". So far it all works

But now, when the radiobutton is YES, i dont know how to check for the textbox not being empty. I did below but nothing gets validated.

My repeater and event code are below:
protected void test(Object source, ServerValidateEventArgs args)
    {
        if (args.Value == "Y") //this reads the radiobutton value
        {

            foreach (RepeaterItem item in rpQuestions.Items)
            {
                
                RadioButtonList rbQuestion = (RadioButtonList)item.FindControl("rbQuestions");
                RequiredFieldValidator rfv = (RequiredFieldValidator) item.FindControl("rfvExplain");
                

                    //for (int i = 0; i <= rbQuestion.Items.Count - 1; i++)
                    //{
                    //    if (rbQuestion.Items[i].Value == "Y") //.SelectedItem.
                            rfv.Enabled = true;

                    //    else
                    //        rfv.Enabled = false;
                    //}
            }
        }
    }


--------------- repeater

<asp:Repeater runat="server" OnItemDataBound="rpQuestions_OnItemDataBound"   ID="rpQuestions">
              <ItemTemplate>
              <tr>
              <td>
                <asp:Label runat="server" ID="lblId" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "QuestionId") %>'></asp:Label>
              </td>
               <td   align="left">
                
                <asp:Label runat="server"  ID="lblQuestion" Text='<%# DataBinder.Eval(Container.DataItem, "Question") %>'></asp:Label>
               </td>
               <td  align="right">
               
                 <!-- OnSelectedIndexChanged="rbQuestions_OnSelectedIndexChanged" AutoPostBack="true" -->
                 <asp:RadioButtonList   runat="server" ID="rbQuestions"   TextAlign="Right" RepeatDirection="Horizontal">
                   <asp:ListItem Value="Y"  Text="Yes"></asp:ListItem>
                   <asp:ListItem Value="N" Text="No"></asp:ListItem>
                 </asp:RadioButtonList>
               
                   
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator10" Display="Dynamic" ControlToValidate="rbQuestions" ErrorMessage="Employment Questions" Text="*" runat="server"></asp:RequiredFieldValidator>     
               
                </td>
              </tr>
              <tr>
              <td></td>
              <td><asp:Label ID="lblExplain" Text='<%# DataBinder.Eval(Container.DataItem, "Explain") %>' runat="server" ></asp:Label></td>
               <td>
                 <asp:TextBox runat="server"   ID="txtExplanation"  TextMode="MultiLine"></asp:TextBox>
                <asp:CustomValidator runat="server"  ControlToValidate="rbQuestions" OnServerValidate="test"></asp:CustomValidator>
                  <asp:RequiredFieldValidator Enabled="false" ID="rfvExplain" Display="Dynamic" ControlToValidate="txtExplanation" ErrorMessage="*" Text="*" runat="server"></asp:RequiredFieldValidator>      
            
               </td>
              </tr>
            </ItemTemplate>
        </asp:Repeater>

Open in new window

0
Comment
Question by:Camillia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Stephan earned 2000 total points
ID: 33564477
Try this:
protected void test(Object source, ServerValidateEventArgs args)
    {
        if (args.Value == "Y") //this reads the radiobutton value
        {

            foreach (RepeaterItem item in rpQuestions.Items)
            {
                
                RadioButtonList rbQuestion = (RadioButtonList)item.FindControl("rbQuestions");
                RequiredFieldValidator rfv = (RequiredFieldValidator) item.FindControl("rfvExplain");
                
                if(rbQuestion != null)
                {
                   if (rbQuestion.SelectedValue == "Y")
                            rfv.Enabled = true;
                       else
                           rfv.Enabled = false;
                }
                   
            }
        }
    }


--------------- repeater

<asp:Repeater runat="server" OnItemDataBound="rpQuestions_OnItemDataBound"   ID="rpQuestions">
              <ItemTemplate>
              <tr>
              <td>
                <asp:Label runat="server" ID="lblId" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "QuestionId") %>'></asp:Label>
              </td>
               <td   align="left">
                
                <asp:Label runat="server"  ID="lblQuestion" Text='<%# DataBinder.Eval(Container.DataItem, "Question") %>'></asp:Label>
               </td>
               <td  align="right">
               
                 <!-- OnSelectedIndexChanged="rbQuestions_OnSelectedIndexChanged" AutoPostBack="true" -->
                 <asp:RadioButtonList   runat="server" ID="rbQuestions"   TextAlign="Right" RepeatDirection="Horizontal">
                   <asp:ListItem Value="Y"  Text="Yes"></asp:ListItem>
                   <asp:ListItem Value="N" Text="No"></asp:ListItem>
                 </asp:RadioButtonList>
               
                   
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator10" Display="Dynamic" ControlToValidate="rbQuestions" ErrorMessage="Employment Questions" Text="*" runat="server"></asp:RequiredFieldValidator>     
               
                </td>
              </tr>
              <tr>
              <td></td>
              <td><asp:Label ID="lblExplain" Text='<%# DataBinder.Eval(Container.DataItem, "Explain") %>' runat="server" ></asp:Label></td>
               <td>
                 <asp:TextBox runat="server"   ID="txtExplanation"  TextMode="MultiLine"></asp:TextBox>
                <asp:CustomValidator runat="server"  ControlToValidate="rbQuestions" OnServerValidate="test"></asp:CustomValidator>
                  <asp:RequiredFieldValidator Enabled="false" ID="rfvExplain" Display="Dynamic" ControlToValidate="txtExplanation" ErrorMessage="*" Text="*" runat="server"></asp:RequiredFieldValidator>      
            
               </td>
              </tr>
            </ItemTemplate>
        </asp:Repeater>

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 33566182
thanks, let me try and see. Hope it works. I've spent a day on this. I'll post back.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question