我正在忙於一個項目,並且我試圖在單擊按鈕時更改圖像。問題是我無法在代碼部分調用圖像控件。我怎樣才能調用圖像控件?無法訪問在HTML中定義的圖像控件,在C#文件後面的代碼中
我曾嘗試將控件添加到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>
誰下投這個問題,應該是** **慚愧。這不是這樣,新用戶受到歡迎。至少這個問題是有道理的。 – Zeeshan
@ user3636129如果圖像不可用,程序不會崩潰, –