//通过数据
    protected void pasBtn_Click(object sender, EventArgs e)
    {
        int nNewsID = Int32.Parse(NewsGridView.DataKeys[e.RowIndex].Value.ToString());//System.EventArgs”并不包含“RowIndex”的定义        OleDbConnection MyConn;
        MyConn = new OleDbConnection(ConfigurationManager.AppSettings["myconnstring"].ToString() + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["mydbpath"]));
        MyConn.Open();
        string cmdText = "update News set pass=1 WHERE Id=" + nNewsID;
        OleDbCommand myCommend = new OleDbCommand(cmdText, MyConn);
        myCommend.ExecuteNonQuery();
        BindGridView(drpSelect.SelectedValue.ToString(), tkeyword);
    }这里的id值应该怎么取啊

解决方案 »

  1.   

    lz是要响应GridView上某一行的的button click事件吧?用GridView的 GridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)事件,e.RowIndex获取鼠标点击的那一行
      

  2.   

    CommandArgument="<%# (Container as GridViewRow).RowIndex %>"这个属性
      

  3.   

       protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {  
            int T_Id = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
        }
    在GridView1_RowUpdating事件里获取
      

  4.   

    pasBtn给这个控件的 CommandArgument 属性加上索引id
    CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ID","{0}")%>'在这个事件中 pasBtn_Click(object sender, EventArgs e) ,可以如下获取 int nNewsID = Int32.Parse(e.CommandArgument.ToString())
      

  5.   

    Winform里面DataGridView有CellContentClick事件WebApp里面GridView有OnRowCommand事件
      

  6.   


    不行啊,System.EventArgs”并不包含“CommandArgument”的定义
      

  7.   


    GridView“NewsGridView”激发了未处理的事件“RowUpdating”。前面加OnRowUpdating="GridVIew_RowUpdating";也没用
      

  8.   

    前台页面
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductTypeaspx.aspx.cs" Inherits="ProductTypeaspx" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
        <style type="text/css">
          table 
             {border-collapse:   collapse;}   
           td  
             {border:   solid   1px   #cccccc;}   
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <div style="z-index: 101; left: 131px; width: 539px; position: absolute; top: 38px;
                height: 431px">
                <table style="border-right: #cccccc 1px solid; border-top: #cccccc 1px solid; font-size: 9pt;
                    border-left: #cccccc 1px solid; border-bottom: #cccccc 1px solid; color: #006699;">
                    <tr>
                        <td style="width: 99px">
                            商品类别名称:</td>
                        <td style="width: 182px">
                            <asp:TextBox ID="TextBox1" runat="server" Height="15px" Style="border-right: #cccccc 1px solid;
                                border-top: #cccccc 1px solid; border-left: #cccccc 1px solid; border-bottom: #cccccc 1px solid"></asp:TextBox></td>
                        <td style="width: 262px">
                            <asp:Button ID="Button1" runat="server" Height="19px" OnClick="Button1_Click" Style="border-right: #cccccc 1px solid;
                                border-top: #cccccc 1px solid; border-left: #cccccc 1px solid; border-bottom: #cccccc 1px solid;
                                background-color: #ffffff" Text="添 加" Width="46px" /></td>
                    </tr>
                    <tr>
                        <td colspan="3" style="border-top: #cccccc 1px solid">
                            所有商品信息:</td>
                    </tr>
                </table>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Style="font-size: 9pt; color: #006699;"
                    Width="540px" DataKeyNames="T_Id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Height="100px">
                    <Columns>
                        <asp:TemplateField HeaderText="序号">
                             <ItemTemplate>
                                   <%#Container.DataItemIndex+1 %>
                             </ItemTemplate>
                            <HeaderStyle Width="80px" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="T_Name" HeaderText="商品名称" >
                            <HeaderStyle Width="250px" />
                        </asp:BoundField>
                        <asp:CommandField HeaderText="编辑" ShowEditButton="True" >
                            <HeaderStyle Width="110px" />
                        </asp:CommandField>
                        <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                    </Columns>
                    <RowStyle HorizontalAlign="Center" BackColor="White" ForeColor="#330099" />
                    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="LightGray" Font-Bold="True" ForeColor="DimGray" />
                </asp:GridView>
            </div>
        
        </div>
        </form>
    </body>
    </html>后台页面using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class ProductTypeaspx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        /// <summary>
        /// 初始化GridView绑定的值
        /// </summary>
        void bind()
        {
            this.GridView1.DataSource = BLL.ProductBLL.QuerryProducttypeInfo();
            this.GridView1.DataBind();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            this.bind();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int T_Id = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
            BLL.ProductBLL.DeleteproductType(T_Id);
            bind();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {  
            int T_Id = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
            string T_Name = (this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
            if (BLL.ProductBLL .UpdateproductType(T_Id,T_Name))
            {
                this.GridView1.EditIndex = -1;
                bind();
            }
            else 
            {
                ClientScript.RegisterStartupScript(this.GetType (),"","<script>alert('修改失败!')</script>");
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            this.bind();
        }
        /// <summary>
        /// 绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                //行的状态判断
                if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
                {
                    LinkButton bb = e.Row.Cells[3].Controls[0] as LinkButton;
                    bb.Attributes.Add("onclick", "javascript:return confirm('删除后将不能恢复!确定要删除吗?')");
                }
                else { }
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.TextBox1.Text=="")
            {
                ClientScript.RegisterStartupScript(this.GetType (),"","<script>alert('类别名称不能为空!')</script>");
            }
            else
            {
                if (BLL.ProductBLL.AddProductType(this.TextBox1.Text))
                {
                    this.bind();
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败!')</script>");
                }
            }
        }
    }做了一个简单的例子,看看是不是你想要的效果!
      

  9.   

    GridView有OnRowCommand事件int T_Id = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString()); 就对了啊记得如果有多个事件,请给commandname属性取个名字