C#asp.net alt标签问题

时间:2015-01-16 22:28:59

标签: c# asp.net html5

大家好,我是网站的新手,并且在asp.net中有一个与c#有关的问题。我已经在asp.net中使用HTML5标记和CSS创建了一个网站,其中包含c#代码。我遇到的问题是我在一个带有c#代码的index.aspx页面后面显示我在面板中的所有产品,这些产品使用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();
        }
        }

    }

1 个答案:

答案 0 :(得分:1)

使用imageButton.AlternateText属性。