protected void gdvMain_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string  = string.Empty;
        if (e.CommandArgument != null)
        {
             = e.CommandArgument.ToString();
        }        if (e.CommandName.Equals("Mark")&&!.Equals("YZ"))
        {
            GridViewRow gvrow = ((e.CommandSource as ImageButton).NamingContainer) as GridViewRow;
            int id = 0;
            if (gvrow != null)
            {
                DataRowView drvMain = gvrow.DataItem as DataRowView;
                if (drvMain != null)
                {
                    int.TryParse(Convert.ToString(drvMain["ID"]), out id);
                }
            }            PeprisOA.Model.CashCall modelCash = new PeprisOA.Model.CashCall();
            PeprisOA.BLL.CashCall managerCash = new PeprisOA.BLL.CashCall();
            modelCash = managerCash.GetModel(id);            if (modelCash != null)
            {
                switch ()
                {
                    case "WS":
                        modelCash.FsValue = "CS";
                        break;
                    case "CS":
                        modelCash.FsValue = "FS";
                        break;
                    case "FS":
                        modelCash.FsValue = "FH";
                        break;
                    case "FH":
                        modelCash.FsValue = "YZ";
                        break;
                    default:
                        modelCash.FsValue = "CS";
                        break;
                }
                if (managerCash.Update(modelCash))
                {
                    PeprisOA.Common.JsUtil.Alert(this, "批复成功!");
                }
                else
                {
                    PeprisOA.Common.JsUtil.Alert(this, "批复失败,请重新批复!");
                }
            }
        }
代码如上,为什么DataRowView drvMain = gvrow.DataItem as DataRowView;的DataItem为Null?

解决方案 »

  1.   

    跟踪过了,DataItem确实为Null,我是想知道这是为什么?按说只要有过DataBind之后DataItem就能得到,为什么是Null呢?
      

  2.   

    换了种方法,问题已解决…粘上代码…
    前台放了个隐藏域,值绑定Id,后台Command事件中获取控件,取得Id GridViewRow gvrow = ((e.CommandSource as ImageButton).NamingContainer) as GridViewRow;
                
                int id = 0;
                if (gvrow != null)
                {
                    HiddenField hfId=gvrow.FindControl("hfId") as HiddenField;
                    if (hfId != null)
                    {
                        int.TryParse(Convert.ToString(hfId.Value), out id);
                    }
                }
      

  3.   

    其实我还是想问,为什么DataItem为Null呢?求解,求解…
      

  4.   

    绑定代码写在回发里吧  IsPostBack这样回发的时候  不会触发绑定, 值是从ViewState中取的
      

  5.   

    恩,楼上说的很有可能,但是我没试…不过 ,应该是这样的…在每次获取DataItem之前必须要重新触发DataBind事件…