这是我的gridview,我添加了一个命令字段编辑,它可以编辑网格视图中的特定值。代码工作正常。我只需要知道如何添加一个必需的字段验证器和绑定字段的正则表达式验证器。 据我所知,在项目模板中添加一个文本框,然后使用 ControlToValidate =“id”进行验证是可能的,但有没有机会为绑定字段添加必需的字段验证器
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" RowStyle-HorizontalAlign="Center"
style="top: 538px; left: 216px; position: absolute; height: 133px; width: 534px"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
Visible="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Sr.No.">
<ItemTemplate>
<asp:Label ID="lblserial" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FID" HeaderText="Faculty ID" ReadOnly="true"
ItemStyle-HorizontalAlign="Center" SortExpression="FID" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString ="{0:d}" SortExpression="Date" ReadOnly="true"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Theme" HeaderText="Theme" SortExpression="Theme"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Duration" HeaderText="Duration" ItemStyle-HorizontalAlign="Center"
SortExpression="Duration" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Organizer" HeaderText="Role" ItemStyle-HorizontalAlign="Center"
SortExpression="Organizer" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Venue" HeaderText="Venue" SortExpression="Venue" ReadOnly="true"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="UpdateDate" HeaderText="UpdateDate" DataFormatString ="{0:d}" ReadOnly="true"
SortExpression="UpdateDate" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<EmptyDataTemplate><h3 align="center" style="font-weight: bold; font-size: large">No Records Found</h3></EmptyDataTemplate>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>"
SelectCommand="SELECT * FROM [WorkshopSC_det] WHERE ([FID] = @FID)"
UpdateCommand="UPDATE [WorkshopSC_det] SET Duration=@Duration,Organizer=@Organizer WHERE(Theme=@Theme) ">
<SelectParameters>
<asp:SessionParameter Name="FID" SessionField="FID" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Theme"/>
<asp:Parameter Name="Duration" />
<asp:Parameter Name="Organizer" />
<asp:Parameter Name="Role" />
</UpdateParameters>
</asp:SqlDataSource>
答案 0 :(得分:0)
我遇到了与Bound字段相同的问题,根据我的理解,字段验证器不支持绑定字段。 我尝试了两种解决方案。
但你可以尝试一下。
模板字段的另一个优点是它允许您向列添加页脚和标题行,这在行插入网格时可能很方便。
<asp:TemplateField HeaderText="Faculty ID" SortExpression="FID" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="fidText" runat="server" Text='<%# Bind("FID") %>' ReadOnly="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqValidatorId" runat="server" ControlToValidate="fidText" Display="Dynamic" ForeColor="Red" ErrorMessage="Value is required."> * </asp:RequiredFieldValidator>
<asp:RegularExpressionValidator runat="server" Display="Dynamic" ControlToValidate="fidText" ErrorMessage="Enter a valid value" ForeColor="Red" ValidationExpression="YOUR REGEX EXPRESSION">Enter a valid value for Faculty Id.</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>