玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

GridView取值方法

Posted on 2007-10-11 10:42 玄铁剑 阅读(3057) 评论(0)  编辑 收藏 引用 所属分类: asp.net

 protected void gvDemo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int rowIndex = -1;
        GridViewRow row = null;       
        switch (e.CommandName)
       {
            case "Command1": // 模板列
                // 对于模板列内的按钮,我们需要显示绑定行索引到按钮的 CommandArgument 属性
                // 以获取触发事件的行信息
                rowIndex = Convert.ToInt32(e.CommandArgument);
                row = gvDemo.Rows[rowIndex];               
                break;
            case "Command2": // 模板列
                // 同样处于模板列中,但不采用 Command1 方式,而是通过 NamingContrainer 属性
                // 直接获取当前的 GridViewRow
                Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control
                row = cmdControl.NamingContainer as GridViewRow;
                break;
            case "Command3": // 绑定列
                // 对于 ButtonField 列,数据源控件内部自动以适当的项索引值填充 CommandArgument 属性。
                // 而无需我们显示绑定其 CommandArgument 属性               
                // 注意,我们这里无法采用 Command2 的方式,对于 BUttonField 触发的事件,
                // GridViewCommandEventArgs.CommandSource 表示的包含此按钮的 GridView
                rowIndex = Convert.ToInt32(e.CommandArgument);
                row = gvDemo.Rows[rowIndex];
                break;
           case "custcmd":
                rowIndex = Convert.ToInt32(e.CommandArgument);
                row = gvDemo.Rows[rowIndex];       
                break;
        }
    }

      <asp:TemplateField HeaderText="cmd" ShowHeader="False">
         <ItemTemplate>
                   <asp:ImageButton ID="imgbtn" runat="server" CausesValidation="False" CommandName="custcmd"
                        ImageUrl="" CommandArgument='<%# gvDemo.Rows.Count %>' />
         </ItemTemplate>
      </asp:TemplateField>

只有注册用户登录后才能发表评论。