DetailsViewInsertEventHandler Delegado
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Representa el método que controla el evento ItemInserting de un control DetailsView.
public delegate void DetailsViewInsertEventHandler(System::Object ^ sender, DetailsViewInsertEventArgs ^ e);
public delegate void DetailsViewInsertEventHandler(object sender, DetailsViewInsertEventArgs e);
type DetailsViewInsertEventHandler = delegate of obj * DetailsViewInsertEventArgs -> unit
Public Delegate Sub DetailsViewInsertEventHandler(sender As Object, e As DetailsViewInsertEventArgs)
Parámetros
- sender
- Object
Origen del evento.
Objeto DetailsViewInsertEventArgs que contiene los datos del evento.
Ejemplos
En el ejemplo de código siguiente se muestra cómo agregar mediante programación un DetailsViewInsertEventHandler delegado al ItemInserting evento de un DetailsView control .
<%@ 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)
{
// Create a new DetailsView object.
DetailsView customerDetailsView = new DetailsView();
// Set the DetailsView object's properties.
customerDetailsView.ID = "CustomerDetailsView";
customerDetailsView.DataSourceID = "DetailsViewSource";
customerDetailsView.AutoGenerateRows = true;
customerDetailsView.AutoGenerateInsertButton = true;
customerDetailsView.AllowPaging = true;
customerDetailsView.DataKeyNames = new String[1] { "CustomerID" };
// Programmatically register the event-handling method
// for the ItemInserted event of a DetailsView control.
customerDetailsView.ItemInserting += new DetailsViewInsertEventHandler(this.CustomerDetailsView_ItemInserting);
// Add the DetailsView object to the Controls collection
// of the PlaceHolder control.
DetailsViewPlaceHolder.Controls.Add(customerDetailsView);
}
void CustomerDetailsView_ItemInserting(Object sender, DetailsViewInsertEventArgs e)
{
// Use the Values property to retrieve the key field value.
String keyValue = e.Values["CustomerID"].ToString();
// Insert the record only if the key field is four characters
// long; otherwise, cancel the insert operation.
if (keyValue.Length == 4)
{
// Change the key field value to upper case before inserting
// the record in the data source.
e.Values["CustomerID"] = keyValue.ToUpper();
MessageLabel.Text = "";
}
else
{
MessageLabel.Text = "The key field must have four digits.";
e.Cancel = true;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewInsertEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewInsertEventHandler Example</h3>
<!-- Use a PlaceHolder control as the container for the -->
<!-- dynamically generated DetailsView control. -->
<asp:PlaceHolder id="DetailsViewPlaceHolder"
runat="server"/>
<br/><br/>
<asp:label id="MessageLabel"
forecolor="Red"
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="DetailsViewSource"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
insertcommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
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)
' Create a new DetailsView object.
Dim customerDetailsView As New DetailsView()
' Set the DetailsView object's properties.
customerDetailsView.ID = "CustomerDetailsView"
customerDetailsView.DataSourceID = "DetailsViewSource"
customerDetailsView.AutoGenerateRows = True
customerDetailsView.AutoGenerateInsertButton = True
customerDetailsView.AllowPaging = True
Dim keyArray() As String = {"CustomerID"}
customerDetailsView.DataKeyNames = keyArray
' Programmatically register the event-handling method
' for the ItemInserted event of a DetailsView control.
AddHandler customerDetailsView.ItemInserting, AddressOf CustomerDetailsView_ItemInserting
' Add the DetailsView object to the Controls collection
' of the PlaceHolder control.
DetailsViewPlaceHolder.Controls.Add(customerDetailsView)
End Sub
Sub CustomerDetailsView_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
' Use the Values property to retrieve the key field value.
Dim keyValue As String = e.Values("CustomerID").ToString()
' Insert the record only if the key field is four characters
' long; otherwise, cancel the insert operation.
If keyValue.Length = 4 Then
' Change the key field value to upper case before inserting
' the record in the data source.
e.Values("CustomerID") = keyValue.ToUpper()
MessageLabel.Text = ""
Else
MessageLabel.Text = "The key field must have four digits."
e.Cancel = True
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewInsertEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewInsertEventHandler Example</h3>
<!-- Use a PlaceHolder control as the container for the -->
<!-- dynamically generated DetailsView control. -->
<asp:PlaceHolder id="DetailsViewPlaceHolder"
runat="server"/>
<br/><br/>
<asp:label id="MessageLabel"
forecolor="Red"
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="DetailsViewSource"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
insertcommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
connectionstring=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
En el ejemplo de código siguiente se muestra cómo agregar mediante declaración un DetailsViewInsertEventHandler delegado al ItemInserting evento de un DetailsView control.
<%@ 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 CustomerDetailsView_ItemInserting(Object sender,
DetailsViewInsertEventArgs e)
{
// Use the Values property to retrieve the key field value.
String keyValue = e.Values["CustomerID"].ToString();
// Insert the record only if the key field is four characters
// long; otherwise, cancel the insert operation.
if (keyValue.Length == 4)
{
// Change the key field value to upper case before inserting
// the record in the data source.
e.Values["CustomerID"] = keyValue.ToUpper();
MessageLabel.Text = "";
}
else
{
MessageLabel.Text = "The key field must have four digits.";
e.Cancel = true;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewInsertEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewInsertEventHandler Example</h3>
<asp:detailsview id="CustomerDetailsView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogenerateinsertbutton="true"
autogeneraterows="true"
allowpaging="true"
oniteminserting="CustomerDetailsView_ItemInserting"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<asp:label id="MessageLabel"
forecolor="Red"
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="DetailsViewSource"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
insertcommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode],
[Country]) VALUES (@CustomerID, @CompanyName, @Address,
@City, @PostalCode, @Country)"
connectionstring=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="VB" autoeventwireup="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomerDetailsView_ItemInserting(ByVal sender As Object, _
ByVal e As DetailsViewInsertEventArgs) _
Handles CustomerDetailsView.ItemInserting
' Use the Values property to retrieve the key field value.
Dim keyValue As String = e.Values("CustomerID").ToString()
' Insert the record only if the key field is four characters
' long; otherwise, cancel the insert operation.
If keyValue.Length = 4 Then
' Change the key field value to upper case before inserting
' the record in the data source.
e.Values("CustomerID") = keyValue.ToUpper()
MessageLabel.Text = ""
Else
MessageLabel.Text = "The key field must have four digits."
e.Cancel = True
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewInsertEventHandler Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewInsertEventHandler Example</h3>
<asp:detailsview id="CustomerDetailsView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogenerateinsertbutton="true"
autogeneraterows="true"
allowpaging="true"
oniteminserting="CustomerDetailsView_ItemInserting"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<asp:label id="MessageLabel"
forecolor="Red"
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="DetailsViewSource"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
insertcommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode],
[Country]) VALUES (@CustomerID, @CompanyName, @Address,
@City, @PostalCode, @Country)"
connectionstring=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
Comentarios
El DetailsView control genera el ItemInserting evento cuando se hace clic en un botón Insertar (un botón con su CommandName
propiedad establecida en "Insertar") dentro del control, pero antes de que el DetailsView control inserte el registro. Esto permite proporcionar un controlador de eventos que realice una rutina personalizada, como codificar HTML los valores de un registro antes de insertarlo en el origen de datos, siempre que se produzca este evento.
Cuando se crea un delegado DetailsViewInsertEventHandler, se identifica el método que controlará el evento. Para asociar el evento al controlador, se debe agregar una instancia del delegado al evento. Siempre que se produce el evento, se llama a su controlador, a menos que se quite el delegado. Para obtener más información sobre los delegados del controlador de eventos, vea Control y generación de eventos.
Métodos de extensión
GetMethodInfo(Delegate) |
Obtiene un objeto que representa el método representado por el delegado especificado. |