我在GridView中加了几个ButtonField,他是触发哪个事件的? 
怎么得到是点击了哪一列,哪一行呢?

解决方案 »

  1.   

    触发的是RowCommand事件,用e.CommandArgument判断行
      

  2.   

    ivws_19您好!
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowIndex = Int32.Parse((String)e.CommandArgument);
            Label2.Text = rowIndex.ToString(); 
        }
    可是我这样写,点击的时候没有反应,是事件错了还是代码写错了呢?
      

  3.   

    在前台的buttonfield中加上CommandName="xxx" 
    在rowcommand中判断if (e.CommandName == "xxx")
      

  4.   

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
        { 
            Response.Write("<script>alert('fsafasdf')</script>");
        } 
    就是这样点击也没有反应,是不是没有触发这个事件。
      

  5.   

    在前台的buttonfield中加上CommandName="xxx" 
    在rowcommand中判断if (e.CommandName == "xxx")
    buttonfield属性CommandName的值设成xx(随意),然后在RowCommand事件中判断一下,就像这样:
        protected void gvDengji_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "xx")
            {
                //执行操作
            }
        }如果不明白,再问我!
      

  6.   

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataSourceID="SqlDataSource1" Height="170px" style="font-size: small" 
                Width="960px" onselectedindexchanged="GridView1_SelectedIndexChanged">
                <Columns>
                    <asp:BoundField DataField="ARITICLEID" HeaderText="序号" 
                        SortExpression="ARITICLEID" />
                    <asp:BoundField DataField="USERNAME" HeaderText="发贴人" 
                        SortExpression="USERNAME" />
                    <asp:BoundField DataField="TITLE" HeaderText="标题" SortExpression="TITLE" />
                    <asp:BoundField DataField="RECALLHITS" HeaderText="回复" 
                        SortExpression="RECALLHITS" />
                    <asp:BoundField DataField="IP" HeaderText="IP地址" SortExpression="IP" />
                    <asp:BoundField DataField="IMAGEURL" HeaderText="相关图片" 
                        SortExpression="IMAGEURL" />
                    <asp:BoundField DataField="DATATIME" HeaderText="发贴时间" 
                        SortExpression="DATATIME" />
                    <asp:ButtonField CommandName="xxx" HeaderText="精品" Text="精品" />
                    <asp:ButtonField CommandName="yyy" HeaderText="置顶" Text="置顶" />
                    <asp:ButtonField CommandName="zzz" HeaderText="编辑" Text="编辑" />
                    <asp:ButtonField CommandName="aaa" HeaderText="删除" Text="删除" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString4 %>" 
                ProviderName="<%$ ConnectionStrings:ConnectionString4.ProviderName %>" 
                
                SelectCommand="SELECT * FROM &quot;LGBBS_USERARITICLE&quot; ORDER BY &quot;DATATIME&quot; DESC">
            </asp:SqlDataSource>
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "xxx")
            {
                Response.Write("<script>alert('fsafasdf')</script>");
                int rowIndex = Int32.Parse((String)e.CommandArgument);
                Label2.Text = rowIndex.ToString();
            }
        } jueyingfd 
    这是我的代码 “精品”他就是不执行RowCommand的代码?
    我是个新手,搞了半天了,你帮我看看,非常感谢你。
      

  7.   

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataSourceID="SqlDataSource1" Height="170px" style="font-size: small" 
                Width="960px" onselectedindexchanged="GridView1_SelectedIndexChanged"> 
    这里面没有调用GridView1_RowCommand这个方法!你设置一下方法先!
      

  8.   

    这里面没有调用GridView1_RowCommand这个方法!你设置一下方法先!
      

  9.   

    将前台换为弄为设计模式,点一个gridview选择属性,点一下那个闪电一样的图标就出现了gridview的方法了,打到RowCommand
    双击一下!
      

  10.   

    将aspx文件弄为设计模式,点一下gridview选择属性,点一下那个闪电一样的图标就出现了gridview的方法了,打到RowCommand 
    双击一下,方法出现于后台cs文件中,你这里已存在方法,将后面的方法设为你已有方法就可以了!
    现在没打错字,非常清楚了!
      

  11.   

    fuda_1985 
    非常谢谢你,问题解决了。