2012-02-23 43 views
1

我希望能夠將參數傳遞到服務器端CustomValidator。將參數傳遞到服務器端CustomValidator

我想這是因爲我想驗證的控件位於網格的編輯字段中。

代碼:

<telerik:GridTemplateColumn DataField="numbercancelled" DataType="System.Int32" FilterControlAltText="Filter numbercancelled column" 
         HeaderText="numbercancelled" SortExpression="Numbercancelled" UniqueName="numbercancelled"> 
         <ItemTemplate> 
          <asp:Label runat="server" ID="lblNumbercancelled" Text='<%# Eval("numbercancelled") %>'> 
          </asp:Label> 
         </ItemTemplate> 
         <EditItemTemplate> 
          <asp:TextBox runat="server" ID="tbNumbercancelled" Text='<%# Bind("numbercancelled") %>' 
           MaxLength="10"> 
          </asp:TextBox> 
          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="This is a required field" 
           ControlToValidate="tbNumbercancelled" Display="Dynamic"></asp:RequiredFieldValidator> 
          <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Please enter a number higher than 0" 
           ValidationExpression="^(0|[1-9][0-9]*)$" ControlToValidate="tbNumbercancelled" 
           Display="Dynamic"></asp:RegularExpressionValidator> 
          <asp:CustomValidator ID="NumberCancelledValidator" 
           runat="server" ErrorMessage="CustomValidator" ControlToValidate="tbNumbercancelled" 
           OnServerValidate="NumberCancelledValidator_ServerValidate" 
           ></asp:CustomValidator> 
         </EditItemTemplate> 
        </telerik:GridTemplateColumn> 

我想通過當前正在編輯行的ID,因爲它是如何生效取決於你正在編輯的行。

最後我想出了這一點:

CustomValidator cvalid = (CustomValidator)source; 
GridEditFormItem gv = (GridEditFormItem)cvalid.NamingContainer; 

int ItemIndex = ((GridEditFormItem)gv.EditFormCell.NamingContainer).ItemIndex; 
var grid = InkoopDetailGridEdit; 
int ID = int.Parse(((Label)((GridDataItem)grid.Items[ItemIndex]).Cells[0].FindControl("ID")).Text); 

回答

2

您可以用下面的例子中得到行索引:

CustomValidator cvalid = (CustomValidator)source; 
GridViewRow gv = cvalid.NamingContainer; 
int index = gv.RowIndex; 
+0

即使辛苦也並沒有直接回答我的問題,它確實讓我回答。 – NomenNescio 2012-02-23 15:20:32

相關問題