在gridview中显示相同的图像基于下拉选择

时间:2014-12-23 09:23:57

标签: c# asp.net

我有菜单表和产品表以及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;
    }
}

                                  
                                               我做错了什么,请帮助我

1 个答案:

答案 0 :(得分:0)

在aspx中设置图片网址, 像这样的东西

<ItemTemplate> 
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "your_path" + "ProductImage" %>'  
   Height="300px" Width="300px"/>
</ItemTemplate>

请确保your_path是存储图片的物理路径,例如"~/images/myimg"并且您的图片名称有效,例如image.jpgimage1.png

通过这种方法,您可以在gridview中显示图像。