难道自定义的验证工作在FormView控件? [英] Does custom validator works in FormView?

查看:163
本文介绍了难道自定义的验证工作在FormView控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须通过谷歌搜索,发现有很多人在这个问题苦苦挣扎,但我仍然没有找到正确的答案。

http://i.stack.imgur.com/15jen.png

我有一个表格视图,需要检查,如果语言code是重复与否,都必须检查服务器端脚本,因为它需要通过数据库查询。

更新4 2011年5月,19.32
//我加的属性表单视图的这里,所以有人可能会指出,如果有什么不对的。

 < ASP:FormView控件ID =fmvxLanguage=服务器的EnableViewState =假DefaultMode =插入
    可见=假WIDTH =95%的DataSourceID =odsLanguage的DataKeyNames =语言code
    的CssClass =formViewAdd>

//

 < D​​XE:ASPxButton ID =btnAddNewLanguage=服务器的CausesValidation =真
图像URL =〜/图片/图标/保存-的icon.png的CommandName =插入文本=保存>
< / DXE:ASPxButton>

我使用插入命令插入按钮中,导致验证设置为true。

 < ASP:的CustomValidator ID =cvdLanguage codeNameDuplicate=服务器
                        的ControlToValidate =txtLanguage code的CssClass =IconValidation
                        的ErrorMessage =&放大器;语言code名称重复LT; IMG SRC =&放大器; QUOT; /images/icon/validation-Icon.png& QUOT; /&放大器; GT。
                        onservervalidate =cvdLanguage codeNameDuplicate_ServerValidate> < / ASP:的CustomValidator>

自定义验证设置为上述

当我按一下按钮,并尝试把断点的ServerValidate事件,我甚至没有到达那里。

 保护无效cvdLanguage codeNameDuplicate_ServerValidate(对象源,ServerValidateEventArgs参数)
    {        如果(LanguageHelper.HaveLanguage code(args.Value))
        {
            args.IsValid = FALSE;
        }
    }

至于现在我使用的标签,而不是自定义的验证通过检查值是否有效事件FormView_ItemInserting,如果值是无效的,我只是用e.Cancel(FormViewInsertEventArgs),并在标签可见。但是,我想知道,如果自定义验证器工作在FormView控件还是我做错什么了。

感谢您。

以下code是不是这个问题,,但它可能是有人搜索这个话题有用的,可能有同样的问题。我不得不重复这个有很多的时间,所以我使可重用的类此事件(用标签作为验证程序)

 公共类clsFormViewDuplicationValidationSetter
{
    #区域物业
    公共FormView控件FormView控件{搞定;组; }    公共委托布尔DelDuplicationValidationNameOnly(字符串pStrName);
    公共委托布尔DelDuplicationValidationNameAndId(字符串pStrName,诠释primaryId);    公共DelDuplicationValidationNameOnly DuplicationValidationNameOnly;
    公共DelDuplicationValidationNameAndId DuplicationValidationDelegationNameAndId;
    公开文本框TextBoxNameToCheckForDuplication {搞定;组; }
    公共标签LabelDuplicationValidationMessage {搞定;组; }    #endregion    #区域构造    ///<总结>
    ///模式的简单复制ValidationName和Id
    ///< /总结>
    ///< PARAM NAME =pObjFormView>&FormView控件LT; /参数>
    ///< PARAM NAME =pObjTextBoxNameToCheckForDuplication> TextBoxName< /参数>
    ///< PARAM NAME =pObjLabelDuplicationValidationMessage>拉布勒显示错误消息< /参数>
    ///< PARAM NAME =pObjDuplicationValidationNameAndId>代表团进行验证功能(名称和ID)LT; /参数>
    公共clsFormViewDuplicationValidationSetter(FormView控件pObjFormView,文本框pObjTextBoxNameToCheckForDuplication,标签pObjLabelDuplicationValidationMessage,DelDuplicationValidationNameAndId pObjDuplicationValidationNameAndId)
    {
        this.FormView = pObjFormView;
        this.TextBoxNameToCheckForDuplication = pObjTextBoxNameToCheckForDuplication;
        this.LabelDuplicationValidationMessage = pObjLabelDuplicationValidationMessage;
        this.DuplicationValidationDelegationNameAndId = pObjDuplicationValidationNameAndId;
        FormView.ItemInserting + =新FormViewInsertEventHandler(FormView_ItemInserting);
    }    ///<总结>
    ///模式的简单复制验证名称
    ///< /总结>
    ///< PARAM NAME =pObjFormView>&FormView控件LT; /参数>
    ///< PARAM NAME =pObjTextBoxNameToCheckForDuplication> TextBoxName< /参数>
    ///< PARAM NAME =pObjLabelDuplicationValidationMessage>拉布勒显示错误消息< /参数>
    ///< PARAM NAME =pObjDuplicationValidationDelegation>代表团进行验证功能(名)LT; /参数>
    公共clsFormViewDuplicationValidationSetter(FormView控件pObjFormView,文本框pObjTextBoxNameToCheckForDuplication,标签pObjLabelDuplicationValidationMessage,DelDuplicationValidationNameOnly pObjDuplicationValidationNameOnly)
    {
        this.FormView = pObjFormView;
        this.TextBoxNameToCheckForDuplication = pObjTextBoxNameToCheckForDuplication;
        this.LabelDuplicationValidationMessage = pObjLabelDuplicationValidationMessage;
        this.DuplicationValidationNameOnly = pObjDuplicationValidationNameOnly;
        FormView.ItemInserting + =新FormViewInsertEventHandler(FormView_ItemInserting);
    }    无效FormView_ItemInserting(对象发件人,FormViewInsertEventArgs E)
    {
        字符串名称= TextBoxNameToCheckForDuplication.Text;
        布尔IsDuplicate;         //添加时,始终ID 0
        如果(DuplicationValidationDelegationNameAndId!= NULL)
            IsDuplicate = DuplicationValidationDelegationNameAndId(姓名,0);
        其他
            IsDuplicate = DuplicationValidationNameOnly(名);        如果(IsDuplicate)
        {
            e.Cancel =真;
            FormView.Visible = TRUE;
            LabelDuplicationValidationMessage.Visible = TRUE;
        }
    }    #endregion
}

当在窗体加载使用

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        文本框objtxtLanguage code =(文本框)fmvxLanguage.FindControl(txtLanguage code);
        标签objFormViewLabelDuplicationValidationMessage =(标签)fmvxLanguage.FindControl(lblFormViewDuplicate);        clsFormViewDuplicationValidationSetter objFormViewDuplicationValidationSetter =新clsFormViewDuplicationValidationSetter(fmvxLanguage,objtxtLanguage$c$c,objFormViewLabelDuplicationValidationMessage,LanguageHelper.HaveLanguage$c$c);
    }


解决方案

我只是找出更妥善的解决办法,我们必须
呼叫Page.Validate()和检查,如果(Page.IsValid)前进行。
如果分配的ValidationGroup,呼叫Page.Validate(groupNameHere)

I have search through google and found that a lot of people is struggling with this issue but i still not found the right answer.

http://i.stack.imgur.com/15jen.png

I have a form view and need to check that if the language code is duplicate or not, it have to be check on server-side script since it needed to be check through database.

updated 4 may 2011, 19.32 // I add attribute of form view here, so someone may point out if there is anything wrong.

<asp:FormView ID="fmvxLanguage" runat="server" EnableViewState="False" DefaultMode="Insert"
    Visible="False" Width="95%" DataSourceID="odsLanguage" DataKeyNames="LanguageCode"
    CssClass="formViewAdd">

//

<dxe:ASPxButton ID="btnAddNewLanguage" runat="server" CausesValidation="True"
Image-Url="~/images/icon/Save-icon.png" CommandName="Insert" Text="Save">
</dxe:ASPxButton>

I Use insert command as Insert in button, Cause Validation is set to true.

<asp:CustomValidator ID="cvdLanguageCodeNameDuplicate" runat="server" 
                        ControlToValidate="txtLanguageCode" CssClass="IconValidation" 
                        ErrorMessage="&lt;img src=&quot;/images/icon/validation-Icon.png&quot;/&gt;     Language code name is duplicated." 
                        onservervalidate="cvdLanguageCodeNameDuplicate_ServerValidate">    </asp:CustomValidator>

Custom Validator is set as above

When i click the button and try putting the breakpoint at serverValidate event, i didn't even reach there

protected void cvdLanguageCodeNameDuplicate_ServerValidate(object source, ServerValidateEventArgs args)
    {

        if (LanguageHelper.HaveLanguageCode(args.Value))
        {
            args.IsValid = false;
        }
    }

As for now i use label instead of custom validator by checking if the value is valid or not in the event FormView_ItemInserting, if the value is not valid i just use e.Cancel (FormViewInsertEventArgs) and make the label visible. but still, i want to know if the custom validator is not works on formview or did i do something wrong.

Thank you.

The following code is not about the question, but it might be useful to someone search to this topic and might have same issue. i have to repeat this a lot of time so i make reusable class for this event ( using label as validator)

public class clsFormViewDuplicationValidationSetter
{
    #region Property


    public FormView FormView { get; set; }

    public delegate bool DelDuplicationValidationNameOnly(string pStrName);
    public delegate bool DelDuplicationValidationNameAndId(string pStrName, int primaryId);

    public DelDuplicationValidationNameOnly DuplicationValidationNameOnly;
    public DelDuplicationValidationNameAndId DuplicationValidationDelegationNameAndId;


    public TextBox TextBoxNameToCheckForDuplication { get; set; }
    public Label LabelDuplicationValidationMessage { get; set; }

    #endregion

    #region Constructor

    /// <summary>
    /// Pattern For Simple Duplication ValidationName and Id
    /// </summary>
    /// <param name="pObjFormView">FormView</param>
    /// <param name="pObjTextBoxNameToCheckForDuplication">TextBoxName</param>
    /// <param name="pObjLabelDuplicationValidationMessage">Lable Showing Error Message</param>
    /// <param name="pObjDuplicationValidationNameAndId">Delegation for validation function (name and id)</param>
    public clsFormViewDuplicationValidationSetter(FormView pObjFormView, TextBox pObjTextBoxNameToCheckForDuplication, Label pObjLabelDuplicationValidationMessage, DelDuplicationValidationNameAndId pObjDuplicationValidationNameAndId)
    {
        this.FormView = pObjFormView;
        this.TextBoxNameToCheckForDuplication = pObjTextBoxNameToCheckForDuplication;
        this.LabelDuplicationValidationMessage = pObjLabelDuplicationValidationMessage;
        this.DuplicationValidationDelegationNameAndId = pObjDuplicationValidationNameAndId;
        FormView.ItemInserting += new FormViewInsertEventHandler(FormView_ItemInserting);
    }

    /// <summary>
    /// Pattern For Simple Duplication Validation Name 
    /// </summary>
    /// <param name="pObjFormView">FormView</param>
    /// <param name="pObjTextBoxNameToCheckForDuplication">TextBoxName</param>
    /// <param name="pObjLabelDuplicationValidationMessage">Lable Showing Error Message</param>
    /// <param name="pObjDuplicationValidationDelegation">Delegation for validation function (name)</param>
    public clsFormViewDuplicationValidationSetter(FormView pObjFormView, TextBox pObjTextBoxNameToCheckForDuplication, Label pObjLabelDuplicationValidationMessage, DelDuplicationValidationNameOnly pObjDuplicationValidationNameOnly)
    {
        this.FormView = pObjFormView;
        this.TextBoxNameToCheckForDuplication = pObjTextBoxNameToCheckForDuplication;
        this.LabelDuplicationValidationMessage = pObjLabelDuplicationValidationMessage;
        this.DuplicationValidationNameOnly = pObjDuplicationValidationNameOnly;
        FormView.ItemInserting += new FormViewInsertEventHandler(FormView_ItemInserting);
    }

    void FormView_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        string name = TextBoxNameToCheckForDuplication.Text;


        bool IsDuplicate; 

         // when adding, id always 0
        if (DuplicationValidationDelegationNameAndId != null)
            IsDuplicate = DuplicationValidationDelegationNameAndId(name, 0);
        else
            IsDuplicate = DuplicationValidationNameOnly(name); 

        if (IsDuplicate)
        {
            e.Cancel = true;
            FormView.Visible = true;
            LabelDuplicationValidationMessage.Visible = true;
        }
    }

    #endregion


}

When using in Form Load

    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox objtxtLanguageCode= (TextBox)fmvxLanguage.FindControl("txtLanguageCode");
        Label objFormViewLabelDuplicationValidationMessage = (Label)fmvxLanguage.FindControl("lblFormViewDuplicate");

        clsFormViewDuplicationValidationSetter objFormViewDuplicationValidationSetter = new clsFormViewDuplicationValidationSetter(fmvxLanguage,objtxtLanguageCode,objFormViewLabelDuplicationValidationMessage,LanguageHelper.HaveLanguageCode);
    }

解决方案

I just find out the more proper solution, we must Call Page.Validate() and checking if(Page.IsValid) before proceed. if the ValidationGroup assigned, Call Page.Validate("groupNameHere")

这篇关于难道自定义的验证工作在FormView控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆