验证一个gridview [英] Validate a Gridview
本文介绍了验证一个gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我想要验证时,在编辑模式下一个gridview。我该怎么做呢?
下面是我的GridView再下面是我的第一次尝试。
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
的DataSourceID =SqlDataSource1背景色=白BORDERCOLOR =#999999
边框样式=无边框宽度=1px的CELLPADDING =3网格=垂直
WIDTH =387px的DataKeyNames =APPIDAllowPaging =真>
< PagerSettings模式=下一步previousFirstLast/>
< RowStyle的BackColor =#EEEEEE前景色=黑/>
<柱体和GT;
< ASP:BoundField的数据字段=APPNAME的HeaderText =应用程序
SORTEX pression =APPNAME/>
< ASP:BoundField的数据字段=说明的HeaderText =说明
SORTEX pression =说明/>
< ASP:CommandField中AccessibleHeaderText =编辑按钮类型=形象
DeleteImageUrl =〜/图片/ bttnDelete.gif
EditImageUrl =〜/图片/ bttnEdit.gif的HeaderText =行动
的ShowDeleteButton =真ShowEditButton =真
ShowHeader =真CancelImageUrl =〜/图片/ bttnCancel.gif
UpdateImageUrl =〜/图片/ bttnSave.gifInsertVisible =FALSE/>
< /专栏>
< FooterStyle背景色=#CCCCCC前景色=黑/>
< PagerStyle背景色=#999999前景色=黑HorizontalAlign =中心/>
< SelectedRowStyle背景色=#008A8CFONT-粗体=真前景色=白/>
< HeaderStyle背景色=#000084FONT-粗体=真前景色=白/>
< AlternatingRowStyle背景色=#DCDCDC/>
< / ASP:GridView的>
第一次尝试
保护小组GridView1_RowDataBound(BYVAL发件人为对象,BYVAL E上System.Web.UI.WebControls.GridViewRowEventArgs)处理GridView1.RowDataBound如果e.Row.RowState = DataControlRowState.Edit然后 昏暗savebtn作为的ImageButton = DirectCast(e.Row.Cells(2).Controls(0),的ImageButton)
savebtn.ValidationGroup =GRD 设置文本框的名称
暗淡txtName的作为文本框= DirectCast(e.Row.Cells(0).Controls(0),文本框)
txtname.ValidationGroup =GRD
昏暗reqval作为新的RequiredFieldValidator
reqval.ID =reqnam
reqval.ValidationGroup =GRD reqval.ErrorMessage =应用程序名称不能为空
reqval.ControlToValidate = txtname.UniqueID 万一
解决方案
您最好的办法是转换的BoundField
到模板列
并添加验证控件的 EditItemTemplate中
。所以,你的第一列将变成:
< ASP:的TemplateField的HeaderText =应用程序SORTEX pression =APPNAME>
<&EditItemTemplate的GT;
< ASP:文本框ID =txtApp=服务器文本='<%#绑定(APPNAME)%>/>
< ASP:RUNAT的RequiredFieldValidator ='服务器'ID ='requiredApp
的ErrorMessage ='应用程序的名称不能为空的ControlToValidate ='txtApp/>
< / EditItemTemplate中>
<&ItemTemplate中GT;
< ASP:标签ID =labelApp=服务器文本='<%#绑定(APPNAME)%>/>
< / ItemTemplate中>
< / ASP:的TemplateField>
I have a gridview that I want to validate when it is in edit mode. how do I do this?
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>
First Attempt
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
解决方案
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>
这篇关于验证一个gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文