我的GridView标记:
<asp:GridView ID="GrdVw" visible="False" runat="server" AllowPaging="True"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataFi
eld="Comment" HeaderText="Comment" />
<asp:TemplateField HeaderText="Review Document">
<ItemTemplate>
<asp:Image ID="currentDocFile" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="reviewDoc_UpldFl" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
我从Page_Load和取消/更新后调用的绑定方法等:
private void BindGrdVw()
{
List<ArticleComments> commentsList = ArticleCommentsBLL.GetComments(ArticleID);
if (cruiseReviewsList.Count != 0)
{
GrdVw.DataSource = commentsList;
GrdVw.DataKeyNames = new string[] { "ID" };
GrdVw.DataBind();
GrdVw.Visible = true;
}
}
..现在你看到我有一个模板字段,我通过我正在编辑的行的'FindControl()'访问EditTemplate中的'FileUpload'控件。但是如何访问'Image'控件的属性'ImageUrl'。
我需要将它设置为类似下面的内容,这是代码隐藏文件中另一个项目的示例代码,但我能够直接访问该图像。
currentProfilePic_Img.ImageUrl = ConfigurationManager.AppSettings["cruisesPpUploadPath"].ToString() + currentCruise.ProfilePic;
* AppSettings返回我用于上传的文件夹的路径。
* currentCruise是一个对象,它的属性是通过我的DAL层分配的。
答案 0 :(得分:2)
我想我明白你要做什么......
如果要动态绑定图像控件URL,则必须挂钩到GridView的RowDataBound事件。
<asp:GridView ID="GrdVw" visible="False" runat="server" AllowPaging="True"
AutoGenerateColumns="False" OnRowDataBound="GrdVwDataBound">
protected virtual void GrdVwDataBound(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var imageControl = e.Row.FindControl("currentDocFile") as Image;
imageControl.ImageUrl = // Image URL here
}
}
希望这有帮助!
更多信息:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx
答案 1 :(得分:1)
如果您想从头开始设置图像:
protected void Page_Load(object sender, EventArgs e)
{
GrdVw.RowDataBound += new GridViewRowEventHandler(GrdVw_RowDataBound);
}
void GrdVw_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Image rowImage = (Image) e.Row.FindControl("currentDocFile");
rowImage.ImageUrl = whatever;
}
}