2013-01-07 19 views
0

我正在尋找FindControl來從VB.Net代碼中查看ASP.Net DetailsView中的控件,在文件後面。它沒有找到任何一個。使用FindControl來查找VB.Net代碼隱藏文件中ASP.Net DetailsView中的控件不起作用

該頁面的標記正在使用母版頁。

<%@ Page 
    Title="Attendance" 
    Language="vb" 
    AutoEventWireup="false" 
    MasterPageFile="~/Knowledge Academy.Master" 
    CodeBehind="Attendance.aspx.vb" 
    Inherits="Knowledge_Academy.Attendance" %> 

<asp:Content 
    ID="ContentBody" 
    ContentPlaceHolderID="BodyPlaceholder" 
    runat="server"> 

目前一個我們DetailsView控件的屬性如下:

<asp:DetailsView 
      ID="DetailsView" 
      runat="server" 
      AutoGenerateRows="False" 
      Height="50px" 
      Width="207px" 
      DataSourceID="SqlDataSourceDetails" 
      DataKeyNames="ID" 
      OnItemCommand="DetailsViewDetails_ItemCommand"> 

      <Fields> 

你能告訴我到包括哪些附加屬性,這樣編碼這樣可以「看到」在DetailsView領域?

Protected Sub DetailsViewDetails_ItemCommand(sender As Object, e As System.Web.UI.WebControls.DetailsViewCommandEventArgs) 

    Select Case e.CommandName 

     Case "Add" 

     Case "Edit" 
      ButtonAddNewAttendance.Enabled = False 

     Case "Delete" 

     Case "Update" 
      ButtonAddNewAttendance.Enabled = True 

     Case "Insert" 

     Case "New" 

      Dim txtBox As TextBox 
      txtBox = DetailsView.FindControl("TextBoxDateAttendanceTakenInsert") 
      txtBox.Text = DateTime.Now 

      Dim drpValue As DropDownList 
      drpValue = DetailsView.FindControl("DropDownListClassInsert") 
      drpValue.SelectedValue = 1 
    End Select 
End Sub 

當前FindControl在DetailsView中找不到任何字段,並給出空引用錯誤。

  • UPDATE *

ItemCommand是不要把編碼正確的位置。

我發現爲了得到這個工作,需要添加OnDataBinding,如下所示,並確保代碼隱藏文件中有一個處理程序,如下所示。

InsertTemplate則標記:

<InsertItemTemplate> 
    <asp:DropDownList 
     ID="DropDownListClassInsert" 
     Runat="server" 
     DataSourceID="SqlDataSourceClasses" 
     DataTextField = "ClassName" 
     DataValueField="ID" 
     SelectedValue='<%# Bind("ClassID") %>' 
     AppendDataBoundItems="True" 
     ForeColor="Blue" 
     OnDataBinding="DropDownListClassInsert_DataBinding"> 
    </asp:DropDownList> 

    <asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertClass" runat="server" ControlToValidate="DropDownListClassInsert" 
     ErrorMessage="Please select a Class here." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
     SetFocusOnError="True" Display="Dynamic"> 
    </asp:RequiredFieldValidator> 
</InsertItemTemplate> 

處理程序中的代碼隱藏文件:

Protected Sub DropDownListClassInsert_DataBinding(sender As Object, e As EventArgs) 

    Dim drpValue As DropDownList 
    drpValue = DetailsView.FindControl("DropDownListClassInsert") 
    drpValue.SelectedValue = intCurrentClassID 
End Sub 

注:intCurrentClassID被聲明爲:

Public Shared intCurrentClassID As Integer = Nothing 

後:

Public Class 

我希望這可以幫助其他有相同問題的人。

回答

0

我相信你想要使用Fields屬性而不是FindControl。一旦擁有DataControlField,您可以將其轉換爲適當的控件類型(即CheckBoxField)。

+0

感謝您的回覆。你能根據我們現有的代碼顯示樣本代碼嗎?謝謝。 –

+0

@ Emad-ud-deen:當然,但我需要看到更多的代碼才能提供這個(這就是爲什麼我沒有試圖在答案中這樣做)。你可以在你嘗試使用它的地方展示代碼嗎? –

+0

好的。我將編輯帖子以顯示完整的子程序。 –

相關問題