SqlDataSource.ButtonField
1.
表示一个字段,该字段显示为数据绑定控件中的按钮。
2.
数据绑定控件(如
GridView
和
DetailsView
)使用
ButtonField
类为每个显示的记录显示一个按钮。根据在其中使用
ButtonField
对象的数据绑定控件,该对象会以不同的方式显示。例如,
GridView
控件将
ButtonField
对象显示为一列
,而
DetailsView
控件则将该对象显示为一行。
GridView
控件将
ButtonField
对象显示为一列
,----
〉竖着的一列
<%
@
Page language="C#" %>
<
script
runat="server">
void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = CustomersGridView.Rows[index];
TableCell contactName = selectedRow.Cells[1];
string contact = contactName.Text;
// Display the selected author.
Message.Text = "You selected " + contact + ".";
}
}
</
script
>
<
html
>
<body>
<form id="Form1" runat="server">
<h3>ButtonField Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<!-- Populate the Columns collection declaratively. -->
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
runat="server">
<columns>
<%
--commandname-->GridViewCommandEventArgs--
%>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
<asp:boundfield datafield="CompanyName"
headertext="Company Name"/>
<asp:boundfield datafield="ContactName"
headertext="Contact Name"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource
id="CustomersSqlDataSource"
selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]"
connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</
html
>
Important
:
1、
onrowcommand
="CustomersGridView_RowCommand"
2、
<
asp
:
buttonfield
按下Button按钮时会引发GridView的RowCommand事件,就是触发CustomersGridView_RowCommand
3、
GridViewRow
selectedRow = CustomersGridView.Rows[index];
4、
int
index = Convert.ToInt32(e.CommandArgument)
5、
TableCell
contactName = selectedRow.Cells[1];//[1]
第一列[2]第二列
;
6、
if
(e.CommandName=="Select")
<asp:buttonfield buttontype="Button"
//commandname="Select"