更新gridview行不查找数据是gridview行

时间:2015-09-17 14:50:52

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

我现在已经看了几天,由于某种原因,我无法解决我的问题。我有一个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

1 个答案:

答案 0 :(得分:0)

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

将新值发布到已编辑的GridView行的正确方法是使用even 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行处于编辑模式。在回发时,编辑按钮触发的新值会发布到服务器,但该行不再处于编辑模式,因此在此行中找不到更多编辑控件。因此,上述方法是获取行的新值的唯一设计方法。