参数如何传递的问题?? 我用DataGrid做的一个列表,现在要在每一行上做一个删除按钮请问怎么传递参数id给cs文件中的删除方法呀 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %><html> <script language="C#" runat="server"> SqlConnection myConnection; protected void Page_Load(Object sender, EventArgs e) { myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes"); if (!IsPostBack) BindGrid(); } public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e) { String deleteCmd = "DELETE from Employee where emp_id = @Id"; SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection); myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11)); myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try { myCommand.ExecuteNonQuery(); Message.InnerHtml = "<b>已删除记录</b><br>" + deleteCmd; } catch (SqlException) { Message.InnerHtml = "错误:未能删除记录"; Message.Style["颜色"] = "红色"; } myCommand.Connection.Close(); BindGrid(); } public void BindGrid() { SqlDataAdapter myCommand = new SqlDataAdapter("select * from Employee", myConnection); DataSet ds = new DataSet(); myCommand.Fill(ds, "员工"); MyDataGrid.DataSource=ds.Tables["员工"].DefaultView; MyDataGrid.DataBind(); }</script><body style="font: 10.5pt 宋体"> <form runat="server"> <h3><font face="宋体">删除数据行</font></h3> <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p> <ASP:DataGrid id="MyDataGrid" runat="server" Width="800" BackColor="#ccccff" BorderColor="black" ShowFooter="false" CellPadding=3 CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" DataKeyField="emp_id" OnDeleteCommand="MyDataGrid_Delete" > <Columns> <asp:ButtonColumn Text="删除员工" CommandName="Delete"/> </Columns> </ASP:DataGrid> </form></body></html> datagrid本来就是服务器端控件,可以直接在cs中写:首先在你的DataGrid属性生成器中选择这个按钮列,其中有一项是命令名,在这填入Delete(可以随便写),然后在DataGrid事件中双击ItemCommand生成DataGrid1_ItemCommand事件,在此事件中写如下代码:private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { string id = e.Item.Cells[0].Text.Trim();//可取到id switch(e.CommandName) { case "Open" : { //(要执行的代码) break; } } } 写错了,呵呵,应该是case "Delete" : DataGrid的OnDeleteCommand="事件名称"然后或者Request或者按rainmanII() 那样…… 删除textbox下拉列选取的内容 C#更新文件报错,大家帮忙看看!分不够可以加~! 新手提问:关于引用类型与值类型的疑问 如何提取控件的问题!高中低手全部来看看! 窗体 传值 问题 帮帮忙 能不能将自己写的控件放入GAC中共享使用阿? 好简单的问题,帮帮忙 winform下如何改变dataGrid指定某行的样式(如颜色等) 正则表达式如何不允许重复字符? 帮我看看这段哪里有错。 一个关于javascript和后台程序结合的问题,我郁闷,请各位大侠来救我于水火 winform用选中datagrid的行后打开新页面,在新页面修改数据后怎么刷新父页面中的datagrid?大家帮助啊
<%@ Import Namespace="System.Data.SqlClient" %><html>
<script language="C#" runat="server"> SqlConnection myConnection; protected void Page_Load(Object sender, EventArgs e)
{
myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes"); if (!IsPostBack)
BindGrid();
} public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
{
String deleteCmd = "DELETE from Employee where emp_id = @Id"; SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>已删除记录</b><br>" + deleteCmd;
}
catch (SqlException)
{
Message.InnerHtml = "错误:未能删除记录";
Message.Style["颜色"] = "红色";
} myCommand.Connection.Close(); BindGrid();
} public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Employee", myConnection); DataSet ds = new DataSet();
myCommand.Fill(ds, "员工"); MyDataGrid.DataSource=ds.Tables["员工"].DefaultView;
MyDataGrid.DataBind();
}</script><body style="font: 10.5pt 宋体"> <form runat="server"> <h3><font face="宋体">删除数据行</font></h3> <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p> <ASP:DataGrid id="MyDataGrid" runat="server"
Width="800"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
DataKeyField="emp_id"
OnDeleteCommand="MyDataGrid_Delete"
> <Columns>
<asp:ButtonColumn Text="删除员工" CommandName="Delete"/>
</Columns> </ASP:DataGrid> </form></body>
</html>
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string id = e.Item.Cells[0].Text.Trim();//可取到id
switch(e.CommandName)
{
case "Open" :
{
//(要执行的代码)
break;
}
}
}
然后或者Request或者按rainmanII() 那样……