簡體   English   中英

根據下拉選擇在gridview中顯示相同圖像

[英]Displaying Same images in gridview Based on dropdown selection

我在公用字段中有菜單表和產品表以及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.jpgimage1.png

通過這種方法,您可以在gridview中顯示圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM