1

在代码隐藏文件中使用带有 VB.Net 的 ASP.Net DetailsView,我们希望用户在插入数据时更容易进行数据输入。

我们希望实现一种方法,让 DetailsView 在单击“新建”按钮之前记住特定字段中的值。在它记住值后,我们希望在单击“新建”后使用记住的值填充 2 个字段,即 DropDownList 和 TextBox 字段。

目前我们在代码隐藏文件中尝试了这个:

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 New TextBox
            txtBox = DetailsView.FindControl("TextBoxDateAttendanceTakenInsert")
            txtBox.Text = DateTime.Now

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

单击“新建”按钮时,将显示此错误:

System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.

此错误出现在 txtBox.Text = DateTime.Now

几乎就好像它无法从标记中找到该字段。你能告诉我我还需要在编码中添加什么吗?

如果我注释掉 TextBox 的编码并让编码为 DropDownList 运行,它会产生相同的错误。

这是显示 2 个字段的标记:

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

<Fields>

<asp:TemplateField HeaderText="Class:" SortExpression="ClassID">
    <EditItemTemplate>

        <asp:DropDownList 
            ID="DropDownListClassEdit" 
            Runat="server"
            DataSourceID="SqlDataSourceClasses"
            DataTextField = "ClassName"
            DataValueField="ID"
            SelectedValue='<%# Bind("ClassID") %>'
            ForeColor="Blue">
        </asp:DropDownList>

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

         </asp:RequiredFieldValidator>

     </EditItemTemplate>

     <InsertItemTemplate>

         <asp:DropDownList 
             ID="DropDownListClassInsert" 
             Runat="server"
             DataSourceID="SqlDataSourceClasses"
             DataTextField = "ClassName"
             DataValueField="ID"
             SelectedValue='<%# Bind("ClassID") %>'
             AppendDataBoundItems="True"
             ForeColor="Blue">
         </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>

     <ItemTemplate>

         <asp:DropDownList 
             ID="DropDownListClass" 
             Runat="server"
             DataSourceID="SqlDataSourceClasses"
             DataTextField = "ClassName"
             DataValueField="ID"
             SelectedValue='<%# Bind("ClassID") %>'
             Enabled="false"
             ForeColor="Blue"
             Font-Bold="true"> 
         </asp:DropDownList>

     </ItemTemplate>
 </asp:TemplateField>

 <asp:TemplateField 
     HeaderText="Attendance Date:" SortExpression="DateAttendanceTaken">
     <EditItemTemplate>
         <asp:TextBox 
             ID="TextBoxDateAttendanceTakenEdit" 
             runat="server" 
             Text='<%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %>'>
         </asp:TextBox>
     </EditItemTemplate>

     <InsertItemTemplate>
         <asp:TextBox 
             ID="TextBoxDateAttendanceTakenInsert" 
             runat="server" 
             Text='<%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %>'>
         </asp:TextBox>
     </InsertItemTemplate>

     <ItemTemplate>
         <asp:Label 
             ID="LabelDateAttendanceTaken" 
             runat="server" 
             Text='<%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %>'>
         </asp:Label>
     </ItemTemplate>

     <ItemStyle ForeColor="Blue" />
 </asp:TemplateField>

现在我将一个值硬编码为:

drpValue.SelectedValue = 1

直到我们可以让编码工作。

* 更新 *

ItemCommand 不是放置编码的正确位置。

我发现要让它工作,需要添加 OnDataBinding,如下所示,并确保代码隐藏文件中有一个处理程序,如下所示。

InsertItemTemplate 标记:

<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

我希望这可以帮助其他有同样问题的人。

4

0 回答 0