/// <summary>
/// 生成事件时激发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        protected void DVClientInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int strProjectId = ?;
            if (e.CommandName == "redact")//编辑
            { 
            }
            else if (e.CommandName == "forbid")//禁用
            {
                bool temp = centaline.Pro.ClassLibrary.Property.CProject.UpStatusF(strProjectId);
            }
            else if (e.CommandName == "comeback")//恢复
            {
                bool temp = centaline.Pro.ClassLibrary.Property.CProject.UpStatusR(strProjectId);
            }
        }
页面代码:
<asp:GridView ID="DVClientInfo" runat="server" onrowdatabound="DVClientInfo_RowDataBound" onrowcommand="DVClientInfo_RowCommand">
<Columns>
    <asp:TemplateField>
      <HeaderTemplate>
         <input id="Checkbox2" type="checkbox" runat="server" /><label>全选</label>
      </HeaderTemplate>
    </asp:TemplateField>
    <asp:BoundField Visible="False" DataField="ProjectId">
    </asp:BoundField>
    <asp:BoundField DataField="PropertyName" HeaderText="项目名称" ></asp:BoundField>
    <asp:BoundField DataField="PropertyCode" HeaderText="项目编号" ></asp:BoundField>
    <asp:BoundField DataField="PropertyDept" HeaderText="隶属部门" ></asp:BoundField>
    <asp:BoundField DataField="Status" HeaderText="状态" ></asp:BoundField>
    <asp:TemplateField HeaderText="操作">
           <ItemTemplate>
              <asp:LinkButton ID="lbtn_redact" runat="server" CommandName="redact" >编辑</asp:LinkButton>
              <asp:LinkButton ID="lbtn_forbid" runat="server" CommandName="forbid" >禁用</asp:LinkButton>
              <asp:LinkButton ID="lbtn_comeback" runat="server" CommandName="comeback" >恢复</asp:LinkButton>
            </ItemTemplate>
            <Itemstyle HorizontalAlign="Center" />
    </asp:TemplateField>
     </Columns>
</asp:GridView>效果如上!我要获得 int strProjectId = ?; 的值 请教各位高手大哥帮帮忙!

解决方案 »

  1.   

     GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
     int index = gvrow.RowIndex;
             
               
    string tNumber =GridView_projectTask.DataKeys[index].
    Values["strProjectId "].ToString();
      

  2.   


    /// <summary>
            /// 生成事件时激发
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void DVClientInfo_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                int strProjectId =            if (e.CommandName == "redact")//编辑
                {             }
                else if (e.CommandName == "forbid")//禁用
                {
                    bool temp = centaline.Pro.ClassLibrary.Property.CProject.UpStatusF(strProjectId);
                    if (temp)
                    {
                        Bind();
                    }
                }
                else if (e.CommandName == "comeback")//恢复
                {
                    bool temp = centaline.Pro.ClassLibrary.Property.CProject.UpStatusR(strProjectId);
                    if (temp)
                    {
                        Bind();
                    }
                }
            }
      

  3.   


            /// <summary>
            /// 生成事件时激发
            /// </summary>
            /// <param name="source"></param>
            /// <param name="e"></param>
            protected void grdbank_ItemCommand(object source, DataGridCommandEventArgs e)
            {
                int strPropertyId = Convert.ToInt32(e.Item.Cells[0].Text);            if (e.CommandName == "lb1")
                {
                    ProjectRedact.Visible = true;                this.Label1.Text = e.Item.Cells[1].Text.ToString().Trim();
                    this.Label2.Text = e.Item.Cells[2].Text.ToString().Trim();
                    this.TextBox6.Text = e.Item.Cells[3].Text.ToString().Trim();
                    this.TextBox7.Text = e.Item.Cells[4].Text.ToString().Trim();
                    this.TextBox8.Text = e.Item.Cells[5].Text.ToString().Trim();
                    this.TextBox9.Text = e.Item.Cells[6].Text.ToString().Trim();
                }
                else if (e.CommandName == "lb2")
                {
                    bool temp = EM_Project.ClassLibrary.CUser.UpPropertyF(strPropertyId);                if (temp)
                    {
                        Bind();
                    }
                }
                else if (e.CommandName == "lb3")                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                {
                    bool temp = EM_Project.ClassLibrary.CUser.UpPropertyR(strPropertyId);                if (temp)
                    {
                        Bind();
                    }
                }
            }能不能像DataGrid里面一样 
    用这样int strPropertyId = Convert.ToInt32(e.Item.Cells[0].Text);的语句代替?
    因为对GridView不太熟悉!
      

  4.   

    可以在LINKBUTTON那里加上CommandName='<%#Eval("id")%>'属性,然后在后台e.CommandName获取ID值,把那些操作名都放进CommandArgument里面。我之前试过好像可以,只是一种方法啦!小弟也是新手,不好别喷!
      

  5.   

    那就这样写吧
    GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 
    int index = gvrow.RowIndex; 
            
              
    string tNumber =DVClientInfo.rows[index][2].tostring()
      

  6.   

    前台:<asp:LinkButton runat="server" ID="btnNext" Text="子类" CommandArgument="Next" CommandName='<%#Eval("ID") %>'></asp:LinkButton>后台:long parentId = long.Parse(e.CommandName);
      

  7.   

    还有就是你在前台控件那里加上datakey属性,并且设置为ID,也可以在后台绑定数据的时候,加上
    this.gvData.DataKeyNames = new string[] { "ID" };gvData为你的控件名称,然后在后台
    grdbank_ItemCommand时间里面:
     long id = long.Paese(this.gvData.DataKeys[e.RowIndex].Value)
    看看能获取到不!
      

  8.   


    错误 37 无法将带 [] 的索引应用于“System.Web.UI.WebControls.GridViewRow”类型的表达式
      

  9.   


    少写了个  “.”
    DVClientInfo.Rows [index ].Cells [2].tostring
      

  10.   

    CommandName=' <%#Eval("ID") %>' 这个属性我在别的事件中有用到不能改!最好不要改 CommandName=' <%#Eval("ID") %>'这个属性
      

  11.   

    方法一(可以获取gridview隐藏的列)
     在gridview中设置datakey  添加你要找的列
    GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 
    int index = gvrow.RowIndex;    
    string tNumber =GridView_projectTask.DataKeys[index]. 
    Values["这是字段名"].ToString();方法二:
    GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 
    int index = gvrow.RowIndex; 
            
    string id=DVClientInfo.Rows [index ].Cells [2].ToString();够清楚了吧,我常用的就这2种