gpt4 book ai didi

asp.net - 我可以在 ASP.NET 中向 DetailsView 添加附加操作吗?

转载 作者:行者123 更新时间:2023-12-01 12:01:39 29 4
gpt4 key购买 nike

目前我有一个附加到 GridView 的 DetailsView。当您从 GridView 中选择一个项目时,详细信息会显示在 DetailsView 中(duh)。现在 DetailsView 上有 3 个事件选项:编辑、删除和新建。我想补充第四点,格式。这将简单地运行一些查询,然后执行删除。这可能吗?如何实现?

另一种可能性是将此事件添加到 GridView(也许在编辑模式下?)

最佳答案

如果您按照建议将自己的按钮添加到 DetailsView,则可以 Hook DetailsView 上的 ItemCommand 事件,并在 ItemCommand 方法中为每个 CommandName 添加任何逻辑

<%@ 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 CustomerDetailView_ItemCommand(Object sender, DetailsViewCommandEventArgs e)
{

// Use the CommandName property to determine which button
// was clicked.
if (e.CommandName == "Add")
{

// Add the current store to the contact list.

// Get the row that contains the store name. In this
// example, the store name is in the second row (index 1)
// of the DetailsView control.
DetailsViewRow row = CustomerDetailView.Rows[1];

// Get the store's name from the appropriate cell.
// In this example, the store name is in the second cell
// (index 1) of the row.
String name = row.Cells[1].Text;

// Create a ListItem object with the store's name.
ListItem item = new ListItem(name);

// Add the ListItem object to the ListBox, if the
// item doesn't already exist.
if (!ContactListBox.Items.Contains(item))
{
ContactListBox.Items.Add(item);
}

}

}

</script>

<html >
<head runat="server">
<title>
DetailsView ItemCommand Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>
DetailsView ItemCommand Example</h3>
<asp:DetailsView ID="CustomerDetailView"
DataSourceID="DetailsViewSource"
AutoGenerateRows="false"
DataKeyNames="CustomerID"
AllowPaging="true"
OnItemCommand="CustomerDetailView_ItemCommand"
runat="server">

<FieldHeaderStyle BackColor="Navy" ForeColor="White" />

<Fields>
<asp:BoundField DataField="CustomerID" HeaderText="Store ID" />
<asp:BoundField DataField="CompanyName" HeaderText="Store Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:ButtonField CommandName="Add" Text="Add Contact" />
</Fields>
</asp:DetailsView>

<hr />

Contacts:<br />
<asp:ListBox ID="ContactListBox" 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" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country] From
[Customers]">
</asp:SqlDataSource>
</form>
</body>
</html>

关于asp.net - 我可以在 ASP.NET 中向 DetailsView 添加附加操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/777984/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com