动态更新asp.net图像控件 [英] Updating asp.net Image control dynamically

查看:173
本文介绍了动态更新asp.net图像控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好帮手,

我有一个we gridview,其中包含有关驱动程序的一些数据.选择后,我可以成功地在列中获取一些数据.
我在文件夹中也有驱动程序映像,并且还能够从datagrid中的值成功构造有效的 imageurl
在文件夹中选择图像.

例如,网格视图包含一个名为1的id字段,因此当用户选择该行时,我可以获取1并构造一个图像网址,例如

C:/webapp/images/1.jpg

最初,我使用ajax在图像控件中显示图像,但现在没有显示,因此,我尝试对页面刷新进行同样的操作,并且没有ajax,但它仍未显示在图像控件中.

请参见下面的一些代码...(不带ajax-updatePanel的版本)
请问我有什么遗漏吗?

至少如果此版本有效,我可能会知道如何Ajax启用它

感谢您的回复

Hello helpers,

I have a we gridview that contains some data on drivers. I am successfully able to get some data in a column when selected.
I also have driver images in a folder and am also able to successfully construct a valid imageurl from a value in the datagrid
to pick an image in the folder.

e.g grid view contains an id field called 1 so when user selects that row, I am able to get 1 and construct an imageurl like

C:/webapp/images/1.jpg

Initially I used ajax to display the image in an image control but it was not showing so now, I have being trying to do same with page refreshes and no ajax but it still does not display in the image control.

Please see some codes below...(version without ajax-updatePanel)
Please am i missing something ?

At least if this version works I may know how to Ajax enable it

Thanks for response

protected void Page_Load(object sender, EventArgs e)
      {
          //populate gridview with drivers data

          var dg=new dalConn();
          dgdrivers.DataSource = dg.Fromdbase("select * from getdrivers");
          dgdrivers.DataBind();

          dvimage.ImageUrl = lblfullname.Text;
      }

      protected void dgdrivers_SelectedIndexChanged(object sender, EventArgs e)
      {

         lblfullname.Text = Server.MapPath("/dImages/" + dgdrivers.SelectedRow.Cells[1].Text.ToString() + ".jpg");

      }

推荐答案

首先,当您当时需要将数据绑定到图像控件时,需要使用虚拟路径而不是实际路径进行绑定

如果您的项目位于c:上,并且在项目内部有一个名为"dImages"的文件夹,它将包含要显示的图像,那么您需要编写图像网址,例如

First of all when you need to bind your data to image control at that time you need to bind it with using virtual path not actual path

if your project is located to c: and inside your project there is a folder named "dImages" and it will contain images to display then you need to write your image url like

lblfullname.Text = "~/dImages/" + dgdrivers.SelectedRow.Cells[1].Text.ToString() + ".jpg"



在您的代码中,您使用"Server.MapPath",我认为使用此方法时不需要获取.
当您实际访问驱动器(例如C:,D:等)或需要访问文件的物理路径时,将使用Server.MapPath.

要显示图像,您需要使用虚拟路径以〜/....."或"../...."开头

或生成图像网址后,请尝试此操作..

图像控件在网格视图的内部还是外部.


如果在网格视图中,则需要在databoud事件(例如项数据绑定或行数据绑定)中编写代码,
在那种情况下,您需要动态地投射控件并分配ImageUrl
像这样



inside your code you use "Server.MapPath", i think you need not get when this method is used..

Server.MapPath is used when you actually access your drives like C:,D: etc or you need to access physical path of your files.

for displaying images you need to use virtual path starts with "~/....." or "../...."

or after genrating image url try this..

Where is your Image Control Located inside grid view or out side grid view..


If it inside grid view then you need to write your code inside a databoud event like item data boud or row data bound,
at that event you need to cast your control dynamically and assign the ImageUrl
Like this

((Image)GridView1.Rows[e.Row.RowIndex].FindControl("YOUIMAGECONTROLID")).ImageUrl = "";



如果在外面,则可以直接将图像URL分配给图像控件.

喜欢..



and if it out side then directly you can assign image URL to your image control..

Like..

YouIMAGEID.ImageUrl = "";



甚至即使图像未显示,也可以在动态分配图像URL之后,触发控件的"DataBind()"方法,如下所示:.



or even if the images is not displayed then after assigning imge url dynamically then fire "DataBind()" method of your control like this..

YOURIMAGEID.DataBind();


这篇关于动态更新asp.net图像控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆