使用FormView插入

时间:2009-11-05 18:44:13

标签: c# asp.net .net formview

我有一个formview控件,在ItemCreated事件中,我使用默认值“启动”了一些字段。

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

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

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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