0

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

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

4

1 回答 1

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;
  }
于 2012-11-30T10:06:17.390 回答