开发者

How to access ImageUrl property of the Image Control placed inside a ItemTemplate in the 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_Upld开发者_StackOverflow中文版Fl" runat="server" />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
</asp:GridView>

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;
        }
     } 

..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'.

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;

*The AppSettings returns the path for the folder that I use for uploading.

*currentCruise is an object and it's properties was assigned through my DAL layer.


I think I understand what you're trying to do...

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
    }
}

Hope this helps!

More info here:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx


If you want to set the Image right from the start:

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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜