为什么返回的值没有保存在DetailsView中?

时间:2015-12-14 18:33:28

标签: asp.net vb.net detailsview databound

解决了空对象引用。现在我需要将返回的值保存到记录中。这是数据绑定代码:

Private Sub dvSMasterCurrentYear_DataBound(sender As Object, e As EventArgs) Handles dvSMasterCurrentYear.DataBound
        Dim dv As DetailsView = New DetailsView
        dv = dvSMasterCurrentYear
        If DetailsViewMode.Insert Then
            Dim ctl = dv.FindControl("PlantYear")
            If ctl IsNot Nothing Then
                DirectCast(dv.FindControl("PlantYear"), TextBox).Text = GetYear()
            Else
                Throw New Exception("Control was not found")
            End If
        End If
    End Sub

以下是GetYear功能:

 Public Function GetYear()
        Dim thisDate As Date = Now
        Dim thisYear As String
        'thisDate = #2/12/1969#
        thisYear = Year(thisDate)
        Return thisYear
    End Function

插入时我用这个:

 Private Sub DetailsView1_ItemInserting(sender As Object, e As DetailsViewInsertEventArgs) Handles dvSMasterCurrentYear.ItemInserting
            Dim view As DetailsView = DirectCast(sender, DetailsView)
            Dim txtYearPlant As TextBox = DirectCast(DirectCast(sender, DetailsView).FindControl("PlantYear"), TextBox)
End Sub

这就是标记的样子:

<asp:TemplateField HeaderText="Year Plant (yyyy)*">
                      <ItemTemplate>
                         <asp:Label ID="Label1" runat="server" Text='<%# Bind("PlantYear") %>'></asp:Label>
                          <%# Eval("Year")%>
                     </ItemTemplate>  
                    <EditItemTemplate>
                         <asp:TextBox ID="PlantYear" runat="server"></asp:TextBox>
                     </EditItemTemplate>
                     <InsertItemTemplate>
                          <asp:TextBox ID="PlantYear" runat="server" Text='<%# GetYear()%>'></asp:TextBox >
                     </InsertItemTemplate>
                 </asp:TemplateField> 

Markup看起来像:

<asp:TemplateField HeaderText="Year Plant (yyyy)*"> 
   <ItemTemplate> 
       <asp:Label ID="Label1" runat="server" Text='<%# Bind("PlantYear")%>'></asp:Label> 
       <%# Eval("Year")%> 
   </ItemTemplate> 
   <EditItemTemplate> 
      <asp:TextBox ID="PlantYear" runat="server"></asp:TextBox>      
   </EditItemTemplate> 
   <InsertItemTemplate> 
      <asp:TextBox ID="PlantYear" runat="server" Text='<%# GetYear()%>'></asp:TextBox > 
   </InsertItemTemplate> 
</asp:TemplateField>

我不知道为什么没有保存这个值,所以如何解决? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

在Private Sub DetailsView1_ItemInserting事件中,我更改了

Dim txtYearPlant As TextBox = DirectCast(DirectCast(sender,DetailsView).FindControl(&#34; YearPlant&#34;),TextBox)

Dim txtYearPlant As TextBox = TryCast(view.FindControl(&#34; YearPlant&#34;),TextBox)  e.Values.Add(&#34; YearPlant&#34;,txtYearPlant.Text)

现在,PlantYear文本框中的值将保存到记录中。