1

我为我的 gridview 设置了分页,现在Guid should contain 32 digits with 4 dashes当我单击分页器中的另一个页面时,我收到一条错误消息。当友好的错误消息弹出时,我单击确定,网格仍然填充下一页数据。

调试已在此方法中捕获了异常:

protected void cntrlGrdVwDisplayProducts_RowCommand ( object sender , GridViewCommandEventArgs e )
    {
        Guid productID;
        try
        {
            productID = new Guid ( e.CommandArgument.ToString ( ) );
            if ( ( ( e.CommandName != null ) & ( e.CommandArgument != null ) ) )
            {
                switch ( e.CommandName.ToString ( ).ToLower ( ) )
                {
                    case "editproduct":
                        EditProduct ( productID );
                        break;
                    case "deleteproduct":
                        DeleteProduct ( productID );
                        break;
                    case "publishproduct":

                        string currentStatus = ((LinkButton)e.CommandSource).Text.Trim();
                        int newStatus = -1;
                        if ( string.Equals ( currentStatus , "Publish" ) )
                        {
                            PublishProduct ( productID , true );

                        }
                        else if ( string.Equals ( currentStatus , "UnPublish" ) )
                        {
                            PublishProduct ( productID , false );

                        }
                        break;

                }
            }
        }
        catch ( Exception ex )
        {
            Forbin.Logging.Log.LogException ( Forbin.Logging.PageType.ProductManage , Forbin.Logging.MessageType.Exception , ex.ToString ( ) );
            UIUtils.ShowMessageToUser ( "OnErrorMesg" , this.Page );
        }
    }

这是我的分页方法以防万一:

protected void cntrlGrdVwDisplayProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        cntrlGrdVwDisplayProducts.PageIndex = e.NewPageIndex;
        BindDataToUI();
    }

网格视图:

<asp:GridView ID="cntrlGrdVwDisplayProducts" runat="server" AutoGenerateColumns="False" AllowPaging="true" PageSize="100" PagerSettings-Mode="NextPreviousFirstLast" OnPageIndexChanging="cntrlGrdVwDisplayProducts_PageIndexChanging" OnRowCommand="cntrlGrdVwDisplayProducts_RowCommand" GridLines="None" OnRowDataBound="cntrlGrdVwDisplayProducts_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Product Name">
                    <ItemTemplate>
                        <div class="AdminFontText">
                            <asp:Label ID="grdLblPrdctName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ProductName") %>'>
                            </asp:Label>
                        </div>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" />
                    <ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Item Number">
                    <ItemTemplate>
                        <div class="AdminFontText">
                            <asp:Label ID="grdLblItemNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ItemNumber") %>'>
                            </asp:Label>
                        </div>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" />
                    <ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <div class="AdminFontText">
                            <asp:Label ID="grdLblPrdctDescription" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Description") %>'>
                            </asp:Label>
                        </div>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Bottom" HorizontalAlign="Left" CssClass="AdminFontHead" />
                    <ItemStyle VerticalAlign="Top" HorizontalAlign="Left" CssClass="AdminFontText" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:LinkButton ID="grdBtnPrdctEdit" runat="server" Text="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ProductID") %>'
                            CommandName="editproduct" CssClass="btn_edit"></asp:LinkButton>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" CssClass="btnFontHead" />
                    <ItemStyle Width="90" VerticalAlign="Top" HorizontalAlign="Center" CssClass="btnFontHead" />
                </asp:TemplateField>
           </Columns>
</asp:GridView>

数据绑定方法:

private void BindDataToUI ( )
    {
        //btnSaveData.Text = "Save";
        List<MappedCategoryInfo> categoryMappings = BusinessFactory.GetCategoryManager ( ).GetCategoryMappings ( );
        BindCategoryFilters ( cntrlDdlFltrCategory , categoryMappings );
        if ( cntrlDdlFltrCategory.Items.Count > 0 )
        {
            cntrlDdlFltrCategory.SelectedIndex = 0;
            cntrlDdlFltrSubCategory.Enabled = false;
            List<ProductInfo> allProducts = new List<ProductInfo> ( );
            Guid subCategoryID = Guid.Empty;
            switch ( cntrlDdlFltrCategory.SelectedItem.Value )
            {
                case All:
                    allProducts = BusinessFactory.GetProductManager ( ).GetAllProducts ( );
                    cntrlDdlFltrSubCategory.Enabled = false;
                    break;
                default:
                    Guid categoryID = new Guid ( cntrlDdlFltrCategory.SelectedValue );
                    BindSubcategoryFilters ( categoryID , cntrlDdlFltrSubCategory , categoryMappings );
                    if ( cntrlDdlFltrSubCategory.Items.Count > 0 )
                    {
                        cntrlDdlFltrSubCategory.Enabled = true;
                        switch ( cntrlDdlFltrSubCategory.SelectedItem.Value )
                        {
                            case All:
                                foreach ( ListItem subCategoryItem in cntrlDdlFltrSubCategory.Items )
                                {
                                    if ( subCategoryItem.Value != All )
                                    {
                                        subCategoryID = new Guid ( subCategoryItem.Value );
                                        allProducts.AddRange ( BusinessFactory.GetProductManager ( ).GetPublAndUnPublProductBasedOnCategory ( subCategoryID ) );
                                    }
                                }
                                break;
                            default:
                                subCategoryID = new Guid ( cntrlDdlFltrSubCategory.SelectedItem.Value );
                                allProducts.AddRange( BusinessFactory.GetProductManager().GetPublAndUnPublProductBasedOnCategory( subCategoryID ) );
                                break;
                        }
                    }
                    break;
            }
            NumRecords.InnerText = allProducts.Count.ToString();
            cntrlGrdVwDisplayProducts.DataSource = allProducts;
            cntrlGrdVwDisplayProducts.DataBind ( );
        }

    }
4

1 回答 1

0

尝试在此行之前检查 e.CommandName :

productID = new Guid ( e.CommandArgument.ToString ( ) );

如果您正在分页或排序,则 ProductID 将不在 e.CommandArgument 属性中。

所以试试

if (e.CommandName == "Page") { return; }
productID = new Guid ( e.CommandArgument.ToString ( ) );
于 2013-10-30T20:51:12.337 回答