如何访问放在里面的ItemTemplate在GridView图像控件的ImageUrl属性 [英] How to access ImageUrl property of the Image Control placed inside a ItemTemplate in the GridView
问题描述
我的GridView的标记:
My GridView Markup:
<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中取消和/更新后打电话等:
My Bind Method that I call from Page_Load and after Cancelling/Updating, etc:
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;
}
}
..现在,当你看到我有一个模板领域,我访问我编辑的行中EditTemplate由文件上传控制的FindControl()。但我怎么能进入图像控件的属性的ImageUrl。
..Now as you see I have a template field, I access the 'FileUpload' control in the EditTemplate by 'FindControl()' of the row that I'm editing. but how can I access the 'Image' control's property 'ImageUrl'.
我需要将其设置为类似于以下,这是在文件后面code另一个项目的样本code,但我能够直接访问图像。
I need to set it to something like the following, this is a sample code from another project in the code behind file but I was able to access the image directly.
currentProfilePic_Img.ImageUrl = ConfigurationManager.AppSettings["cruisesPpUploadPath"].ToString() + currentCruise.ProfilePic;
*本的AppSettings返回,我使用的上传文件夹的路径。
*The AppSettings returns the path for the folder that I use for uploading.
* currentCruise是一个对象,它的属性是通过我的DAL层分配。
*currentCruise is an object and it's properties was assigned through my DAL layer.
推荐答案
我想我明白你想怎么办...
I think I understand what you're trying to do...
如果您希望将图像控制URL动态绑定,你必须勾到在GridView的RowDataBound事件。
If you want to bind the image control URL dynamically, you will have to hook into the RowDataBound event of the GridView.
<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
}
}
希望这有助于!
在此处了解详情:
<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx
这篇关于如何访问放在里面的ItemTemplate在GridView图像控件的ImageUrl属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!