在网格视图中显示相同的图像 基于下拉选择
本文关键字:图像 于下拉 选择 网格 视图 显示 | 更新日期: 2023-09-27 18:35:01
我在公共字段中有菜单表和产品表以及 MenuId例
菜单表
MenuId MenuName
11 Shirts
12 Tshirts
产品表
ProductId ProductName MenuId ProductImage
1 Levisshirts 11 image
2 white shirt 11 image2
根据下拉选择在 GirdView 中显示图像,但问题是它为每个产品显示相同的图像,我的代码如下
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
if (!IsPostBack)
ddlbind();
}
private void BindGridData()
{
SqlCommand command = new SqlCommand("SELECT * from rsa_ProductItemTable where MenuId=" + Dropsearch.SelectedItem.Value, con);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataSet ds = new DataSet();
daimages.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.Attributes.Add("bordercolor", "black");
}
public void ddlbind()
{
SqlCommand command = new SqlCommand("SELECT * from rsa_mastermenu", con);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
Dropsearch.DataSource = dt;
Dropsearch.DataTextField = "MenuName";
Dropsearch.DataValueField = "MenuId";
Dropsearch.DataBind();
Dropsearch.Items.Insert(0, new ListItem("Select", "0"));
}
protected void Dropsearch_SelectedIndexChanged(object sender, EventArgs e)
{
int imgid = int.Parse(Dropsearch.SelectedItem.Value);
BindGridData();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Image img = (Image)e.Row.FindControl("Image1");
img.ImageUrl = "GridviewImage.ashx?ImID=" + Dropsearch.SelectedItem.Value;
}
}
我做错了什么,请帮我解决这个问题
在 aspx 中设置图像网址,像这样的东西
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "your_path" + "ProductImage" %>'
Height="300px" Width="300px"/>
</ItemTemplate>
请确保your_path是存储图像的物理路径,例如"~/images/myimg"
并且映像名称有效,例如image.jpg
或image1.png
通过此方法,您可以在网格视图中显示图像。