ASP. NET gridview display image [reprint]

Source: Internet
Author: User

The life of study is nothing more than a miserable reminder, and I regret it. Don't sigh, because I have helped my friends write something about it, so I have reprinted others' code, learned, and learned, no matter how I learn it in the future. Do not talk nonsense, on the code.

 

Display images in the gridview

<1> Add an imagefield and bind itData Source.
<2>Edit TemplateTo add a column with an image.

Two methods are described as follows:

<1> Add a gridview to the page, click its task menu "Edit column", select imagefield in the pop-up edit box, click Add, and select the column you just added, the right panel displays its attributes. In the dataimageurlfield attribute, enter the column name in the data table.

Code:
<Asp: gridview id = "gridview1" runat = "server">
<Columns>
<Asp: imagefield dataimageurlfield = "IMG">
</ASP: imagefield>
</Columns>
</ASP: gridview>

Then edit the background code:
Datatable dt = new datatable ();
Datacolumn Dc = new datacolumn ();
DT. Columns. Add (DC );
Dc = new datacolumn ("IMG ");
DT. Columns. Add (DC );

Datarow DR = DT. newrow ();
Dr [0] = "11111111111111111 ";
Dr [1] = resolveurl ("~ /OK .gif ");
DT. Rows. Add (DR );

Dr = DT. newrow ();
Dr [0] = "22222222222222222 ";
Dr [1] = resolveurl ("~ /OK .gif ");
DT. Rows. Add (DR );

Gridview1.datasource = DT;
Gridview1.databind ();

 

<2> edit a template.
Edit the foreground code first:

<asp:GridView ID="GridView1" runat="server">            <Columns>                <asp:TemplateField>                    <ItemTemplate>                       <asp:Image ID="img1" ImageUrl='<%#Eval("img") %>'                                   runat="server" AlternateText="image lost" />                    </ItemTemplate>                </asp:TemplateField>            </Columns>        </asp:GridView>

Similar to the first method, the final implementation mechanism is to retrieve the column name of the data source. Therefore, in Eval, the column name containing the image URL is given in the form of a string, and the background code remains unchanged. the final effect is the same as that of the first method.

The wooden knife believes that: in the current discussion, the first implementation method seems better, but the xxfield control is limited after all. If you want to put other controls in it, you have to use the template, therefore, the second method is more general and more powerful.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.