【发布时间】:2009-11-05 18:44:13
【问题描述】:
我有一个表单视图控件,在ItemCreated 事件中,我正在“启动”一些具有默认值的字段。
但是,当我尝试使用 formview 插入时,在调用 ItemInserting 事件之前,由于某种原因它首先调用 ItemCreated。这会导致字段在插入发生之前被默认值覆盖。
如何让它在ItemInserting 事件之前不调用ItemCreated 事件?
【问题讨论】:
我有一个表单视图控件,在ItemCreated 事件中,我正在“启动”一些具有默认值的字段。
但是,当我尝试使用 formview 插入时,在调用 ItemInserting 事件之前,由于某种原因它首先调用 ItemCreated。这会导致字段在插入发生之前被默认值覆盖。
如何让它在ItemInserting 事件之前不调用ItemCreated 事件?
【问题讨论】:
需要使用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
【讨论】:
您无法更改事件触发的顺序。但是,您可能应该将设置默认值的代码包装在 !IsPostBack 中,这样它就不会重置您的值,例如:
protected void FormView_ItemCreated(Object sender, EventArgs e)
{
if(!IsPostBack)
{
//Set default values ...
}
}
【讨论】:
尝试检查表单视图的CurrentMode 属性。
void FormView_ItemCreated(object sender, EventArgs e)
{
if (FormView.CurrentMode != FormViewMode.Insert)
{
//Initialize your default values here
}
}
【讨论】: