我用DataGrid做的一个列表,
现在要在每一行上做一个删除按钮请问怎么传递参数id给cs文件中的删除方法呀

解决方案 »

  1.   

    <%@ 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>
      

  2.   

    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;
    }
                                }
                       }
      

  3.   

    写错了,呵呵,应该是case "Delete" :
      

  4.   

    DataGrid的OnDeleteCommand="事件名称"
    然后或者Request或者按rainmanII() 那样……