Connecting Tech Pros Worldwide Help | Site Map

dynamical bind required field validator

Newbie
 
Join Date: Apr 2009
Posts: 18
#1: May 5 '09
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.         }
Familiar Sight
 
Join Date: Mar 2008
Posts: 141
#2: May 5 '09

re: dynamical bind required field validator


What is your doubt?

Does it work or not?
Newbie
 
Join Date: Apr 2009
Posts: 18
#3: May 5 '09

re: dynamical bind required field validator


yeah that is the issue it doesn't work
Reply