我是ASP.NET的新条目,我正在尝试开发Shop WebSite。我使用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" />
<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>
我想有3种类型的物品:笔记本电脑,pendrive和键盘。现在我想在ListView中添加一列来显示每行的图像。特别是对于那些id为“111”的项目,必须显示一个笔记本图像,对于那些id为“222”的项目,必须显示一个pendrive图像和那些id为id的项目使用“333”时必须显示键盘图像。有可能的 ?我没有修改aspx.cs文件,我刚刚通过Visual接口添加了ListView控件,并从那里[1]填充了它(相对于我的数据库),因此我不想“脏“我的项目。提前谢谢!
答案 0 :(得分:0)
您只需要在代码隐藏中调用一个函数来构建图像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";
}
从ImageUrl
Image
控件的<asp:Image ID="ItemImage" runat="server"
ImageUrl='<%# BuildItemImageUrl(Eval("Id", "{0}")) %>'
/>
的数据绑定语句中调用该函数:
{{1}}