Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 向ListView添加图像_Asp.net_Image_Listview - Fatal编程技术网

Asp.net 向ListView添加图像

Asp.net 向ListView添加图像,asp.net,image,listview,Asp.net,Image,Listview,我是ASP.NET的新手,我正在尝试开发一个商店网站。我使用ListView来显示所有项目 <%@ Page Title="" Language="C#" MasterPageFile="~/Hepsiburada.master" AutoEventWireup="true" CodeFile="IndexItems.aspx.cs" Inherits="IndexItems" %> <asp:Content ID="Content1" ContentPlaceHolde

我是ASP.NET的新手,我正在尝试开发一个商店网站。我使用ListView来显示所有项目

  <%@ Page Title="" Language="C#" MasterPageFile="~/Hepsiburada.master" AutoEventWireup="true" CodeFile="IndexItems.aspx.cs" Inherits="IndexItems" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="bodyContent" Runat="Server">
    <table cellpadding="0" cellspacing="0" class="style1">
        <tr>
            <td>

                Welcome <asp:LoginName ID="loginName" runat="server" />
            <asp:LoginStatus ID="chiusuraSessione" LogoutText ="Click here to logout" runat="server" LogoutAction="Redirect" LogoutPageUrl="~/LoginPage.aspx" OnLoggingOut="chiusuraSessione_LoggingOut"  />

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="shoppingCartButton" runat="server" Font-Bold="False" Font-Italic="False" Font-Size="Large" Height="34px" Text="Shopping Cart" Width="154px"  PostBackUrl="~/Checkout.aspx" />

            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="selctItemsLabel" runat="server" Font-Bold="True" Text="Select Items to buy from the below list of items :"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="formatErrorLabel" runat="server" ForeColor="Red"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:ListView ID ="ItemsList" runat="server" DataSourceID="SqlDataSource" DataKeyNames="id" onitemcommand="ItemsList_ItemCommand"  >



                    <AlternatingItemTemplate>
                        <tr style="">


                             <td>
                                <asp:Button ID="addToCartButton" runat="server" Text="Add" CommandName="Add"/>
                            </td>

                            <td>
                                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                            </td>
                            <td>
                                <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                            </td>
                            <td>
                                <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />
                            </td>
                            <td>
                                <asp:Label ID="quantityLabel" runat="server" Text='<%# Eval("quantity") %>' />
                            </td>


                             <td>
                                <asp:TextBox ID="amountTextBox" runat="server" Text="0" Width="20px" />
                            </td>
                        </tr>
                    </AlternatingItemTemplate>
                    <EditItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
                                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Add" Text="Add">
                        </asp:LinkButton>

                            </td>



                            <td>
                                <asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="priceTextBox" runat="server" Text='<%# Bind("price") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="quantityTextBox" runat="server" Text='<%# Bind("quantity") %>' />
                            </td>
                        </tr>
                    </EditItemTemplate>
                    <EmptyDataTemplate>
                        <table runat="server" style="">
                            <tr>
                                <td>No data was returned.</td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>
                    <InsertItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
                            </td>



                            <td>
                                <asp:TextBox ID="idTextBox" runat="server" Text='<%# Bind("id") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="priceTextBox" runat="server" Text='<%# Bind("price") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="quantityTextBox" runat="server" Text='<%# Bind("quantity") %>' />
                            </td>
                            <td><asp:Button ID="Buttonbal" runat="server" 
        CommandName="Insert" Text="Insert" /></td>

                        </tr>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <tr style="">


                            <td>
                                <asp:Button ID="addToCartButton" runat="server" Text="Add" CommandName="Add" />
                            </td>

                            <td>
                                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                            </td>
                            <td>
                                <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                            </td>
                            <td>
                                <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />
                            </td>
                            <td>
                                <asp:Label ID="quantityLabel" runat="server" Text='<%# Eval("quantity") %>' />
                            </td>



                             <td>
                                <asp:TextBox ID="amountTextBox" runat="server" Text="0" Width="20px" />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                                        <tr runat="server" style="">
                                             <th runat="server">  </th>
                                            <th runat="server">id</th>
                                            <th runat="server">name</th>
                                            <th runat="server">price</th>
                                            <th runat="server">quantity</th>
                                        </tr>
                                        <tr id="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" style="">
                                    <asp:DataPager ID="DataPager1" runat="server">
                                        <Fields>
                                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
                                        </Fields>
                                    </asp:DataPager>
                                </td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                    <SelectedItemTemplate>
                        <tr style="">
                            <td>
                                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                            </td>
                            <td>
                                <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                            </td>
                            <td>
                                <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />
                            </td>
                            <td>
                                <asp:Label ID="quantityLabel" runat="server" Text='<%# Eval("quantity") %>' />
                            </td>
                        </tr>
                    </SelectedItemTemplate>
                </asp:ListView>

                <asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [items]"></asp:SqlDataSource>

            </td>
        </tr>
        </table>
</asp:Content>


],因此我不想“弄脏”我的项目。提前谢谢你

只需调用代码隐藏中的函数即可构建图像URL:

protected static string BuildItemImageUrl(string id)
{
    if (id.StartsWith("111"))
    {
        return "~/images/notebook.jpg";
    }
    if (id.StartsWith("222"))
    {
        return "~/images/pendrive.jpg";
    }
    if (id.StartsWith("333"))
    {
        return "~/images/keyboard.jpg";
    }

    return "~/images/unknown.jpg";
}
图像
控件的
图像URL
的数据绑定语句调用函数:

<asp:Image ID="ItemImage" runat="server"
    ImageUrl='<%# BuildItemImageUrl(Eval("Id", "{0}")) %>'
/>


如果您有关于如何修改代码以执行某项操作的问题,请提供您的代码,这样答案就不必从头开始,在收到答案时会更有用。更新问题以包含代码,请不要将其作为注释发布(出于可读性目的)。不起作用。此外,它还表示“警告元素‘Image’不是已知元素。如果网站中存在编译错误,或者Web.config文件丢失,则可能会发生这种情况。”。必须将图像控件放置在何处?@Fabrizio:它需要位于
ItemTemplate
/
AlternatingItemTemplate
中的某个位置。您需要将其放入
标记中,使其显示在表中。