验证网格视图 [英] Validate a Gridview

查看:20
本文介绍了验证网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 gridview,我想在它处于编辑模式时进行验证.我该怎么做?

I have a gridview that I want to validate when it is in edit mode. how do I do this?

下面是我的 Gridview,下面是我的第一次尝试.

Below is my Gridview and below that is my first attempt.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#999999" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
        Width="387px" DataKeyNames = "APPID" AllowPaging="True">
        <PagerSettings Mode="NextPreviousFirstLast" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <Columns>
            <asp:BoundField DataField="APPName" HeaderText="Application" 
                SortExpression="APPName" />
            <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" />
            <asp:CommandField AccessibleHeaderText="Edit" ButtonType="Image" 
                DeleteImageUrl="~/images/bttnDelete.gif" 
                EditImageUrl="~/images/bttnEdit.gif" HeaderText="Action" 
                ShowDeleteButton="True" ShowEditButton="True" 
                ShowHeader="True" CancelImageUrl="~/images/bttnCancel.gif" 
                UpdateImageUrl="~/images/bttnSave.gif" InsertVisible="False" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>

第一次尝试

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowState = DataControlRowState.Edit Then

            Dim savebtn As ImageButton = DirectCast(e.Row.Cells(2).Controls(0), ImageButton)
            savebtn.ValidationGroup = "grd"

            'set up Name Textbox
            Dim txtname As TextBox = DirectCast(e.Row.Cells(0).Controls(0), TextBox)
            txtname.ValidationGroup = "grd"


            Dim reqval As New RequiredFieldValidator
            reqval.ID = "reqnam"
            reqval.ValidationGroup = "grd"

            reqval.ErrorMessage = "Application Name Cannot Be Empty"
            reqval.ControlToValidate = txtname.UniqueID

        End If

推荐答案

最好的办法是将 BoundField 转换为 TemplateField 并将验证控件添加到 <代码>EditItemTemplate.所以你的第一列会变成:

You best bet is to convert the BoundField into a TemplateField and add the validation control to the EditItemTemplate. So your first column would become:

<asp:TemplateField HeaderText="Application" SortExpression="APPName">
    <EditItemTemplate>
        <asp:TextBox ID="txtApp" runat="server" Text='<%# Bind("APPName") %>'/>
        <asp:RequiredFieldValidator runat='server' ID='requiredApp' 
            ErrorMessage='Application Name Cannot Be Empty' ControlToValidate='txtApp' /> 
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="labelApp" runat="server" Text='<%# Bind("APPName") %>'/>
    </ItemTemplate>
</asp:TemplateField>

这篇关于验证网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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