当我更新GridViewRow时,为什么新值不会上升?

时间:2011-10-03 21:11:08

标签: asp.net vb.net webforms

所以发生的事情是我单击“编辑”按钮,键入更新的值并点击“更新”。但代码隐藏获取原始值而不是更新值。我无法弄清楚为什么。它以前总是有效。

标记

<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

2 个答案:

答案 0 :(得分:0)

这可能是一百万件事。你检查了什么?你把它缩小了吗?

以下是您的一些起点:

  • GridView2_RowUpdating内放置一个断点,以确保它被调用。
  • 检查capIDcapID.Text的值 - 它不应为0.
  • 检查您的数据库表是否包含CapID等于capID.Text
  • 值的行
  • updateRows上放置一个断点,以确保将数据库行加载到您的代码中。
  • 确保点击了updRow.EndEdit(),并且正确地将值填充到行中。
  • 最后,检查updateResAllocations是否正常工作。从这里看它是如何工作的是不可能的,所以我不能就此提出任何建议。

解决这个问题的最简单方法可能是询问是谁写的,以获得一些帮助。

答案 1 :(得分: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