使用asp.net在Repeater Control中进行自定义验证

时间:2012-11-30 09:49:56

标签: asp.net validation repeater

我在转发器控件中有文本框,标签和按钮。按钮应验证文本框。如果用户输入的值大于4,则应显示错误消息,并且应根据标签框值更改文本框值。

如何使用转发器内的自定义验证来验证文本框?

1 个答案:

答案 0 :(得分:0)

On OnItemDataBound事件或OnItemCreated事件执行此代码

            Label lbl = (Label)e.Item.FindControl("lblId"); 
            Button btn = (Button)e.Item.FindControl("btnId");
            TextBox txt = (TextBox)e.Item.FindControl("txtId");
            CustomValidator cfv = (CustomValidator)e.Item.FindControl("cfvId");
            if (cfv != null && txt!=null&&btn!=null&&lblId!=null)
            {
                cfv.ControlToValidate = txt.ClientID;
                cfv.ClientValidationFunction = "YourFunction";
                cfv.ValidationGroup = cfv.ClientID + "ValidationGroup";
                btn.ValidationGroup = cfv.ClientID + "ValidationGroup";
                lbl.AssociatedControlID=txt.ClientID;
            }

  //javascript code on client side
  function YourFunction(sender, args)
  {
      args.IsValid=parseInt(args.Value)<=4;
      if(!args.IsValid)
         $('#'+sender.controltovalidate).val($('label:[for="'+sender.controltovalidate+'"]').text());
      return;
  }