日期:2014-05-18  浏览次数:21054 次

RowCommand 中DataItem的问题
C# code

 protected void gdvMain_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string mark = string.Empty;
        if (e.CommandArgument != null)
        {
            mark = e.CommandArgument.ToString();
        }

        if (e.CommandName.Equals("Mark")&&!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 (mark)
                {
                    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?

------解决方案--------------------
跟踪,确认转换对象存在
------解决方案--------------------
绑定代码写在回发里吧 IsPostBack

这样回发的时候 不会触发绑定, 值是从ViewState中取的