Go to: display images in the DataGrid

Source: Internet
Author: User

PreviousArticleThe following example shows how to save images to the database. Previously, response. binarywrite () is used to display only one image. Now, a DataGrid control is used to display the image in the DataGrid. (Note: The paging function is not provided in the following example)
Two image controls are used. One is the imagebutton on the server side and the other is the image control on the client side.

Front-end Code :

<% @ Page Language = "C #" codebehind = "datagridimage. aspx. cs" autoeventwireup = "false" inherits = "nettest. datagridimage" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> datagridimage </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Table align = "center" border = "1" width = "60%">
<Tbody>
<Tr>
<TD align = "center"> <font style = "font-weight: bold; font-size: 11pt"> display pictures in the database in the DataGrid </font> </TD>
</Tr>
<Tr>
<TD>
<Asp: DataGrid id = "datagrid1" runat = "server" autogeneratecolumns = "false" width = "100%">
<Columns>
<Asp: templatecolumn>
<Itemtemplate>
<Font face = "">
<Table id = "Table1" cellspacing = "1" cellpadding = "1" width = "100%" border = "0">
<Tr>
<TD colspan = "2"> <font style = "font-size: 10pt"> image name: </font> <font style = "font-size: 10pt">
<Asp: Label id = "lbimagename" runat = "server" text = '<% # databinder. eval (container, "dataitem. imagename") %>'>
</ASP: Label> </font> </TD>
</Tr>
<Tr>
<TD> <font style = "font-size: 10pt"> image: </font>
<Asp: Image id = imagebutton1 width = 100 Height = 100 runat = "server" imageurl = '<% # "ReadImage. aspx? Imageid = "+ databinder. eval (container," dataitem. ID ") %> '>
</ASP: Image>
</TD>
<TD>
'">
</TD>
</Tr>
</Table>
</Font>
</Itemtemplate>
</ASP: templatecolumn>
</Columns>
</ASP: DataGrid> </TD>
</Tr>
</Tbody>
</Table>
</Form>
</Body>
</Html>

 

Background code:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;

Namespace nettest
{
/// <Summary>
/// Summary of the datagridimage.
/// </Summary>
Public class datagridimage: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
Private string STR = "Server = localhost; uid = sa; Pwd =; database = northwind ";
Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
{
Filldata ();
}
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void filldata () // bind the DataGrid
{
Sqlconnection Cn = new sqlconnection (STR );
Sqlcommand cmd = new sqlcommand ();
Cmd. commandtext = "select * From upimage ";
Cmd. Connection = cn;
CN. open ();
Sqldataadapter da = new sqldataadapter ();
Da. selectcommand = cmd;
Dataset DS = new dataset ();
Da. Fill (DS, "Images ");
Datagrid1.datasource = Ds. Tables ["Images"];
Datagrid1.databind ();
CN. Close ();
}
}
}

ReadImage. aspx (obtain the image based on the number of the uploaded image. You only need to create a page without any controls)
Background code:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Namespace nettest
{
/// <Summary>
/// ReadImage summary.
/// </Summary>
Public class ReadImage: system. Web. UI. Page
{

Private void page_load (Object sender, system. eventargs E)
{
String conn = "Server = localhost; uid = sa; Pwd =; database = northwind ";
Sqlconnection Cn = new sqlconnection (conn );
Sqlcommand cmd = new sqlcommand ();
Cmd. commandtext = "select image from upimage where id = '" + this. request ["imageid"] + "'";
Cmd. Connection = cn;
CN. open ();
This. response. contenttype = "image /*";
Sqldatareader DR = cmd. executereader ();
While (dr. Read ())
{
This. response. binarywrite (byte []) Dr ["image"]);
}
CN. Close ();
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

}
}

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.