如何显示在GridView基于SelectedValue的图像? [英] How to display an image based on SelectedValue in a GridView?
问题描述
我已经建立了一些后端code检索SQL Server数据库中的图像。我想根据GridView控件的均流选定值我的Web表单上显示此图像。
I have built some back-end code to retrieve an image from a SQL Server database. I want to display this image on my web form based on the currenct selected value of a gridview control.
我有一个名为GetImage.aspx网页,其中有两个查询字符串参数,ENTITYID和的EntityType。 ENTITYID是GridView.SelectedValue和的EntityType简直是T。我的code为GetImage.aspx看起来是这样的:
I have a page called GetImage.aspx which takes two querystring parameters, entityId and entityType. entityId is the GridView.SelectedValue and entityType is simply "T". My code for GetImage.aspx looks like this:
protected void Page_Load (Object server, EventArgs e)
{
if ((Request.QueryString["entityId"] != null) && (Request.QueryString["entityType"] != null))
{
int entityId = int.Parse(Request.QueryString["entityId"]);
string entityType = Request.QueryString["entityType"];
DBUtil DB = new DBUtil();
DataTable dt = DB.GetScreenshot(entityId, entityType);
if (dt != null)
{
Byte[] bytes = (Byte[])dt.Rows[0]["screenshot"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = dt.Rows[0]["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename="+ dt.Rows[0]["fileName"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}
什么ASP.NET code,我需要我的呼吁表单上显示这个?
What ASP.NET code do I need to display this on my calling form?
感谢。
修改
我加入这个code到我的形式(我不是要在GridView显示图像,只是我在GridView下表):
I have added this code to my form (I'm not trying to display the image IN the GridView, just on my form under the GridView):
<img id="tradeScreenshot" runat="server" alt="screenshot" />
和添加了这个$ C $ C:
and added this code:
protected void grdTrades_SelectedIndexChanged(object sender, EventArgs e)
{
string tradeId = grdTrades.SelectedDataKey.Value.ToString();
tradeScreenshot.Src = "GetImage.aspx?entityId=" + tradeId + "&entityType=T";
}
不显示图像,但是我可以看到正确的ID在数据库服务器上执行,当我选择在GridView项目的存储过程,所以这部分很努力,但图像的显示不。我缺少什么?
The image is not displayed, however I can see the stored procedure being executed on the database server with the correct ID when I select an item in the GridView, so that part is working, but the display of the image isn't. What am I missing?
推荐答案
取决于你显示什么样的GridView和行如何选择。格不具有的SelectedValue - 它已选定的项目,其可具有多个数据键与它
Depends on what you show in gridview, and how rows are selected. Grid doesn't have selectedvalue - it has selected item, which can have multiple data keys with it.
ASP.NET:
<img src="" runat="server" id="selectedimage" />
<asp:GridView OnSelectedIndexChanged="ChangeItem"
DataKeyNames="ImageId"
runat="server" id="MyGrid"></asp:GridView>
C#:
public void ChangeItem(object o,EventArgs e){
string ImageId = MyGrid.SelectedDataKey.Value.ToString();
selectedimage.Src = "GetImage.aspx?entityId="+ImageId+"&entityType=T";
}
这篇关于如何显示在GridView基于SelectedValue的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!