Image 强制转换 C# "img"+i就是string,怎么能这么转换呢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你应该吧图片放另外页面引用,而不是直接给图片URL赋值 可以把"img"+i强制转换为动态的用户控件的ID呀!只是我写不出这个方法,所以向大家请教! (Image)("img" + i); //这是字符串,怎么能转换成image 加入两个Image控件,名称分别为Image1、Image2,分别指定ImageUrl属性;加入一个TextBox控件,名称为txtImageUrl;加入一个按钮,名称为getImageButton,在其Click事件中写入下面代码:int NodeCount=2;for (int i = 1; i <= NodeCount; i++){ System.Web.UI.WebControls.Image image=(System.Web.UI.WebControls.Image)this.FindControl("Image"+i.ToString()); string imageUrl=image.ImageUrl; this.txtImageUrl.Text=this.txtImageUrl.Text.Trim().ToString()+"第"+i.ToString()+"个图片地址为"+imageUrl;}运行后TextBox控件将得到你设置的两个地址 根据名称返回控件,在WebForm中没有FindControl方法。在WinForm中,我用递归写了一个类似WebForm中FindControl的方法:/// <summary> /// 根据控件名找出容器中的控件,考虑有些控件放在窗体的容器中,采用了递归查找。 /// </summary> /// <param name="container">控件容器</param> /// <param name="controlName">控件名称</param> /// <returns></returns> private Control FindControl(Control container, string controlName) { Control findControl = null; foreach(Control control in container.Controls) { if (control.Controls.Count == 0) { if (control.Name == controlName) { findControl = control; break; } } else { findControl = FindControl(control, controlName); } } return findControl; }==================================调用这个方法:Image img = (Image)FindControl(this,("img" + i));就可以正常使用了。 根据名称返回控件,在WebForm中没有FindControl方法。上面有错误,应该是在WinForm中没有FindControl方法。 楼上说的对,不过楼主似乎是WebForm WebForm更简单,本身自带FindControl方法。直接: Image img = (Image)this.FindControl("img" + i); 怎样在Dundas图表控件中实现柱形图的值从负值开始从小到大显示,急!! dataset能否根据字段名来访问? 关于Audio播放器的问题? ODBC驱动不支持insert命令吗? 自己定义的类是托管的吗? 手动打开excel文件,再运行程序,文件关闭进程也释放不掉 请版竹过来看看我怎么惹他了 如何卸载注册表中我注册的.net组件 告诉问题,给现金100(关于flash控件背景透明问题) 如何将字符数组转换成字节数组 熬夜答题(英文),关于delegate和异步调用的问题,请高手进来帮忙看下 100分请求解决线程堵塞、不能中断的问题
加入一个TextBox控件,名称为txtImageUrl;
加入一个按钮,名称为getImageButton,在其Click事件中写入下面代码:
int NodeCount=2;
for (int i = 1; i <= NodeCount; i++)
{
System.Web.UI.WebControls.Image image=(System.Web.UI.WebControls.Image)this.FindControl("Image"+i.ToString());
string imageUrl=image.ImageUrl;
this.txtImageUrl.Text=this.txtImageUrl.Text.Trim().ToString()+"第"+i.ToString()+"个图片地址为"+imageUrl;
}
运行后TextBox控件将得到你设置的两个地址
在WinForm中,我用递归写了一个类似WebForm中FindControl的方法:
/// <summary>
/// 根据控件名找出容器中的控件,考虑有些控件放在窗体的容器中,采用了递归查找。
/// </summary>
/// <param name="container">控件容器</param>
/// <param name="controlName">控件名称</param>
/// <returns></returns>
private Control FindControl(Control container, string controlName)
{
Control findControl = null;
foreach(Control control in container.Controls)
{
if (control.Controls.Count == 0)
{
if (control.Name == controlName)
{
findControl = control;
break;
}
}
else
{
findControl = FindControl(control, controlName);
}
}
return findControl;
}
==================================
调用这个方法:
Image img = (Image)FindControl(this,("img" + i));
就可以正常使用了。
直接: Image img = (Image)this.FindControl("img" + i);