【问题标题】:using FormView to insert使用 FormView 插入
【发布时间】:2009-11-05 18:44:13
【问题描述】:

我有一个表单视图控件,在ItemCreated 事件中,我正在“启动”一些具有默认值的字段。

但是,当我尝试使用 formview 插入时,在调用 ItemInserting 事件之前,由于某种原因它首先调用 ItemCreated。这会导致字段在插入发生之前被默认值覆盖。

如何让它在ItemInserting 事件之前不调用ItemCreated 事件?

【问题讨论】:

    标签: c# asp.net .net formview


    【解决方案1】:

    需要使用formview Databound 事件而不是formview ItemCreated 事件来设置值,试试like

    protected void frm_DataBound(object sender, EventArgs e)
    {
        if (frm.CurrentMode == FormViewMode.Edit)//whatever your mode here is.
        {
            TextBox txtYourTextBox = (TextBox)frm.FindControl("txtYourTextBox");
            txtYourTextBox.Text// you can set here your Default value
        }
    }
    

    同时检查这个类似问题的线程 FormView_Load being overwritten C# ASP.NET

    【讨论】:

      【解决方案2】:

      您无法更改事件触发的顺序。但是,您可能应该将设置默认值的代码包装在 !IsPostBack 中,这样它就不会重置您的值,例如:

      protected void FormView_ItemCreated(Object sender, EventArgs e)
      {
        if(!IsPostBack)
        {
          //Set default values ...
        }
      }
      

      【讨论】:

      • 那行不通,因为当我真的想点击 ItemCreated 时,它是在回发中。
      • 在这种情况下,您可能需要在设置之前添加一个 If 语句检查是否有值。
      【解决方案3】:

      尝试检查表单视图的CurrentMode 属性。

      void FormView_ItemCreated(object sender, EventArgs e)
      {
          if (FormView.CurrentMode != FormViewMode.Insert)
          {
              //Initialize your default values here
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-14
        相关资源
        最近更新 更多