0

我在更新面板中有一个网格视图。在 gridview 我有一个图像按钮。该按钮用于删除一行。

在网格视图的 rowCommand 事件中,我执行以下操作:

 protected void gvLineItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

            switch (e.CommandName)
            {
                case "Delete":
                    Label l = null;
                    l = (Label)row.FindControl("lblLineItemID");

                    if(l!=null)
                    {
                        long lID;
                        lID = Convert.ToInt64(l.Text);
                        BL.DeleteLineItem(Convert.ToInt64(hlID.Text), lID);
                        BindGrid(Session["SortExpression"].ToString(), Convert.ToInt32(rbSortGrid.SelectedValue));
                    }
                    break;
            }
        }

我对此进行了调试,我看到数据库中删除了该行(数据已正确删除)。但即使在绑定网格之后,gridview 仍然显示“已删除”行。绑定网格很简单,如下所示:

protected void BindGrid(string sortExpression, int sortDirection)
        {
            DataSet ds

            ds = BL.GetLineItemGridData(Convert.ToInt64(hlID.Text), sortExpression, sortDirection);

            gvLineItems.DataSource = ds.Tables[0];
            gvLineItems.DataBind();
            gvLineItems.Visible = true;
        }

数据集返回正确的行(没有删除的行),但是当我查看网页时,它仍然显示已删除的行。

编辑

有人要了gridview的HTML,这里是:

<asp:UpdatePanel ID="myPanel" runat="server" UpdateMode="Always">
             <ContentTemplate>
                <asp:GridView GridLines="Horizontal" CellPadding="4" Font-Size="Small" 
                     DataKeyNames="ID" Width="100%" AlternatingRowStyle-BackColor="#e5f1fa" 
                     BackColor="#E8E8E8" HeaderStyle-ForeColor="White" 
                     HeaderStyle-BackColor="#1367AD" ID="gvLineItems" runat="server" 
                     AllowSorting="True" AutoGenerateColumns="False" ShowFooter="True" 
                     onrowcommand="gvLineItems_RowCommand" >
                    <Columns>
                    <asp:TemplateField>
                       <ItemStyle Width="1px" />
                       <ItemTemplate>
                            <asp:Label Width=".05px" ID="lblLineItemID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>' style="display:none"></asp:Label>
                       </ItemTemplate>
                       <ControlStyle Width="0px" />
                       <HeaderStyle Width="0px" />
                     </asp:TemplateField> 
                    <asp:TemplateField>
                       <HeaderTemplate>
                          <asp:CheckBox ToolTip="Select / Deselect all rows?" ID="HeaderLevelCheckBox" onclick="toggleSelection(this);" runat="server" />
                       </HeaderTemplate>
                       <ItemTemplate>
                          <asp:CheckBox ID="chkSelector" ToolTip="Select row?" runat="server" onclick="ChangeRowColor(this)" />
                       </ItemTemplate>
                       <ItemStyle HorizontalAlign="Center" />
                       <HeaderStyle HorizontalAlign="Center" />
                    </asp:TemplateField>
                    <asp:TemplateField>
                       <ItemTemplate>
                         <asp:ImageButton ID="ibAddLineItem" runat="server" ImageUrl="images/InsertRow.gif" CommandName="Insert" ToolTip="Insert new line item."/>
                       </ItemTemplate>
                    </asp:TemplateField>  
                    <asp:TemplateField>
                       <ItemTemplate>
                         <asp:ImageButton ID="ibDeleteLineItem" runat="server" ImageUrl="images/DeleteRow.gif" CommandName="Delete" ToolTip="Delete line item."/>
                       </ItemTemplate>
                    </asp:TemplateField>                          
                    <asp:TemplateField HeaderText="Item #" SortExpression="LineItemNumber">
                         <ItemTemplate>
                             <asp:Label runat="server" ID="lblItemNumber" Visible="True" Text='<%# DataBinder.Eval(Container, "DataItem.LineItemNumber") %>' />
                         </ItemTemplate>
                         <ItemStyle Width="10%" HorizontalAlign="Center" />
                         <HeaderStyle HorizontalAlign="Center" Width="10%" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Quantity" SortExpression="LineItemQuantity">
                          <ItemTemplate>
                              <asp:TextBox Font-Names="Arial" ToolTip="Enter item quantity." ID="txtLineItemQuantity" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LineItemQuantity") %>' /> 
                              </ItemTemplate>
                              <ItemStyle Width="4%" HorizontalAlign="Center" />
                              <HeaderStyle HorizontalAlign="Center" Width="4%" />
                     </asp:TemplateField>
                    <asp:TemplateField HeaderText="Description" SortExpression="LineItemDescription">
                          <ItemTemplate>
                              <asp:TextBox Columns="15" Width="300px" Font-Names="Arial" ToolTip="Enter item description." ID="txtLineItemDescription" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LineItemDescription") %>' /> 
                              </ItemTemplate>
                              <ItemStyle Width="15%" HorizontalAlign="Left" />
                              <HeaderStyle HorizontalAlign="Left" />
                     </asp:TemplateField>
                     <asp:TemplateField HeaderText="Added By" SortExpression="AddedBy">
                         <ItemTemplate>
                             <asp:Label runat="server" ID="lblAddedBy" Visible="True" Text='<%# DataBinder.Eval(Container, "DataItem.AddedBy") %>' />
                         </ItemTemplate>
                         <ItemStyle Width="10%" HorizontalAlign="Center" />
                         <HeaderStyle HorizontalAlign="Center" Width="10%" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Added On" SortExpression="AddedOn">
                         <ItemTemplate>
                             <asp:Label runat="server" ID="lblAddedOn" Visible="True" Text='<%# DataBinder.Eval(Container, "DataItem.AddedOn") %>' />
                         </ItemTemplate>
                         <ItemStyle Width="10%" HorizontalAlign="Center" />
                         <HeaderStyle HorizontalAlign="Center" Width="10%" />
                    </asp:TemplateField>
                    </Columns>
                </asp:GridView>
             </ContentTemplate>
             <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnAddLineItem" />
                <asp:AsyncPostBackTrigger ControlID="rbSortGrid" />
             </Triggers>
         </asp:UpdatePanel>
4

2 回答 2

0

呃,我明白了,我刚刚添加了这个:

 protected void gvLineItems_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataSet ds = null;
            gvLineItems.DataSource = null;
            ds = BL.GetLineItemGridData(Convert.ToInt64(hlID.Text), Session["SortExpression"].ToString(), Convert.ToInt32(rbSortGrid.SelectedValue));

            gvLineItems.DataSource = ds.Tables[0];
            gvLineItems.DataBind();
            gvLineItems.Visible = true;
        }

并从中删除了对的调用BindGrid

 protected void gvLineItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

            switch (e.CommandName)
            {
                case "Delete":
                    Label l = null;
                    l = (Label)row.FindControl("lblLineItemID");

                    if(l!=null)
                    {
                        long lID;
                        lID = Convert.ToInt64(l.Text);
                        BL.DeleteLineItem(Convert.ToInt64(hlID.Text), lID);
                        //BindGrid(Session["SortExpression"].ToString(), Convert.ToInt32(rbSortGrid.SelectedValue));
                    }
                    break;
            }
        }
于 2012-04-24T17:52:04.750 回答
0

或者,您可以在控制事件处理完成后发生的 Page PreRender 事件中绑定 GridView。然后,您不需要连接数据源并在 rowCommand 事件中绑定网格。

于 2013-09-27T07:45:36.027 回答