繁体   English   中英

如何访问放置在GridView的ItemTemplate内的图像控件的ImageUrl属性

[英]How to access ImageUrl property of the Image Control placed inside a ItemTemplate in the GridView

我的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'控件。 但是如何访问“图像”控件的属性“ ImageUrl”。

我需要将其设置为如下所示,这是来自文件后面代码中另一个项目的示例代码,但是我可以直接访问该图像。

currentProfilePic_Img.ImageUrl = ConfigurationManager.AppSettings["cruisesPpUploadPath"].ToString() + currentCruise.ProfilePic;

* AppSettings返回我用于上传的文件夹的路径。

* currentCruise是一个对象,它的属性是通过我的DAL层分配的。

我想我知道您要做什么...

如果要动态绑定图像控件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

如果您想从一开始就设置图像:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM