private void gv01_RowUpdating(object sender,GridViewUpdateEventArgs e)
{
  //......
  do something  
}GridView里有一个inputfile:file01,我想当file01.PostedFile.ContentLength>0时才执行gv01_RowUpdating事件,这个怎样处理,在哪处理?谢谢

解决方案 »

  1.   

    直接在gv01_RowUpdating里判断文件大小可否?也许应该看看RowCommand事件以及RowUpdating事件先后执行顺序
      

  2.   

    GridView.RowUpdating只有在点击GridView的‘更新’按钮时才执行。也就是说之前已经点击‘编辑’按钮进入编辑模式了。在编辑模式下点击‘更新’时才执行RowUpdating.不满足某个条件时就不让GridView进入更新,那就在GridView.RowCommand事件处理代码里检查条件,然后阻止进入编辑模式。如果已经进入编辑模式,那就在RowUpdating里检查条件,然后取消更新。
      

  3.   

    对了,不是GridView.RowCommand,这是在按下GridView控件中的Button时发生的。应该是RowEditing,即GridView控件进入编辑模式前(当按下数据行的【编辑】按钮时)时发生。protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
            if (1 == 1) // 这里是你的条件。
            {
                e.Cancel = true;            Literal txtMsg = new Literal();
                txtMsg.Text = "<script>alert('数据编辑取消')</script>";
                Page.Controls.Add(txtMsg);
            }
    }
      

  4.   

    如下事件:
      private void gv01_RowUpdating(object sender,GridViewUpdateEventArgs e)
      {
        string fname,firstname,filefullname,savePath;
    GridViewRow erow=gv01.Rows[gv01.EditIndex];

    savePath=Server.MapPath("..\\Attachments\\");

    HtmlInputFile file01=(HtmlInputFile)erow.Cells[2].Controls[1];

    if(file01.PostedFile.ContentLength>0)
    {
      File.Delete(savePath+erow.Cells[1].Text);
      
      fname=System.IO.Path.GetFileName(file01.PostedFile.FileName);
      firstname=System.IO.Path.GetFileNameWithoutExtension(file01.PostedFile.FileName);
      filefullname=DateTime.Now.ToString("yyyymmddhhss")+"_"+fname;

      try
      {
        e.NewValues["fileno"]=erow.Cells[0].Text;
        e.NewValues["att"]=filefullname;

        file01.PostedFile.SaveAs(savePath+filefullname);
      }
      catch(Exception ex)
      {
        throw(ex);
      }
        }
    else
    {
      Response.Write(@"<script>alert('請選擇附件!')</script>");
      e.Cancel=false;
    }
      }如果我没有加附件,即file01.PostedFile.ContentLength>0为空时,会报错:
    必須宣告變數 '@att'。 
    描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。 例外詳細資訊: System.Data.SqlClient.SqlException: 必須宣告變數 '@att'。
      

  5.   

    问题是你已经请求了事件,
    办法1 用 JS 控制
    办法2 读取值,条件不符合就直接 retuen;
      

  6.   

    to FreeSarge 办法1 用 JS 控制 ---我也想用js控制,但不知怎么用。办法2 读取值,条件不符合就直接 retuen;
    這個辦法和我的代碼是不是一樣的?不一樣又要怎樣弄?謝謝!
      

  7.   

    js 按钮 + OnClientClick = retrun JsFcuntion(this)function JsFcuntion(obj)
    {
       paren = obj.parentElement;
       return parseInt(paren.cells[?].firstChild.PostedFile.ContentLength);
    }?  是你 inputfile 所在单元格的列
    PostedFile.ContentLength 你说的这个属性,我也没找到,你自己调试一下,大体的思路就是这个,先找到你要的数据,然后根据结果返回是否请求事件。
      

  8.   

    paren = obj.parentElement..parentElement;
    这里写错了