ImageField 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 ImageField 类的新实例。
public:
ImageField();
public ImageField ();
Public Sub New ()
示例
以下示例演示如何使用此构造函数将对象Columns动态添加到ImageField控件的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>
注解
使用此构造函数初始化类的新实例 ImageField 。 将字段添加到动态创建的数据绑定控件时,通常使用此构造函数。
若要将对象动态添加到 ImageField 数据绑定控件,请创建新 ImageField 对象,设置其属性,然后将其添加到数据绑定控件的字段集合。 例如,如果使用控件 GridView ,请将 ImageField 对象添加到 Columns 集合。
备注
尽管可以动态向数据绑定控件添加字段,但强烈建议静态声明字段,然后根据需要显示或隐藏字段。 静态声明所有字段可减小父数据绑定控件的视图状态大小。