如何放置一个必填字段校验器的GridView文本框内 [英] How to place a required field validator inside a GridView TextBox

查看:887
本文介绍了如何放置一个必填字段校验器的GridView文本框内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GridView控件包含一些模板列项目文本框控制。我想就加必填字段校验器。这是我的code:

I have a GridView with some TemplateField items containing TextBox controls. I would like to add a required field validator on it. This is my code:

<asp:TemplateField HeaderText="vid">
    <EditItemTemplate>
         <asp:TextBox ID="txtvid" runat="server" Width="150px"
                            Text='<%# Bind("vid") %>'>
         </asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
         <asp:Label 
                   ID="lblvid" runat="server" 
                   Text='<%# Bind("vid") %>'>
         </asp:Label>
    </ItemTemplate>
 </asp:TemplateField>

我怎样在 txtvid

推荐答案

在编辑模板,添加一个的RequiredFieldValidator 是这样的:

In the Edit template, add a RequiredFieldValidator like this:

<EditItemTemplate>
    <asp:TextBox ID="txtvid" 
                 runat="server" Width="150px"
                 Text='<%# Bind("vid") %>'>
    </asp:TextBox>
    <asp:RequiredFieldValidator 
                 ControlToValidate="txtvid" 
                 runat="server" 
                 ErrorMessage="Please enter a 'vid' number" 
                 Text="*"/>
</EditItemTemplate>

下面是的RequiredFieldValidator MSDN上。

更新:

如果你想一个普通的前pression验证,其pretty大致相同,但与 RegularEx pressionValidator 控件:

If you wanted a regular expression validator, its pretty much the same, but with the RegularExpressionValidator control:

 <asp:RegularExpressionValidator 
     ControlToValidate="txtvid"
     ValidationExpression="\d{10}"
     runat="server" 
     ErrorMessage="Please enter a 'vid' of 10 digits" 
     Text="*"/>

下面是对<一个功能的完整列表href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularex$p$pssionvalidator.aspx\"相对= MSDN上的nofollow的> RegularEx pressionValidator 。

这篇关于如何放置一个必填字段校验器的GridView文本框内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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