下面代码通过测试 好用 BUTTON是放在DATAGRID的摸版列里的Private Sub MyDataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.ItemCommand '********************* DATAGRID BUTTON EVENT ********************** Dim b As Button b = CType(e.Item.Cells(3).FindControl("ok"), Button) If (e.CommandName = "Delete") Then b.Attributes("onclick") = Nothing b.CommandName = "jj" b.Text = "nook" Else b.Attributes.Add("onclick", "javascript:return M_mail();") b.CommandName = "Delete" b.Text = "OK" End If '******************************************************************** End SubPrivate Sub MyDataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.DeleteCommand Response.Write("fdsafjasljflsadf") End Sub<head> <script language="javascript"> function M_mail(){ if (!confirm("よろしいですか?")) { return false; }else{ return true; } } </script></head>
{
//执行按纽代码
}
还可以在datagrid_itemdatabind的时候
Dim Obj As Object Obj = e.Item.FindControl("button")
Obj.Attributes.Add("onkeydown", "if(event.keyCode==13) { event.keyCode=9}")换成C#也应该很容易的吧?
如何给一个按钮加上删除确认呢???
为什么有错呢?btnDelete.Attributes["onclick"]="javascript:if(confirm('确定要删除数据库吗?')){return true;} else {return false;}";
为什么有错呢?btnDelete.Attributes["onclick"]="javascript:if(confirm('确定要删除数据库吗?')){return true;} else {return false;}";
你要自己分析COMMANDNAME 来区分是哪个按扭
分析:
如下,该模板列定义了两个Button.
===========================================
<Columns>
<asp:ButtonColumn HeaderText="Modify" ButtonType="PushButton" Text="Modify" CommandName="Modify" />
<asp:ButtonColumn HeaderText="Delete" ButtonType="PushButton" Text="Delete"
CommandName="Delete" />
</Columns>
===========================================
将datagrid的OnItemCommand属性设为"OnItemCommand"
在添加如下函数:
===========================================
void OnItemCommand(Object sender, DataGridCommandEventArgs e)
{
if( ((Button)e.CommandSource).CommandName == "Modify")
{
Label1.Text="Modify"+e.Item.Cells[3].Text; }
else if( ((Button)e.CommandSource).CommandName == "Delete")
{
Label1.Text="Delete"+e.Item.Cells[3].Text;
} }
============================================
if( ((Button)e.CommandSource).CommandName == "Modify")中设定的Modify即datagrid中的
CommandName属性值。选定分支后就是写你要做的代码了。要说明的是取出相应行中的数据。
即用e.Item.Cells[3].Text。
要说的都已经说完了,下面是我做测试时用的程序。用的是sqlserver2000自带的数据库pubs
你可以根据需要自已练习一下。=============================================
=============================================<%@ Page Debug="true" Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server"> // Public variable, DataSet is kept in Session.
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter;
SqlCommandBuilder commandBuilder;
DataSet dataSet; private void DataBind()
{
grid1.DataSource = dataSet;
grid1.DataBind();
} private void PrepareConnection()
{
string dataSource = "Data Source=localhost;";
string security = "user id=sa; password=sa;";
string initialCatalog = "initial catalog=pubs;";
string cnnString = dataSource + security + initialCatalog;
connection = new SqlConnection(cnnString); // Create Data Command
string strSql = "select au_id, au_lname, au_fname, address, phone from [authors]";
command = new SqlCommand(strSql, connection); // Create Data Adapter and its commands
adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
commandBuilder = new SqlCommandBuilder(adapter);
adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
adapter.DeleteCommand = commandBuilder.GetDeleteCommand();
adapter.InsertCommand = commandBuilder.GetInsertCommand();
} public void Page_Load()
{
if(!IsPostBack)
{
PrepareConnection();
dataSet = new DataSet(); try
{
connection.Open();
adapter.FillSchema(dataSet, SchemaType.Mapped);
adapter.Fill(dataSet);
}
catch(SqlException e)
{
Response.Write(e.ToString());
return;
}
finally
{
connection.Close();
} Session["dataSet"] = dataSet;
DataBind();
} dataSet = (DataSet)Session["dataSet"];
} void OnItemCommand(Object sender, DataGridCommandEventArgs e)
{
if( ((Button)e.CommandSource).CommandName == "Modify")
{
Label1.Text="Modify"+e.Item.Cells[3].Text; }
else if( ((Button)e.CommandSource).CommandName == "Delete")
{
Label1.Text="Delete"+e.Item.Cells[3].Text;
} }</script>
<html>
<head>
<title>Modify and Delete Data</title>
</head>
<body>
<form id="form1" runat="server">
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
<asp:DataGrid id="grid1" runat="server" AutoGenerateColumns="true" OnItemCommand="OnItemCommand">
<HeaderStyle backcolor="lightblue" font-name="Arial" font-bold="true" />
<ItemStyle backcolor="lightyellow" />
<Columns>
<asp:ButtonColumn HeaderText="Modify" ButtonType="PushButton" Text="Modify" CommandName="Modify" />
<asp:ButtonColumn HeaderText="Delete" ButtonType="PushButton" Text="Delete" CommandName="Delete" />
</Columns>
</asp:DataGrid>
</p> </form>
</body>
</html>
好用
BUTTON是放在DATAGRID的摸版列里的Private Sub MyDataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.ItemCommand
'********************* DATAGRID BUTTON EVENT **********************
Dim b As Button
b = CType(e.Item.Cells(3).FindControl("ok"), Button)
If (e.CommandName = "Delete") Then
b.Attributes("onclick") = Nothing
b.CommandName = "jj"
b.Text = "nook"
Else
b.Attributes.Add("onclick", "javascript:return M_mail();")
b.CommandName = "Delete"
b.Text = "OK"
End If
'********************************************************************
End SubPrivate Sub MyDataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.DeleteCommand
Response.Write("fdsafjasljflsadf")
End Sub<head>
<script language="javascript">
function M_mail(){
if (!confirm("よろしいですか?"))
{
return false;
}else{
return true;
}
}
</script></head>
这里边