且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET中的Gridview中将多个URL传递给Imageurl

更新时间:2023-02-16 13:44:39

<ItemTemplate>
                            <asp:Image ID="Image2" runat="server" Width="100" ImageUrl='<%# "DisplayAllItems.aspx?id="+Eval("ProductID")+" %>' />
                            <asp:Image ID="Image1" runat="server" Width="100" ImageUrl='<%# "DisplayAllProducts.aspx?id="+Eval("ProductID")%>' />
                        </ItemTemplate>









in .aspx





OR

in .aspx

<asp:TemplateField HeaderText="Product Image">
                        <ItemTemplate>
                            <asp:Panel ID="pnlImages" runat="server">
                            </asp:Panel>
                        </ItemTemplate>
                    </asp:TemplateField>





in aspx.cs



protected void grdCart_RowDataBound(对象发送者,GridViewRowEventArgs e)

{

if(e.Row.RowType == DataControlRowType.DataRow)

{

面板pnl =(面板)e.Row.FindControl(pnlImages);



图片img =新图片();

img。 ImageUrl =〜/ Image / ball_grey.png;

pnl.Controls.Add(img);



img = new Image( );

img.ImageUrl =ImageHandler.ashx?image = Image / ball_silver.png;

pnl.Controls.Add(img);

}

}



in aspx.cs

protected void grdCart_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Panel pnl = (Panel)e.Row.FindControl("pnlImages");

Image img = new Image();
img.ImageUrl = "~/Image/ball_grey.png";
pnl.Controls.Add(img);

img = new Image();
img.ImageUrl = "ImageHandler.ashx?image=Image/ball_silver.png";
pnl.Controls.Add(img);
}
}


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID">
            <Columns>
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" />
                <asp:BoundField DataField="ProductDescription" HeaderText="ProductDescription" />
                <asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" />
                <asp:TemplateField HeaderText="Image">
                    <ItemTemplate>
                        <img src="GridViewImage123.ashx?autoid=<%# Eval("Id").ToString() %>&tableName=<%# Eval("TableName").ToString() %>"

                            width="150" height="100" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>










While combining all the data, add one more column table name also
i.e in

string qry1 = "Select ProductID,ProductName,ProductDescription,ProductPrice,ProductImage,'tblImage' as TableName from tblImage";
string qry2 = "Select ProductID,ProductName,ProductDescription,ProductPrice,ProductImage,'tblRatnams' as TableName from tblRatnams";
string qry3 = "Select ProductID,ProductName,ProductDescription,ProductPrice,ProductImage,'tblRudrakshas' as TableName from tblRudrakshas";










//Send the TableName to ashx
public class GridViewImage123 : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.QueryString["autoId"] == null) return;

        string constr = "Data Source=.\\SQLEXPRESS;AttachDbFILEName=|DataDirectory|\\ImageDB.mdf;integrated security=true;user instance=true;";

        string productId = context.Request.QueryString["productId"];
        string tableName = context.Request.QueryString["tableName"];

        using (SqlConnection conn = new SqlConnection(constr))
        {
            string query = "SELECT ProductImage FROM " + tableName + " WHERE ProductID =" + productId;

            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
                {
                    reader.Read();
                    context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("ProductImage")]);
                    reader.Close();
                }
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}