2011-10-03 145 views
0

所以發生的是我點擊編輯按鈕,鍵入更新的值,然後點擊更新。但代碼隱藏獲取原始值而不是更新的值。我無法弄清楚爲什麼。它以前一直很有用。當我更新GridViewRow時,爲什麼新值沒有拉上來?

標記

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
    CellPadding="7" ForeColor="#333333" GridLines="None" Font-Size="Small" 
    ShowFooter="True" DataKeyNames="CapID"> 
    <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
    <asp:TemplateField HeaderText="AllocationId"> 
     <ItemTemplate> 
     <asp:Label ID="show" runat="server" Text='<%# Eval("CapID") %>' /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Reference Number"> 
     <ItemTemplate> 
     <asp:Label ID="showRefNo" runat="server" Text='<%# Eval("RefNo") %>'/> 
     </ItemTemplate> 
     <EditItemTemplate> 
     <asp:TextBox runat="server" ID="EditRefNo" 
        Text='<%# Bind("RefNo") %>'/> 
     </EditItemTemplate> 
     <FooterTemplate> 
     <asp:TextBox runat="server" ID="InsertRefNo" 
        Text='<%# Bind("RefNo") %>'/> 
     </FooterTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField HeaderText="Resource"> 
     <ItemTemplate> 
     <asp:Label ID="showResource" runat="server" 
        Text='<%# Eval("Resource") %>'/> 
     </ItemTemplate> 
     <EditItemTemplate> 
     <asp:TextBox runat="server" ID="EditResource" 
        Text='<%# Bind("Resource") %>'/> 
     </EditItemTemplate> 
     <FooterTemplate> 
     <asp:TextBox runat="server" ID="InsertResource" 
        Text='<%# Bind("Resource") %>'/> 
     </FooterTemplate> 
    </asp:TemplateField> 
    <!-- and so on... --> 
    </Columns> 
    <!-- styles etc -->   
    <EmptyDataTemplate> 
    Ref Num:&nbsp;<asp:TextBox ID="newRefNo" runat="server"/> 
    Resource:&nbsp;<asp:TextBox ID="newResource" runat="server"/> 
    Hours Allocated:&nbsp;<asp:TextBox ID="newHours" runat="server"/> 
    Week Offset:&nbsp;<asp:TextBox ID="newOffset" runat="server"/> 
    <asp:Button runat="server" ID="NewDataInsert" 
       CommandName="NewDataInsert" Text="Insert"/> 
    </EmptyDataTemplate> 
</asp:GridView> 

代碼隱藏

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
    Handles Me.Load 
     If Not IsPostBack Then 
      GridView1_DataBind() 
      GridView2_DataBind() 
     End If 
End Sub 


Protected Sub GridView2_RowUpdating(ByVal sender As Object, ByVal e As 
    GridViewUpdateEventArgs) Handles GridView2.RowUpdating 

    Dim capID As Label = GridView2.Rows(e.RowIndex).Cells(0) 
     .FindControl("show") 
    Dim refNo As TextBox = GridView2.Rows(e.RowIndex).Cells(1) 
     .FindControl("EditRefNo") 
    Dim resource As TextBox = 
     GridView2.Rows(e.RowIndex).Cells(2).FindControl("EditResource") 
    Dim hours As TextBox = 
     GridView2.Rows(e.RowIndex).Cells(3).FindControl("EditHours") 
    Dim offSet As TextBox = 
     GridView2.Rows(e.RowIndex).Cells(4).FindControl("EditOffset") 

    Dim newResAlloc As DataTable = resourceInfo.loadResAllocations 
    Dim updateRows As DataRow() = 
     newResAlloc.Select("CapID = " & "'" & capID.Text & "'") 

    If (Not updateRows Is Nothing) And updateRows.Length > 0 Then 
     For Each updRow As DataRow In updateRows 
      updRow.BeginEdit() 

      updRow.Item("Refno") = refNo.Text 
      updRow.Item("Resource") = resource.Text 
      updRow.Item("Hours") = hours.Text 
      updRow.Item("Offset") = offSet.Text 

      updRow.EndEdit() 
     Next 
    End If 

    resourceInfo.updateResAllocations(newResAlloc) 

    GridView2.EditIndex = -1 
    GridView2_DataBind() 
End Sub 

回答

0

的問題是,我的RowCommand方法調用的DataBind

錯誤方式:

Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView2.RowCommand 

    If e.CommandName = "NewDataInsert" Then 

     Dim refNo As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewRefNo") 
     Dim resource As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewResource") 
     Dim hours As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewHours") 
     Dim offset As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewOffset") 

     Dim newResAlloc As DataTable = resourceInfo.loadResAllocations 
     Dim newAllocRow As DataRow = newResAlloc.NewRow 
     newAllocRow.ItemArray = New Object() {Nothing, refNo.Text, resource.Text, hours.Text, offset.Text} 
     newResAlloc.Rows.Add(newAllocRow) 

     resourceInfo.updateResAllocations(newResAlloc) 

    ElseIf e.CommandName = "InsertNew" Then 

     Dim refNo As TextBox = GridView2.FooterRow.FindControl("InsertRefNo") 
     Dim resource As TextBox = GridView2.FooterRow.FindControl("InsertResource") 
     Dim hours As TextBox = GridView2.FooterRow.FindControl("InsertHours") 
     Dim offset As TextBox = GridView2.FooterRow.FindControl("InsertOffset") 

     Dim newResAlloc As DataTable = resourceInfo.loadResAllocations 
     Dim newAllocRow As DataRow = newResAlloc.NewRow 
     newAllocRow.ItemArray = New Object() {Nothing, refNo.Text, resource.Text, hours.Text, offset.Text} 
     newResAlloc.Rows.Add(newAllocRow) 

     resourceInfo.updateResAllocations(newResAlloc) 


    End If 

    GridView2_DataBind() 'Culprit 
End Sub 

正確方法:

Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles GridView2.RowCommand 

    If e.CommandName = "NewDataInsert" Then 

     Dim refNo As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewRefNo") 
     Dim resource As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewResource") 
     Dim hours As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewHours") 
     Dim offset As TextBox = GridView2.Controls(0).Controls(0).FindControl("NewOffset") 

     Dim newResAlloc As DataTable = resourceInfo.loadResAllocations 
     Dim newAllocRow As DataRow = newResAlloc.NewRow 
     newAllocRow.ItemArray = New Object() {Nothing, refNo.Text, resource.Text, hours.Text, offset.Text} 
     newResAlloc.Rows.Add(newAllocRow) 

     resourceInfo.updateResAllocations(newResAlloc) 

     GridView2_DataBind() 'Only called if IF is true 

    ElseIf e.CommandName = "InsertNew" Then 

     Dim refNo As TextBox = GridView2.FooterRow.FindControl("InsertRefNo") 
     Dim resource As TextBox = GridView2.FooterRow.FindControl("InsertResource") 
     Dim hours As TextBox = GridView2.FooterRow.FindControl("InsertHours") 
     Dim offset As TextBox = GridView2.FooterRow.FindControl("InsertOffset") 

     Dim newResAlloc As DataTable = resourceInfo.loadResAllocations 
     Dim newAllocRow As DataRow = newResAlloc.NewRow 
     newAllocRow.ItemArray = New Object() {Nothing, refNo.Text, resource.Text, hours.Text, offset.Text} 
     newResAlloc.Rows.Add(newAllocRow) 

     resourceInfo.updateResAllocations(newResAlloc) 

     GridView2_DataBind() 'Only called if IF is true 
    End If 


End Sub 
0

這可能是一百萬,一個東西。你檢查了什麼?你把它縮小了嗎?

下面是一些出發點爲您提供:

  • 將斷點內GridView2_RowUpdating,以確保它被調用。
  • 檢查的capID值,或capID.Text - 它不應該是0
  • 檢查你的數據庫表中包含的行,其中促進會等於capID.Text
  • 值將斷點上updateRows,以確保數據庫行正在加載到您的代碼中。
  • 確保updRow.EndEdit()正在被擊中,並且值正確填充到行中。
  • 最後,檢查updateResAllocations是否正常工作。從這裏看不出它是如何工作的,所以我不能就此提出任何建議。

解決這個問題的最簡單方法可能是問誰寫了一些幫助。

相關問題