将图像从Sql数据库获取到图像控件的问题 [英] Problems getting an image from Sql database into image control
问题描述
<表>
< tr>< td class =AddArticleLabel>文章名称:< / td>
< td class =AddArticleTextbox>< asp:TextBox ID =txtNameclass =AddArticleTextboxrunat =server/>< / td>
< / tr>
< tr>< td class =AddArticleLabel>文章标题:< / td>
< td class =AddArticleTextbox>< asp:TextBox ID =txtTitleclass =AddArticleTextboxrunat =server/>< / td>
< / tr>
< tr>
< td>< asp:图片ID =Image1runat =server
ImageUrl ='<%#ResolveUrl(〜/ Uploads /+ Eval(Thumbnail)) %>'/>< / td>
< / tr>
< tr>< td class =AddArticleLabel>文章副本:< / td>
< td class =CopyTextbox>< asp:TextBox ID =txtCopyheight =100pxwidth =300pxrunat =server/>< / td>
< / tr>
< / table>
我的代码背后:
protected void Page_Load(object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
string ArticleID =;
if(Request.QueryString [ArticleID]!= null)
{
ArticleID = Request.QueryString [ArticleID];
if(Request.QueryString [delete] ==yes)
{
ArticleDataAccessLayer adal = new ArticleDataAccessLayer();
adal.DeleteArticle(ArticleID);
Response.Redirect(AllArticles.aspx);
}
文章a = new ArticleDataAccessLayer()。GetArticle(ArticleID);
txtName.Text = a.Name;
txtTitle.Text = a.Title;
txtCopy.Text = a.Copy;
//我需要这样的东西吗?我试过了,虽然它没有用。
Image1.ImageUrl = a.Thumbnail;
hfArticleID.Value = a.ArticleID.ToString();
}
}
}
如果图像来自数据库,那么您需要一个HTTP处理程序来查找图像字节并返回它们以响应图像请求。如果您的文件系统上有图像,则需要将URL作为ImageUrl传递到Web服务器文件系统中的图像。两种方式存储都没有任何意义,我可以看到。将URL路径存储在DB中。
Hi guys. Wondering if anyone can offer any ideas on what I may be doing wrong. I have a sql database that contains images as image files (image.jpg). I also have these same images stored in my application in an Uploads folder. I'm trying to retrieve the image from the db depending on the id the user selects in the previous page and show it in the image control in a table, but I can't get it working. The other fields in the table come back fine (txtName, txtTitle, txtCopy). Just the image isn't working. What I'm not sure about is if I need something in the code behind as well as in the control itself to bring the image in. Any help is appreciated.
<table> <tr><td class="AddArticleLabel">Article name: </td> <td class="AddArticleTextbox"><asp:TextBox ID="txtName" class="AddArticleTextbox" runat="server" /></td> </tr> <tr><td class="AddArticleLabel">Article title: </td> <td class="AddArticleTextbox"><asp:TextBox ID="txtTitle" class="AddArticleTextbox" runat="server" /></td> </tr> <tr> <td><asp:Image ID="Image1" runat="server" ImageUrl='<%#ResolveUrl("~/Uploads/"+Eval("Thumbnail")) %>' /></td> </tr> <tr><td class="AddArticleLabel">Article copy: </td> <td class="CopyTextbox"><asp:TextBox ID="txtCopy" height="100px" width="300px" runat="server" /></td> </tr> </table>
And my code behind:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string ArticleID = ""; if (Request.QueryString["ArticleID"] != null) { ArticleID = Request.QueryString["ArticleID"]; if (Request.QueryString["delete"] == "yes") { ArticleDataAccessLayer adal = new ArticleDataAccessLayer(); adal.DeleteArticle(ArticleID); Response.Redirect("AllArticles.aspx"); } Articles a = new ArticleDataAccessLayer().GetArticle(ArticleID); txtName.Text = a.Name; txtTitle.Text = a.Title; txtCopy.Text = a.Copy; // Do I need something like this? I've tried and it didn't work though. Image1.ImageUrl = a.Thumbnail; hfArticleID.Value = a.ArticleID.ToString(); } } }
If the image comes from the DB, then you need a HTTP handler to look up the image bytes and return them in response to the image request. If you have the image on your file system, then you need to pass a URL to the image in your web server's file system as the ImageUrl. Storing it both ways makes no sense that I can see. Store the URL path in the DB.
这篇关于将图像从Sql数据库获取到图像控件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!