【问题标题】:Updating gridview row NOT finding data is gridview row更新 gridview 行未找到数据是 gridview 行
【发布时间】:2015-09-17 14:50:51
【问题描述】:

我已经找了几天了,由于某种原因我无法解决我的问题。我有一个 gridview 控件并且想要更新选中的行。我正在尝试填充 vidInformaiton 类,传递给我的存储过程。

                  <asp:GridView ID="gvVideos" CssClass="gvVideosClass" runat="server" 
            AutoGenerateColumns="False" DataKeyNames="CustomerId"
            OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
            OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added.">
            <Columns>
                 <asp:TemplateField HeaderText="Customer">
                    <EditItemTemplate>
                        <asp:TextBox ID="customerId" runat="server" Text='<%# Bind("customerId")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblcustomerID" runat="server" Text='<%# Bind("customerId")%>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="160px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Fid" ItemStyle-Width="50">
                    <ItemTemplate>
                        <%# Container.DataItemIndex + 1 %>
                        <input type="hidden" name="vidId" value='<%# Eval("fId")%>' />
                    </ItemTemplate>

        <ItemStyle Width="50px"></ItemStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Title">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("title") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="160px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Type">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("typeContent")%>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("typeContent")%>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="160px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Youtube ID">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ytid") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ytid") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="160px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("descvid") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("descvid") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="160px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Image">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("thumbnail") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("thumbnail") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="160px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SubmitDate">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("submitdate") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("submitdate") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="160px" />
                </asp:TemplateField>
              <asp:TemplateField>
                    <ItemTemplate>
                      <asp:CheckBox ID="chkActive" runat="server" Checked='<%# Eval("active")%>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Categories">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("categories") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label8" runat="server" Text='<%# Bind("categories") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="100px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Sort order">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("sortord") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("sortord") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Width="50px" />
                </asp:TemplateField>
                <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150"/>
            </Columns>
        </asp:GridView>
        <br />


    </div>

我的代码如下所示。不知道我错过了什么。我似乎无法从行中获取值。我已经尝试了一切,DirectCast,FindControl。

Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Dim updVid As New VidInformation
    Dim updVidRecord As New vidController


    Dim row = gvVideos.Rows(e.RowIndex)
    updVid.customerId = row.Cells(1).Text.ToString

    gvVideos.EditIndex = -1
    gvVideos.DataSource = updVidRecord.UpdateVidRecord(updVid)
    bindGridview()

End Sub

【问题讨论】:

  • 您是否在按下保存按钮后尝试获取新值?然后只需使用e.NewValues["title"]
  • 是的,所以当用户更新信息并点击“更新”时。它如何知道它正在更新哪一行?
  • 这令人困惑。如果用户在行内点击“更新”,那么对于 ASP.NET 来说,哪一行被更新是很明显的。如果他们单击此按钮Update Sort Order 我非常怀疑是否会有任何行更新。还是我误解了你的问题?
  • 对不起,我关心的不是排序顺序,它只是更新当前行信息,我无法从当前行的单元格中获取值。我使用 VidInformation 类将值传递给存储过程。
  • 我会假设它会使用 EditTemplate,我应该可以使用 customerId.text???

标签: asp.net sql-server vb.net gridview


【解决方案1】:

将一系列有用的 cmets 转换为答案

获取发布到已编辑 GridView 行的新值的正确方法是使用偶数 args 对象的NewValues 属性:

Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    ...
    Dim updVid As New VidInformation
    updVid.customerId = e.NewValues("customerId")
    ...
End Sub

至于为什么最初的方法不起作用。要意识到的重要一点是,有问题的 GridView 行在页面的先前加载期间处于编辑模式。在回发期间,哪个编辑按钮触发了新值被发布到服务器,但该行不再处于编辑模式,因此在该行中找不到更多的编辑控件。因此,上述方法是获得行的新值的唯一设计方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多