2014-01-06 30 views
2

這是我的gridview,我添加了一個命令字段編輯它將允許編輯網格視圖中的特定值。代碼工作正常.i只需要知道如何爲綁定字段添加必需的字段驗證器和正則表達式驗證器。 據我所知,增加項目模板中的文本框,然後使用的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 [email protected],[email protected] WHERE([email protected]) "> 

     <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

希望這可以幫助你 - http://stackoverflow.com/questions/7685853/how-to-give-required-field-validator-to-textbox-in-gridview – mmssaann

+0

而這一次 - HTTP: //www.codeproject.com/Articles/203892/Required-BoundField-in-Gridview – mmssaann

+0

我沒有使用項目模板和文本框,我需要爲綁定字段本身添加RFV –

回答

0

我不得不添加所需的字段校驗器的任何機會字段驗證器不支持綁定字段和我的理解綁定字段的相同問題。 我嘗試了兩種解決方案。

  1. 我跟着this article,它在我的情況worked.But我想補充的正則表達式和所需的字段驗證而這種做法是不相符的,當有一個以上的驗證。

但是你可以試試它。

  1. 用模板字段替換綁定字段並在模板字段中添加項目模板和字段驗證器,如下所示。它用於教師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> 
相關問題