ImageField Konstruktor
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Initierar en ny instans av ImageField klassen.
public:
ImageField();
public ImageField();
Public Sub New ()
Exempel
I följande exempel visas hur du använder den här konstruktorn för att dynamiskt lägga till ett ImageField objekt i Columns en kontrollsamling GridView .
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
// Dynamically create a GridView control.
GridView employeesView = new GridView();
employeesView.ID = "EmployeesGrid";
employeesView.AutoGenerateColumns = false;
employeesView.DataSourceID = "EmployeeSource";
// Dynamically create field columns to display the desired
// fields from the data source.
// Create an ImageField object to display an employee's photo.
ImageField photoImageField = new ImageField();
photoImageField.DataImageUrlField = "PhotoPath";
photoImageField.AlternateText = "Employee Photo";
photoImageField.NullDisplayText = "No image on file.";
photoImageField.HeaderText = "Photo";
photoImageField.ReadOnly = true;
// Create a BoundField object to display an employee's last name.
BoundField lastNameBoundField = new BoundField();
lastNameBoundField.DataField = "LastName";
lastNameBoundField.HeaderText = "Last Name";
// Create a BoundField object to display an employee's first name.
BoundField firstNameBoundField = new BoundField();
firstNameBoundField.DataField = "FirstName";
firstNameBoundField.HeaderText = "First Name";
// Add the field columns to the Fields collection of the
// GridView control.
employeesView.Columns.Add(photoImageField);
employeesView.Columns.Add(lastNameBoundField);
employeesView.Columns.Add(firstNameBoundField);
// Add the GridView control to the Controls collection
// of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(employeesView);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ImageField Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ImageField Constructor Example</h3>
<asp:placeholder id="GridViewPlaceHolder"
runat="server"/>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="EmployeeSource"
selectcommand="Select [EmployeeID], [LastName], [FirstName], [PhotoPath] From [Employees]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
' Dynamically create a GridView control.
Dim employeesView As GridView = New GridView()
employeesView.ID = "EmployeesGrid"
employeesView.AutoGenerateColumns = False
employeesView.DataSourceID = "EmployeeSource"
' Dynamically create field columns to display the desired
' fields from the data source.
' Create an ImageField object to display an employee's photo.
Dim photoImageField As ImageField = New ImageField()
photoImageField.DataImageUrlField = "PhotoPath"
photoImageField.AlternateText = "Employee Photo"
photoImageField.NullDisplayText = "No image on file."
photoImageField.HeaderText = "Photo"
photoImageField.ReadOnly = True
' Create a BoundField object to display an employee's last name.
Dim lastNameBoundField As BoundField = New BoundField()
lastNameBoundField.DataField = "LastName"
lastNameBoundField.HeaderText = "Last Name"
' Create a BoundField object to display an employee's first name.
Dim firstNameBoundField As BoundField = New BoundField()
firstNameBoundField.DataField = "FirstName"
firstNameBoundField.HeaderText = "First Name"
' Add the field columns to the Fields collection of the
' GridView control.
employeesView.Columns.Add(photoImageField)
employeesView.Columns.Add(lastNameBoundField)
employeesView.Columns.Add(firstNameBoundField)
' Add the GridView control to the Controls collection
' of the PlaceHolder control.
GridViewPlaceHolder.Controls.Add(employeesView)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ImageField Constructor Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ImageField Constructor Example</h3>
<asp:placeholder id="GridViewPlaceHolder"
runat="server"/>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:sqldatasource id="EmployeeSource"
selectcommand="Select [EmployeeID], [LastName], [FirstName], [PhotoPath] From [Employees]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
Kommentarer
Använd den här konstruktorn för att initiera en ny instans av ImageField klassen. Den här konstruktorn används ofta när du lägger till fält i en dynamiskt skapad databunden kontroll.
Om du vill lägga till ett ImageField objekt dynamiskt i en databunden kontroll skapar du ett nytt ImageField objekt, anger dess egenskaper och lägger sedan till det i den databundna kontrollens fältsamling. Om du till exempel använder GridView kontrollen lägger du till objektet ImageField i Columns samlingen.
Note
Även om du dynamiskt kan lägga till fält i en databunden kontroll rekommenderar vi starkt att fälten deklareras statiskt och sedan visas eller döljs efter behov. Om du statiskt deklarerar alla fält minskar storleken på visningstillståndet för den överordnade databundna kontrollen.