在GridView中查找图像

本文关键字:图像 查找 GridView | 更新日期: 2023-09-27 18:13:48

我在GridView中有一个图像。我需要为它制作路径,并希望在后台代码(c#)中完成。我找不到gvImage。我是否使用GridView1。FindControl之类的?

我想在c#中找到它

标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Competitor_ID" DataSourceID="AccessDataSource1" EmptyDataText="There are no data records to display.">
    <Columns>
        <asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />
        <asp:BoundField DataField="Competitor_ID" HeaderText="Competitor_ID" InsertVisible="False" ReadOnly="True" SortExpression="Competitor_ID" />
        <asp:BoundField DataField="Competitor_Salutation" HeaderText="Competitor_Salutation" SortExpression="Competitor_Salutation" />
        <asp:BoundField DataField="Competitor_Name" HeaderText="Competitor_Name" SortExpression="Competitor_Name" />
        <asp:BoundField DataField="Competitor_DoB" HeaderText="Competitor_DoB" SortExpression="Competitor_DoB" />
        <asp:BoundField DataField="Competitor_Email" HeaderText="Competitor_Email" SortExpression="Competitor_Email" />
        <asp:BoundField DataField="Competitor_Description" HeaderText="Competitor_Description" SortExpression="Competitor_Description" />
        <asp:BoundField DataField="Competitor_Country" HeaderText="Competitor_Country" SortExpression="Competitor_Country" />
        <asp:BoundField DataField="Competitor_Gender" HeaderText="Competitor_Gender" SortExpression="Competitor_Gender" />
        <asp:BoundField DataField="Competitor_ContactNo" HeaderText="Competitor_ContactNo" SortExpression="Competitor_ContactNo" />
        <asp:BoundField DataField="Competitor_Website" HeaderText="Competitor_Website" SortExpression="Competitor_Website" />
        <asp:BoundField DataField="Competitor_Photo" HeaderText="Competitor_Photo" SortExpression="Competitor_Photo" /> 
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Image ID="gvImage" runat="server" Height="122px" Width="148px" />   
            </ItemTemplate>
        </asp:TemplateField>       
    </Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/UserDatabase.accdb" DeleteCommand="DELETE FROM `Competitors` WHERE `Competitor_ID` = ?" InsertCommand="INSERT INTO `Competitors` (`Competitor_ID`, `Competitor_Salutation`, `Competitor_Name`, `Competitor_DoB`, `Competitor_Email`, `Competitor_Description`, `Competitor_Country`, `Competitor_Gender`, `Competitor_ContactNo`, `Competitor_Website`, `Competitor_Photo`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" SelectCommand="SELECT `Competitor_ID`, `Competitor_Salutation`, `Competitor_Name`, `Competitor_DoB`, `Competitor_Email`, `Competitor_Description`, `Competitor_Country`, `Competitor_Gender`, `Competitor_ContactNo`, `Competitor_Website`, `Competitor_Photo` FROM `Competitors`" UpdateCommand="UPDATE `Competitors` SET `Competitor_Salutation` = ?, `Competitor_Name` = ?, `Competitor_DoB` = ?, `Competitor_Email` = ?, `Competitor_Description` = ?, `Competitor_Country` = ?, `Competitor_Gender` = ?, `Competitor_ContactNo` = ?, `Competitor_Website` = ?, `Competitor_Photo` = ? WHERE `Competitor_ID` = ?">

编辑:尝试这个代码来获取图像的URL,但我不认为我得到任何东西。

protected void Page_Load(object sender, EventArgs e)
{
    string imgPath = Path.Combine("~/UploadedFiles/", (GridView1.FindControl("gvImage")).ImageUrl); 
}

出现'Control'不包含'ImageUrl'的定义,并且没有'ImageUrl'的扩展方法接受'Control'类型的第一个参数

在GridView中查找图像

由于Image位于重复数据的控件中,因此必须指定行号。gvImage只存在于GridView1中

Image image = GridView1.Rows[i].FindControl("gvImage") as Image;
string imgPath = image.ImageUrl;