gpt4 book ai didi

c# - GridView 内绑定(bind)字段元素的必填字段和正则表达式验证器

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:59 24 4
gpt4 key购买 nike

这是我的 GridView ,我添加了一个命令字段编辑,它将允许编辑 GridView 中的特定值。代码工作正常。我只需要知道如何添加绑定(bind)字段的必填字段验证器和正则表达式验证器。据我所知,在项目模板中添加一个文本框,然后使用 ControlToValidate =”id” 进行验证是可能的,但是是否有可能为 Bound Field 添加必需的字段验证器

     <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>

最佳答案

我对绑定(bind)字段有同样的问题,根据我的理解,字段验证器不支持绑定(bind)字段。我尝试了两种解决方案。

  1. 我关注了 this article 它奏效了。但在我的例子中,我想添加正则表达式和必填字段验证器,当有多个验证器时,这种方法不一致。

不过你可以试试。

  1. 将绑定(bind)字段替换为模板字段,并在模板字段内添加项目模板和字段验证器,如下所示。它用于教师 ID 字段,您可以对其他列重复相同的操作。

模板字段的另一个优点是它允许您向列添加页脚和标题行,这在向网格插入行的情况下可能很方便。

        <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>

关于c# - GridView 内绑定(bind)字段元素的必填字段和正则表达式验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20944428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com