不能访问在HTML中定义的图像控件,在c#文件后面的代码中
本文关键字:文件 代码 图像 访问 HTML 定义 不能 控件 | 更新日期: 2023-09-27 18:03:21
我正忙于一个项目,我试图得到一个图像改变时,一个按钮被点击。问题是我不能在代码部分调用图像控件。我如何调用图像控件?
我已经尝试将控件添加到designer.cs文件,但我无法访问UI控件,我也尝试了许多其他方法,但它没有解决问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using INF3014F_BMW_TEAM16.Models;
using System.Web.ModelBinding;
namespace INF3014F_BMW_TEAM16
{
public partial class ProductDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public IQueryable<Product> GetProduct(
[QueryString("ProductID")] int? productId, [RouteData] string productName)
{
var _db = new INF3014F_BMW_TEAM16.Models.ProductContext();
IQueryable<Product> query = _db.Products;
if (productId.HasValue && productId > 0)
{
query = query.Where(p => p.ProductID == productId);
}
else if (!String.IsNullOrEmpty(productName))
{
query = query.Where(p =>
String.Compare(p.ProductName, productName) == 0);
}
else
{
query = null;
}
return query;
}
protected void RedBtn_Click(object sender, EventArgs e)
{
Image1.ImageUrl = "/Catalog/Images/1hacth3red.jpg";
}
<asp:FormView ID="productDetail" runat="server" ItemType="INF3014F_BMW_TEAM16.Models.Product" SelectMethod ="GetProduct" RenderOuterTable="false" OnPageIndexChanging="productDetail_PageIndexChanging">
<ItemTemplate>
<div>
<h1><%#:Item.ProductName %></h1>
</div>
<br />
<table>
<tr>
<td>
<Image id="Image1" src="/Catalog/Images/<%#:Item.ImagePatheWhite %>" style="border: solid; height: 300px" alt="<%#:Item.ProductName %>" runat ="server" />
</td>
<td> </td>
<td style="vertical-align: top; text-align:left;">
<b>Description:</b><br /><%#:Item.Description %><br /><span>
<b>Price:</b> <%#: String.Format("{0:c}", Item.UnitPrice) %></span><br /><span>
<b>Product Number:</b> <%#:Item.ProductID %></span><br />
<br />
<asp:Button ID="WhiteBtn" runat="server" Text="White" BackColor ="White" OnClick ="WhiteBtn_Click" />
<<asp:Button ID="BlackBtn" runat="server" Text="Black" BackColor ="Black" />
<asp:Button ID="RedBtn" runat="server" Text="Red" BackColor ="Red" OnClick ="RedBtn_Click" />
<br />
<a href="/AddToCart.aspx?productID=<%#:Item.ProductID %>">
<span class="ProductListItem">
<b>Add To Cart<b>
</span>
</a>
<br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
用户服务器控件:
<asp:Image ID="Image1" runat="server" ImageUrl="/Catalog/Images/<%# Item.ImagePatheWhite %>" AlternateText="<%#:Item.ProductName %>" BorderColor="Black" Height="300px" />
<Image>
标签不存在,您必须使用标签并使用属性runat="server"
<img alt="" src="" />
我认为应该是:
<asp:Image id="Image1" runat="server" ... etc./>
设置ImageUrl服务器端