473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

dynamical bind required field validator

Hi,
I have a repeater control. it dynamically bind textbox or dropdown base on some condition. i want to bind required field validator dynamically for validate textbox or dropdown.

here is my aspx design
Expand|Select|Wrap|Line Numbers
  1. <asp:Repeater ID="repeaterItemAttribute" runat="server" OnItemDataBound="repeaterItemAttribute_ItemDataBound">
  2.                                 <ItemTemplate>
  3.                                     <li>
  4.                                         <label for="state" class="newlabel">
  5.                                             <%# DataBinder.Eval(Container.DataItem, "AttributeName")%>
  6.                                             :</label>
  7.                                         &nbsp;&nbsp;
  8.                                         <asp:DropDownList ID="dropDownListAttributeValue" runat="server" class="select">
  9.                                         </asp:DropDownList>
  10.                                         <asp:TextBox ID="textBoxAttributeValue" runat="server"></asp:TextBox>
  11.                                         <cc1:AhsCustomValidator ID="requiredFieldValidator" runat="server" Display="None"
  12.                                             ChangeCssClass="text error" ErrorMessage=" is required"></cc1:AhsCustomValidator>
  13.                                     </li>
  14.                                 </ItemTemplate>
  15.                             </asp:Repeater> 
and here is my code behind
Expand|Select|Wrap|Line Numbers
  1. protected void repeaterItemAttribute_ItemDataBound(object sender, RepeaterItemEventArgs e)
  2.         {
  3.             if (e != null)
  4.             {
  5.                 if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
  6.                      (e.Item.ItemType == ListItemType.Item))
  7.                 {
  8.  
  9.                     AhsCustomValidator ahsCustomerValidator =
  10.                         (AhsCustomValidator)e.Item.FindControl(RequiredFieldValidator);
  11.  
  12.                     //Get display style of currently biding item it could be either DDW or SLE
  13.                     String dispayStyle = ((DataRowView)e.Item.DataItem)[2].ToString();
  14.                     //Get a references of TextBox and DrowDownList control binding for each item
  15.                     TextBox textBox = (TextBox)e.Item.FindControl(TextBoxAttributeValue);
  16.                     DropDownList dropDownList = (DropDownList)e.Item.FindControl(DropDownListAttributeValue);
  17.  
  18.                     //display style type DDW display only drop down list otherwise TextBox
  19.                     if (dispayStyle == DisplayStyle.DDW.ToString())
  20.                     {
  21.                         long itemId = long.Parse(((DataRowView)e.Item.DataItem)[4].ToString());
  22.                         long attriubteId = long.Parse(((DataRowView)e.Item.DataItem)[0].ToString());
  23.                         string tradeCode = ((DataRowView)e.Item.DataItem)[3].ToString();
  24.  
  25.  
  26.                         AttributeDto selectedAttriubte =
  27.                             GetAttributeFromSelectedItem(itemId, tradeCode, attriubteId);
  28.                         BindAttributeValueDropDownList(dropDownList, selectedAttriubte, 0);
  29.                         dropDownList.Visible = true;
  30.                         dropDownList.Style.Value = ((DataRowView)e.Item.DataItem)[0].ToString() + "-" +
  31.                             ((DataRowView)e.Item.DataItem)[1].ToString();
  32.                         textBox.Visible = false;
  33.  
  34.                         //check this item already has a value(previously user has selected value)
  35.                         //set it
  36.                         if ((((DataRowView)e.Item.DataItem)[5]) != null)
  37.                         {
  38.                             dropDownList.SelectedIndex = int.Parse(((DataRowView)e.Item.DataItem)[5].ToString());
  39.                         }
  40.  
  41.                         ahsCustomerValidator.ControlToValidate = dropDownList.ID;
  42.                         ahsCustomerValidator.ValidationType = ValidationType.RegularExpression;
  43.                         ahsCustomerValidator.ValidationExpression = "";
  44.                         ahsCustomerValidator.ErrorMessage = "required";
  45.  
  46.                     }
  47.                     else if (dispayStyle == DisplayStyle.SLE.ToString())
  48.                     {
  49.                         dropDownList.Visible = false;
  50.                         textBox.Style.Value = ((DataRowView)e.Item.DataItem)[0].ToString() + "-" +
  51.                            ((DataRowView)e.Item.DataItem)[1].ToString();
  52.                         textBox.Visible = true;
  53.                         //check this item already has a value(previously user has selected value)
  54.                         //set it
  55.                         if ((((DataRowView)e.Item.DataItem)[5]) != null)
  56.                         {
  57.                             textBox.Text = ((DataRowView)e.Item.DataItem)[5].ToString();
  58.                         }
  59.                         ahsCustomerValidator.ControlToValidate = textBox.ID;
  60.                         ahsCustomerValidator.ValidationType = ValidationType.RequiredField;
  61.                         ahsCustomerValidator.ErrorMessage = "required";
  62.  
  63.                     }
  64.                     if (!bool.Parse(((DataRowView)e.Item.DataItem)[6].ToString()))
  65.                     {
  66.                         ahsCustomerValidator.Enabled = false;
  67.                     }
  68.                 }
  69.             }
  70.         }
May 5 '09 #1
2 3883
balame2004
142 100+
What is your doubt?

Does it work or not?
May 5 '09 #2
yeah that is the issue it doesn't work
May 5 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Do | last post by:
Hi, Has anybody run into problems with Page.IsValid? My errors show up when the fields aren't filled correctly, but my processing continues even if the page is invalid. I leave name and...
6
by: Paul | last post by:
I am trying to setup a field validator and tried using the control to validate set to a dropdown list box but did not seem to work. Is there anyway to set this up or do you need to use client side...
2
by: z. f. | last post by:
Hi, i have a field with required field validator. on the client side it's working. when i submit without the required field the page gets back because of not valid, but the message from the...
1
by: Sam Collett | last post by:
Is there a way of having a required field validator check to see if at least one of a set of controls has information in it? I'm sure this can somehow be done using a custom validator, but would...
3
by: Skeptical | last post by:
Hello, I have a form and two buttons on it. Button1 submits the data to a database and RequiredFieldValidator is used to make sure all the required fields are filled up, the second button has a...
3
by: Mike Collins | last post by:
Is it possible to use a required field validator and have it make a user answer a comment field based on a drop down value picked? For instance, if the user selects "It was terrible", make them...
7
by: Newbie Coder | last post by:
How do I use a Required Field Validator control in ASP.NET 2.0 to validate an e-mail address entered into a textbox control because there is no VALIDATION EXPRESSION like in previous versions? ...
0
by: nimeshdotnet | last post by:
Hi All, I have a textbox whose readonly property is set to true. This textbox is also guarded by a required field validator. What i m doing that I have a calendar control whose on date selection i...
6
by: mathewgk80 | last post by:
Hi all, I have two textboxes, txtName and txtDescription. i added required field validator to both of them. I have a Gridview in the same page. In tht gridview, i have a template field...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.