在gridview中显示默认img

时间:2017-02-12 12:23:39

标签: asp.net gridview

我在gridview 中列出了我的所有用户,我将为那些没有个人资料图片的人显示默认图片。

我看过这个解决方案,但是我无法让它工作,我做错了什么。

Displaying a default image in gridview

我的代码Gridview控件

        <asp:Panel ID="pnlListeOverBruger" runat="server">
        <asp:GridView ID="gwListeOverBruger" CssClass="table table-bordered table-hover" OnRowDataBound="gwListeOverBruger_RowDataBound" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="fldId">
            <Columns>
                <%-- profil billede --%>
                <asp:TemplateField  HeaderText="Billede" SortExpression="fldBrugernavn">
                    <ItemTemplate>                           
                     <asp:Image ID="img" ImageUrl='../img/profil/<%#Eval("fldImg") %>' runat="server" style='width: 100px; overflow: hidden;'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <%-- brugernavn --%>
                <asp:TemplateField HeaderText="Brugernavn" SortExpression="fldBrugernavn">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtBrugernavn" runat="server" Text='<%# Bind("fldBrugernavn") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBrugernavn" runat="server" Text='<%# Bind("fldBrugernavn") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <%-- Email --%>
                <asp:TemplateField HeaderText="Email" SortExpression="fldEmail">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("fldEmail") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblEmail" runat="server" Text='<%# Bind("fldEmail") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <%-- bruger rolle --%>
                <asp:TemplateField HeaderText="Bruger rolle" SortExpression="fldRolle">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtBrugerRolle" runat="server" Text='<%# Bind("fldRolle") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBrugerRolle" runat="server" Text='<%# Bind("fldRolle") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <%-- Slette knappen --%>
                <asp:CommandField HeaderText="Handling" ControlStyle-CssClass="btn btn-danger" ShowDeleteButton="True" />
            </Columns>
            <PagerStyle CssClass="GridPager" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DBcmsListeOverBruger %>" DeleteCommand="DELETE FROM CmsBrugere WHERE (fldId = @original_fldId) AND (fldRolle_fk &gt; '1')" InsertCommand="INSERT INTO [CmsBrugere] ([fldBrugernavn]) VALUES (@fldBrugernavn)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT CmsBrugere.fldId, CmsBrugere.fldImg, CmsBrugere.fldBrugernavn, CmsBrugere.fldEmail, CmsBrugerRoller.fldBrugerId_fk, CmsBrugerRoller.fldRolle FROM CmsBrugere LEFT OUTER JOIN CmsBrugerRoller ON CmsBrugerRoller.fldId = CmsBrugere.fldRolle_fk" UpdateCommand="UPDATE [CmsBrugere] SET [fldBrugernavn] = @fldBrugernavn WHERE [fldId] = @original_fldId">
            <DeleteParameters>
                <asp:Parameter Name="original_fldId" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="fldBrugernavn" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="fldBrugernavn" Type="String" />
                <asp:Parameter Name="original_fldId" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </asp:Panel>

Codebehind Pageload

  protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {


        foreach (GridViewRow gvr in gwListeOverBruger.Rows)
        {
            Image image = (Image)gvr.FindControl("img");

            if (image != null && image.ImageUrl == "")
            {
                image.ImageUrl = "../img/profil/default-billede/ingen-profil-billede.png";
            }




        }
    }
}

Codebehind OnRowDataBound

 protected void gwListeOverBruger_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image profilImg = (Image)e.Row.FindControl("img");

        if (profilImg != null && profilImg.ImageUrl == "")
        {
            profilImg.ImageUrl = "../img/profil/default-billede/ingen-profil-billede.png";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在Image Control中使用三元运算符来检查是否有图像,如果没有显示默认图像。

StartupUri="MainWindow.xaml"