分页上的ASP.NET Gridview错误

时间:2013-10-30 19:24:20

标签: asp.net gridview grid paging

我为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();
    }

的GridView:

<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 ( );
        }

    }

1 个答案:

答案 0 :(得分:0)

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

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

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

所以试试

if (e.CommandName == "Page") { return; }
productID = new Guid ( e.CommandArgument.ToString ( ) );
相关问题