2011-10-03 108 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是否正常工作。从这里看不出它是如何工作的,所以我不能就此提出任何建议。

解决这个问题的最简单方法可能是问谁写了一些帮助。

相关问题