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
-
<asp:Repeater ID="repeaterItemAttribute" runat="server" OnItemDataBound="repeaterItemAttribute_ItemDataBound">
-
<ItemTemplate>
-
<li>
-
<label for="state" class="newlabel">
-
<%# DataBinder.Eval(Container.DataItem, "AttributeName")%>
-
:</label>
-
-
<asp:DropDownList ID="dropDownListAttributeValue" runat="server" class="select">
-
</asp:DropDownList>
-
<asp:TextBox ID="textBoxAttributeValue" runat="server"></asp:TextBox>
-
<cc1:AhsCustomValidator ID="requiredFieldValidator" runat="server" Display="None"
-
ChangeCssClass="text error" ErrorMessage=" is required"></cc1:AhsCustomValidator>
-
</li>
-
</ItemTemplate>
-
</asp:Repeater>
and here is my code behind
-
protected void repeaterItemAttribute_ItemDataBound(object sender, RepeaterItemEventArgs e)
-
{
-
if (e != null)
-
{
-
if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
-
(e.Item.ItemType == ListItemType.Item))
-
{
-
-
AhsCustomValidator ahsCustomerValidator =
-
(AhsCustomValidator)e.Item.FindControl(RequiredFieldValidator);
-
-
//Get display style of currently biding item it could be either DDW or SLE
-
String dispayStyle = ((DataRowView)e.Item.DataItem)[2].ToString();
-
//Get a references of TextBox and DrowDownList control binding for each item
-
TextBox textBox = (TextBox)e.Item.FindControl(TextBoxAttributeValue);
-
DropDownList dropDownList = (DropDownList)e.Item.FindControl(DropDownListAttributeValue);
-
-
//display style type DDW display only drop down list otherwise TextBox
-
if (dispayStyle == DisplayStyle.DDW.ToString())
-
{
-
long itemId = long.Parse(((DataRowView)e.Item.DataItem)[4].ToString());
-
long attriubteId = long.Parse(((DataRowView)e.Item.DataItem)[0].ToString());
-
string tradeCode = ((DataRowView)e.Item.DataItem)[3].ToString();
-
-
-
AttributeDto selectedAttriubte =
-
GetAttributeFromSelectedItem(itemId, tradeCode, attriubteId);
-
BindAttributeValueDropDownList(dropDownList, selectedAttriubte, 0);
-
dropDownList.Visible = true;
-
dropDownList.Style.Value = ((DataRowView)e.Item.DataItem)[0].ToString() + "-" +
-
((DataRowView)e.Item.DataItem)[1].ToString();
-
textBox.Visible = false;
-
-
//check this item already has a value(previously user has selected value)
-
//set it
-
if ((((DataRowView)e.Item.DataItem)[5]) != null)
-
{
-
dropDownList.SelectedIndex = int.Parse(((DataRowView)e.Item.DataItem)[5].ToString());
-
}
-
-
ahsCustomerValidator.ControlToValidate = dropDownList.ID;
-
ahsCustomerValidator.ValidationType = ValidationType.RegularExpression;
-
ahsCustomerValidator.ValidationExpression = "";
-
ahsCustomerValidator.ErrorMessage = "required";
-
-
}
-
else if (dispayStyle == DisplayStyle.SLE.ToString())
-
{
-
dropDownList.Visible = false;
-
textBox.Style.Value = ((DataRowView)e.Item.DataItem)[0].ToString() + "-" +
-
((DataRowView)e.Item.DataItem)[1].ToString();
-
textBox.Visible = true;
-
//check this item already has a value(previously user has selected value)
-
//set it
-
if ((((DataRowView)e.Item.DataItem)[5]) != null)
-
{
-
textBox.Text = ((DataRowView)e.Item.DataItem)[5].ToString();
-
}
-
ahsCustomerValidator.ControlToValidate = textBox.ID;
-
ahsCustomerValidator.ValidationType = ValidationType.RequiredField;
-
ahsCustomerValidator.ErrorMessage = "required";
-
-
}
-
if (!bool.Parse(((DataRowView)e.Item.DataItem)[6].ToString()))
-
{
-
ahsCustomerValidator.Enabled = false;
-
}
-
}
-
}
-
}