bentos wrote:
Hello,
The form data item is null at this point (maybe require another event handler?), here is the code as it stands
It could be that it is null, because you're inserting an item, and not updating one.
Basically, the password field in this case cannot be null, I suppose I could work around by setting a hidden field to empty string, then after the insert reloading the entity and updating the required fields, but you would think we would be able to access the entity object at some point to save this messing around?
Thank you for your help,
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
LLBLGenProDataSourceExist.FilterToUse = new PredicateExpression(
(CustomerFields.Email == e.Values["Email"]));
LLBLGenProDataSourceExist.Select();
if (LLBLGenProDataSourceExist.EntityCollection.Count == 1)
{
Panel1.Visible = true;
e.Cancel = true;
}
else
{
Panel1.Visible = false;
CustomerEntity CE = (CustomerEntity)FormView1.DataItem;
CE.Password = new PasswordGenerator().Generate();
}
}
I see you execute a Select() inside the ItemInserting event handler, I don't think that's wise. However I do understand why you're doing this, because otherwise the form won't show up when your collection is empty. You can show the insert template by doing:
protected void _switchViewButton_Click(object sender, EventArgs e)
{
switch(_downloadItemEditFormView.CurrentMode)
{
case FormViewMode.Edit:
_downloadItemEditFormView.ChangeMode(FormViewMode.Insert);
break;
case FormViewMode.Insert:
_downloadItemEditFormView.ChangeMode(FormViewMode.Edit);
break;
}
}
Here I show an event handler for the button which flips the formview state: update or insert.
You could grab the last entity in the collection as the entity which was added. You could also subscribe to the LLBLGenProDataSourceExist.EntityCollection.EntityAdded event and handle it there.
You could also pre-set the password in an override of OnInitialized in the entity itself. When inserting data through a formview, the entity isn't there until the form is submitted. Then the data is passed to the datasourcecontrol, which calls ExecuteInsert of the datasourcecontrol, and in there a new entity is created, filled with the data and added to the entitycollection.