我有一个formview控件,在ItemCreated事件中,我用默认值“启动”了一些字段。
但是,当我尝试使用formview插入时,在ItemInserting事件被调用之前,由于某种原因,它首先调用ItemCreated。这会导致字段在插入发生之前被缺省值覆盖。
如何让它在ItemInserting事件之前不调用ItemCreated事件?
发布于 2009-11-06 13:20:04
您需要使用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的帖子。
发布于 2009-11-06 02:54:46
您不能更改事件的触发顺序。但是,您可能应该包装在!IsPostBack中设置默认值的代码,这样它就不会重置您的值,例如:
protected void FormView_ItemCreated(Object sender, EventArgs e)
{
if(!IsPostBack)
{
//Set default values ...
}
}发布于 2009-11-06 10:36:54
请尝试检查窗体视图的CurrentMode属性。
void FormView_ItemCreated(object sender, EventArgs e)
{
if (FormView.CurrentMode != FormViewMode.Insert)
{
//Initialize your default values here
}
}https://stackoverflow.com/questions/1682700
复制相似问题