C# asp.net alt 标记问题
C# asp.net alt tag issue
大家好,我是该站点的新手,有一个关于 asp.net 中的 c# 的问题。我使用 HTML5 标记和 asp.net 中的 CSS 制作了一个网站,并使用 c# 作为后台代码。我遇到的问题是我制作了一个 index.aspx 页面,后面有 c# 代码,以在面板中显示我的所有产品,这些产品是使用 Fillpage 方法直接从图像文件夹中检索的。我遇到的问题是我不确定如何向图像添加 alt 标签,因为直到我 运行 我的网站在浏览器中我才真正看到图像。我测试了我的网站的可访问性,结果显示缺少 alt 标签错误。任何人都可以给我一些关于如何将 alt 标签添加到图像的建议吗?
谢谢你。
index.aspx.cs
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Fillpage();
}
private void Fillpage()
{
//Retrieve list of all products in the database
ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetAllProducts();
//check products exist in the database
if (products !=null)
{
//create a new panel with an imagebutton and 2 labels for each product
foreach (Product product in products)
{
Panel productPanel = new Panel();
ImageButton imageButton = new ImageButton();
Label lblName = new Label();
Label lblPrice = new Label();
//Set childControls properties
imageButton.ImageUrl = "~/Images/Products/" + product.Image;
imageButton.CssClass = "productImage";
imageButton.PostBackUrl = "~/pages/Product.aspx?id=" + product.ID;
lblName.Text = product.Name;
lblName.CssClass = "productName";
lblPrice.Text = "£" + product.Price;
lblPrice.CssClass = "productPrice";
//Add childControls to the panel
productPanel.Controls.Add(imageButton);
productPanel.Controls.Add(new Literal{Text = "<br />"});
productPanel.Controls.Add(lblName);
productPanel.Controls.Add(new Literal{Text = "<br />"});
productPanel.Controls.Add(lblPrice);
//Add dynamic paneld to static parent panel
pnlProducts.Controls.Add(productPanel);
}
}
else
{
//no products found
pnlProducts.Controls.Add(new Literal {Text = "No Products found!"});
}
}
}
product.aspx.cs
using System;
using System.Linq;
using Microsoft.AspNet.Identity;
public partial class Pages_Product : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
string clientId = Context.User.Identity.GetUserId();
if (clientId != null)
{
int id = Convert.ToInt32(Request.QueryString["id"]);
int amount = Convert.ToInt32(ddlAmount.SelectedValue);
Cart cart = new Cart
{
Amount = amount,
ClientID = clientId,
DatePurchased = DateTime.Now,
IsInCart = true,
ProductID = id
};
CartModel model = new CartModel();
lblResult.Text = model.InsertCart(cart);
}
else
{
lblResult.Text = " Please log in to order products ";
}
}
}
private void FillPage()
{
//Get selected product data
if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
int id = Convert.ToInt32(Request.QueryString["id"]);
ProductModel model = new ProductModel();
Product product = model.GetProduct(id);
//Fill page with data
lblTitle.Text = product.Name;
lblDescription.Text = product.Description;
lblPrice.Text = "Price per unit:<br/>£ " + product.Price;
imgProduct.ImageUrl = "~/Images/Products/" + product.Image;
lblItemNr.Text = product.ID.ToString();
//Fill amount list with numbers 1-20
int[] amount = Enumerable.Range(1, 20).ToArray();
ddlAmount.DataSource = amount;
ddlAmount.AppendDataBoundItems = true;
ddlAmount.DataBind();
}
}
}
使用imageButton.AlternateText
属性。
大家好,我是该站点的新手,有一个关于 asp.net 中的 c# 的问题。我使用 HTML5 标记和 asp.net 中的 CSS 制作了一个网站,并使用 c# 作为后台代码。我遇到的问题是我制作了一个 index.aspx 页面,后面有 c# 代码,以在面板中显示我的所有产品,这些产品是使用 Fillpage 方法直接从图像文件夹中检索的。我遇到的问题是我不确定如何向图像添加 alt 标签,因为直到我 运行 我的网站在浏览器中我才真正看到图像。我测试了我的网站的可访问性,结果显示缺少 alt 标签错误。任何人都可以给我一些关于如何将 alt 标签添加到图像的建议吗? 谢谢你。 index.aspx.cs
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Fillpage();
}
private void Fillpage()
{
//Retrieve list of all products in the database
ProductModel productModel = new ProductModel();
List<Product> products = productModel.GetAllProducts();
//check products exist in the database
if (products !=null)
{
//create a new panel with an imagebutton and 2 labels for each product
foreach (Product product in products)
{
Panel productPanel = new Panel();
ImageButton imageButton = new ImageButton();
Label lblName = new Label();
Label lblPrice = new Label();
//Set childControls properties
imageButton.ImageUrl = "~/Images/Products/" + product.Image;
imageButton.CssClass = "productImage";
imageButton.PostBackUrl = "~/pages/Product.aspx?id=" + product.ID;
lblName.Text = product.Name;
lblName.CssClass = "productName";
lblPrice.Text = "£" + product.Price;
lblPrice.CssClass = "productPrice";
//Add childControls to the panel
productPanel.Controls.Add(imageButton);
productPanel.Controls.Add(new Literal{Text = "<br />"});
productPanel.Controls.Add(lblName);
productPanel.Controls.Add(new Literal{Text = "<br />"});
productPanel.Controls.Add(lblPrice);
//Add dynamic paneld to static parent panel
pnlProducts.Controls.Add(productPanel);
}
}
else
{
//no products found
pnlProducts.Controls.Add(new Literal {Text = "No Products found!"});
}
}
}
product.aspx.cs
using System;
using System.Linq;
using Microsoft.AspNet.Identity;
public partial class Pages_Product : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
string clientId = Context.User.Identity.GetUserId();
if (clientId != null)
{
int id = Convert.ToInt32(Request.QueryString["id"]);
int amount = Convert.ToInt32(ddlAmount.SelectedValue);
Cart cart = new Cart
{
Amount = amount,
ClientID = clientId,
DatePurchased = DateTime.Now,
IsInCart = true,
ProductID = id
};
CartModel model = new CartModel();
lblResult.Text = model.InsertCart(cart);
}
else
{
lblResult.Text = " Please log in to order products ";
}
}
}
private void FillPage()
{
//Get selected product data
if (!string.IsNullOrWhiteSpace(Request.QueryString["id"]))
{
int id = Convert.ToInt32(Request.QueryString["id"]);
ProductModel model = new ProductModel();
Product product = model.GetProduct(id);
//Fill page with data
lblTitle.Text = product.Name;
lblDescription.Text = product.Description;
lblPrice.Text = "Price per unit:<br/>£ " + product.Price;
imgProduct.ImageUrl = "~/Images/Products/" + product.Image;
lblItemNr.Text = product.ID.ToString();
//Fill amount list with numbers 1-20
int[] amount = Enumerable.Range(1, 20).ToArray();
ddlAmount.DataSource = amount;
ddlAmount.AppendDataBoundItems = true;
ddlAmount.DataBind();
}
}
}
使用imageButton.AlternateText
属性。