DetailsViewInsertEventHandler Delegar
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Representa o método que manipula o evento ItemInserting de um controle 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
A fonte do evento.
Um DetailsViewInsertEventArgs que contém os dados do evento.
Exemplos
O exemplo de código a seguir demonstra como adicionar programaticamente um DetailsViewInsertEventHandler delegado ao ItemInserting evento de um DetailsView controle .
<%@ 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>
O exemplo de código a seguir demonstra como adicionar declarativamente um DetailsViewInsertEventHandler delegado ao ItemInserting evento de um DetailsView controle.
<%@ 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>
Comentários
O DetailsView controle aciona o ItemInserting evento quando um botão Inserir (um botão com sua CommandName
propriedade definida como "Inserir") dentro do controle é clicado, mas antes que o DetailsView controle insira o registro. Isso permite que você forneça um manipulador de eventos que executa uma rotina personalizada, como codificar HTML os valores de um registro antes de inseri-lo na fonte de dados, sempre que esse evento ocorrer.
Ao criar um DetailsViewInsertEventHandler delegado, você identifica o método que manipulará o evento. Para associar o evento ao manipulador de eventos, adicione uma instância do delegado ao evento. O manipulador de eventos é chamado sempre que o evento ocorre, a menos que você remova o representante. Para obter mais informações sobre delegados do manipulador de eventos, consulte Manipulando e gerando eventos.
Métodos de Extensão
GetMethodInfo(Delegate) |
Obtém um objeto que representa o método representado pelo delegado especificado. |