图像未在gridview排序的templatefield中显示

时间:2013-06-20 10:35:56

标签: asp.net c#-4.0 gridview

我在网格视图中遇到TemplateField问题。

有两个TemplateField(imgFSL,imgMain)根据数据库中的值显示图像。在页面加载中,网格视图显示正确的图像照片。单击gridview列排序之一后,即使网址正确,网格也不会显示图像。请帮我解决这个问题。我搜索过其他网站并尝试了很多方法。但是在单击网格视图排序后仍然没有解决它。

这是网格视图的设计器页面

<asp:UpdatePanel ID="upBOMList" runat="server">
<ContentTemplate>
    <asp:Panel ID="Panel2" runat="server" ScrollBars="Vertical" Height="350px">
        <asp:GridView ID="grdBOMList" runat="server" AutoGenerateColumns="False" 
            AllowSorting="true" BorderStyle="Solid"
            EmptyDataText="<%$ Resources:Resource, NoBOMListMsg %> " Font-Names="Verdana"
            BorderWidth="1px" CellPadding="3" CellSpacing="1" GridLines="None" Width="100%"
            BackColor="White" BorderColor="#999999" DataKeyNames="PartNo" ForeColor="Black"
            OnSelectedIndexChanged="grdBOMList_SelectedIndexChanged" OnRowDataBound="grdBOMList_RowDataBound"
            OnPageIndexChanging="grdBOMList_PageIndexChanging" 
            OnSorting="grdBOMList_Sorting" ShowFooter="False" >
            <AlternatingRowStyle CssClass="gridAlternatingRowStyle" />
            <Columns>
                <asp:BoundField ControlStyle-BorderWidth="0" DataField="ItemLocationNo" HeaderText="<%$ Resources:Resource, LocID %>"
                SortExpression="ItemLocationNo">
                    <ControlStyle BorderWidth="0px"></ControlStyle>
                    <ItemStyle CssClass="gridPaddingStyle_center" />
                    <HeaderStyle Width="50px" CssClass="gridPaddingStyle_center"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField ControlStyle-BorderWidth="0" DataField="PartCategory" HeaderText="<%$ Resources:Resource, Category %>"
                 SortExpression="PartCategory">
                    <ControlStyle BorderWidth="0px"></ControlStyle>
                    <ItemStyle CssClass="gridPaddingStyle_center" />
                    <HeaderStyle CssClass="gridPaddingStyle_center" Width="150px"></HeaderStyle>
                </asp:BoundField>
                <asp:ButtonField DataTextField="PartNo" HeaderText="<%$ Resources:Resource, PartNo %>"
                    ShowHeader="True" CommandName="Select" SortExpression="PartNo">
                    <ItemStyle CssClass="gridPaddingStyle_center" />
                    <HeaderStyle CssClass="gridPaddingStyle_center" Width="80px"></HeaderStyle>
                </asp:ButtonField>
                <asp:BoundField ControlStyle-BorderWidth="0" DataField="Description" HeaderText="<%$ Resources:Resource, Description %>"
                 SortExpression="Description">
                    <ControlStyle BorderWidth="0px"></ControlStyle>
                    <ItemStyle CssClass="gridPaddingStyle_left" />
                    <HeaderStyle CssClass="gridPaddingStyle_center" Width="150px"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField ControlStyle-BorderWidth="0" DataField="VDRCode" HeaderText="<%$ Resources:Resource, VendorsPN %>"
                 SortExpression="VDRCode">
                    <ControlStyle BorderWidth="0px"></ControlStyle>
                    <ItemStyle CssClass="gridPaddingStyle_left" />
                    <HeaderStyle CssClass="gridPaddingStyle_center" Width="80px"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField ControlStyle-BorderWidth="0" DataField="FSL" HeaderText="FSL">
                    <ControlStyle BorderWidth="0px"></ControlStyle>
                    <HeaderStyle CssClass="gridPaddingStyle_center" Width="150px"></HeaderStyle>
                </asp:BoundField>
                <asp:BoundField ControlStyle-BorderWidth="0" DataField="Main" HeaderText="<%$ Resources:Resource, Main %>">
                    <ControlStyle BorderWidth="0px"></ControlStyle>
                    <HeaderStyle CssClass="gridPaddingStyle_center" Width="150px"></HeaderStyle>
                </asp:BoundField>
                <asp:TemplateField HeaderText="FSL" HeaderStyle-Width="5%" ItemStyle-HorizontalAlign="Center"
                    FooterStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Image ID="imgFSL" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="<%$ Resources:Resource, Main %>" HeaderStyle-Width="5%"
                    ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Image ID="imgMain" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle CssClass="gridHeaderStyle" ForeColor="#ffffff" />
            <FooterStyle CssClass="gridFooterStyle" ForeColor="Black" />
            <PagerStyle CssClass="gridPagerStyle" ForeColor="#ffffff" />
            <RowStyle CssClass="gridRowStyle" ForeColor="Black" />
            <SelectedRowStyle BackColor="#2a2a7f" Font-Bold="False" ForeColor="White" />
        </asp:GridView>
    </asp:Panel>
</ContentTemplate>
<Triggers>
    <asp:PostBackTrigger ControlID="btnImportFile" />
    <asp:PostBackTrigger ControlID="btnExportFile" />
    <asp:PostBackTrigger ControlID="btnExport" />
    <asp:PostBackTrigger ControlID="btnCreate" />                                
</Triggers>

这是gridview行数据绑定,排序功能和创建表

的编码移植
public void grdBOMList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Subsidiary sub = new SubsidiaryFacade().GetBySubsidiaryCode(userSubsidiaryCode, userid, userEntityCode);
            int redInd = (int)sub.RedIndicatorQty;
            int greenInd = (int)sub.GreenIndicatorQty;

            string fsl = e.Row.Cells[5].Text;
            string main = e.Row.Cells[6].Text;

            System.Web.UI.WebControls.Image imgFSL = (System.Web.UI.WebControls.Image)e.Row.FindControl("imgFSL");

            if (fsl != string.Empty && fsl != null)
            {
                int quantityFSL = Convert.ToInt32(fsl); 
                if (quantityFSL != null)
                {
                    if (quantityFSL <= redInd)
                    {
                        imgFSL.ImageUrl = "~/image/Circle_Red.png";
                        //imgFSL.ImageUrl = ResolveUrl("~/image/Circle_Red.png");
                        //imgFSL.ImageUrl = Page.ResolveClientUrl("~/image/Circle_Red.png");
                    }
                    else if (redInd < quantityFSL && quantityFSL < greenInd)
                    {
                        imgFSL.ImageUrl = "~/image/Circle_Orange.png";
                        //imgFSL.ImageUrl = ResolveUrl("~/image/Circle_Orange.png");
                        //imgFSL.ImageUrl = Page.ResolveClientUrl("~/image/Circle_Orange.png");
                    }
                    else if (quantityFSL >= greenInd)
                    {
                        imgFSL.ImageUrl = "~/image/Circle_Green.png";
                        //imgFSL.ImageUrl = ResolveUrl("~/image/Circle_Green.png");
                        //imgFSL.ImageUrl = Page.ResolveClientUrl("~/image/Circle_Green.png");
                    }
                }
            }

            if (main != string.Empty && main != null)
            {
                int quantityMain = Convert.ToInt32(main);
                System.Web.UI.WebControls.Image imgMain = (System.Web.UI.WebControls.Image)e.Row.FindControl("imgMain");

                if (quantityMain != null)
                {
                    if (quantityMain <= redInd)
                    {
                        imgMain.ImageUrl = "~/image/Circle_Red.png";
                    }
                    else if (redInd < quantityMain && quantityMain < greenInd)
                    {
                        imgMain.ImageUrl = "~/image/Circle_Orange.png";
                    }
                    else if (quantityMain >= greenInd)
                    {
                        imgMain.ImageUrl = "~/image/Circle_Green.png";
                    }
                }
            }
        }
    }

这是网格排序

protected void grdBOMList_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        string direction = string.Empty;

        if (SortDirection == SortDirection.Ascending)
        {
            SortDirection = SortDirection.Descending;
            direction = "DESC";
        }

        else
        {
            SortDirection = SortDirection.Ascending;
            direction = "ASC";
        }

        BindBOMListGrid();

        List<BOM> list = (List<BOM>)grdBOMList.DataSource;

        DataTable dt = CreateDataTable_BOM();
        if (list.Count > 0)
        {
            for (int i = 0; i < list.Count; i++)
            {
                BOM b = list[i];
                DataRow row;
                row = dt.NewRow();

                row["ItemLocationNo"] = b.ItemLocationNo;
                row["PartCategory"] = b.PartCategory;
                row["PartNo"] = b.PartNo;
                row["Description"] = b.Description;
                row["VDRCode"] = b.VDRCode;
                int fslvalue = Convert.ToInt32(b.FSL);

                Subsidiary sub = new SubsidiaryFacade().GetBySubsidiaryCode(userSubsidiaryCode, userid, userEntityCode);
                int redInd = (int)sub.RedIndicatorQty;
                int greenInd = (int)sub.GreenIndicatorQty;

                if (Convert.ToString(b.FSL) != string.Empty && b.FSL != null)
                {
                    int quantityFSL = Convert.ToInt32(b.FSL);
                    if (quantityFSL != null)
                    {
                        if (quantityFSL <= redInd)
                        {
                            row["imgFSL"] = ResolveUrl("~/image/Circle_Red.png");
                        }
                        else if (redInd < quantityFSL && quantityFSL < greenInd)
                        {
                            row["imgFSL"] = ResolveUrl("~/image/Circle_Orange.png");
                        }
                        else if (quantityFSL >= greenInd)
                        {
                            row["imgFSL"] = ResolveUrl("~/image/Circle_Green.png");
                        }
                    }
                }

                dt.Rows.Add(row);
            }
        }

        dt.DefaultView.Sort = sortExpression + " " + direction;

        grdBOMList.DataSource = dt;
        grdBOMList.DataBind();
        ModalPopupExtender.Show();

    }

这是为网格视图创建数据表

private DataTable CreateDataTable_BOM()
    {
        DataTable myDataTable = new DataTable();
        DataColumn myDataColumn;

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "ItemLocationNo";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "PartCategory";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "PartNo";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Description";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "VDRCode";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.Int32");
        myDataColumn.ColumnName = "FSL";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "imgFSL";
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.Int32");
        myDataColumn.ColumnName = "Main";
        myDataTable.Columns.Add(myDataColumn);

        return myDataTable;
    }

在页面加载中,网格显示图像(排序前)

enter image description here

单击其中一个网格列排序后,即使网址正确,图像也不显示

enter image description here

请给我任何建议。

1 个答案:

答案 0 :(得分:0)

可能是因为您没有注册任何gridview事件的触发器。

尝试为gridview添加回发触发器。为:

<asp:PostBackTrigger ControlID="grdBOMList" />