Here's the code for the CslaDatasource:
Protected
Sub CslaDataSource2_InsertObject(ByVal sender As Object, ByVal e As Csla.Web.InsertObjectArgs) Handles CslaDataSource2.InsertObject Dim obj As NdrnGender = GetNdrnGender()Csla.Data.DataMapper.Map(e.Values, obj,
New String() {"NDRNGenderID", "NDRNTimeStamp"})e.RowsAffected = SaveNdrnGender(obj)
End Sub Protected Sub CslaDataSource2_SelectObject(ByVal sender As Object, ByVal e As Csla.Web.SelectObjectArgs) Handles CslaDataSource2.SelectObject Try Dim Obj As Object = GetNdrnGender() If Obj IsNot Nothing Thene.BusinessObject = Obj
End If Catch ex As System.Security.SecurityExceptionValidationUtil.ApplyValidationToValidator(Master.FindControl(
"vldGeneral"), ex)ChangeView(Views.MainView)
End Try End SubHere's the code for the GetNdrnGender function:
Private
Function GetNdrnGender() As NdrnGender Dim BusnessObject As Object = Session("currentObject") If BusnessObject Is Nothing OrElse Not TypeOf BusnessObject Is NdrnGender Then If MainFormView.CurrentMode = FormViewMode.Edit ThenBusnessObject = NdrnGender.GetNdrnGender(
Me.LookupGridView.SelectedValue, Profile.TimeZone) ElseBusnessObject = NdrnGender.NewNdrnGender(Profile.TimeZone)
End IfSession(
"currentObject") = BusnessObject End If Return CType(BusnessObject, NdrnGender)And here's the code in the dataportal_Create method that defaults the date:
<CSLA.RunLocal()> _
Protected Overloads Sub DataPortal_Create(ByVal crit as TimeZoneCriteria) ' Set defaults for propertiesm_activeDate =
New SmartDate(TimeZoneSource.TZSClass.ConvertUtcToLocal(crit.TimeZone, Date.UtcNow))m_timeZone = crit.TimeZone
Me.ValidationRules.CheckRules() End SubWhen I stepped through debugging, the Default values for the date and timezone are being set properly, but those default values never get set to the textbox in the FormView control that is bound to the ActiveDate Property in the BO. If more code is needed, let me know and I'll be happy to provide it, but these are the areas where the the defaults should be set from.
Thanks,
Mike
I **think** that the SelectObject is called for the grid as a whole, not for a given row.
(I'm still learning, and that is what appears to be happening based on my work over the last few days. Haven't had time to work thru an experiment to prove my suspicion yet.)
I'm not sure whether forcing a databind on the grid would do anything useful or not.
Attempted to take your code and get it to work within a FormView as follows - any ideas on how I can set textbox values or checkbox values to default values:
Protected Sub frmPlants_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles frmPlants.ItemCreated
If (Me.frmPlants.CurrentMode = FormViewMode.Insert) Then
CType(frmPlants.FindControl("logIsActive"), TextBox).Text = 1 CType(frmPlants.FindControl("corporation_id"), TextBox).Text = Session("svCorpId") End If End SubSystem bombs when I run the code - getting the following message:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Any assistance will be appreciated.
DC
For anyone (like me) still struggling with this issue. I just discovered that the error is being thrown because the form must be in edit mode before a textbox value can be set. I'm stil experimenting but I guess you could set the mode to edit, set your values, then change it back to insert again.
I'm trying to figure out how to bring back entered values in a form after a rules violation during insert. I got it working in the ItemCreated event but still haven't figured out how I will initiate this event during the insert process.
If someone has developed a good process for performing validation, I'd really appreciate a little guidance here.
Due to some reasons the frmPlants_ItemCreated(..) is being called more then once while loading the page. In the first call it does not find the control but in the subsequent calls it find it and sets the defualt value even if it is in insert mode. So just add one 'if' statement before setting the default value
corporation_id as TextBox=frmPlants.FindControl("corporation_id")
if (corporation_id!=null) then
corporation_id.Text=Session("svCorpId")
end if
Getting default data into a grid or details view in insert mode is a pain. Microsoft really didn't think this scenario through very well. Look at PTWeb, because ProjectEdit.aspx does address this with code such as:
protected void DetailsView1_ItemCreated(object sender, EventArgs e)
{
if (DetailsView1.DefaultMode == DetailsViewMode.Insert)
{
Project obj = GetProject();
((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text = obj.Name;
((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text = obj.Started;
((TextBox)DetailsView1.Rows[3].Cells[1].Controls[0]).Text = obj.Ended;
((TextBox)DetailsView1.FindControl("TextBox1")).Text = obj.Description;
}
}
Hey there,
This is Joe from Abound Inc. I spent some time on this issue and found the simple solution. You simply need to create a template field for the field you want to set a default value for. In the template field ASP.Net is very flexible in that you can put any type of control in there whether it be a label, text box, checkbox etc... You set the value for that template field by making a call to a method you create in the code-behind file. I have a page called Clients.aspx where I have a gridview and detailsview. In this page I set it up so that when the user clicks to add a new client, the COMPANY_ID field is set to session variable and cannot be changed by the user.
In the Clients.aspx file the code for the template field looks like this:
<asp:TemplateField HeaderText="COMPANY_ID" SortExpression="COMPANY_ID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("COMPANY_ID") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# GetCompanyID() %>'></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("COMPANY_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Notice the call to GetCompanyID() in the InsertItemTemplate. That method is defined in the code-behind file (Clients.aspx.cs):
protected String GetCompanyID()
{
String sCompany = "";
if (Session["company_id"] != null)
{
sCompany = (string)Session["company_id"];
}
else
{
sCompany = "0";
}
return sCompany;
}
I hope this helps
Joe Werner
Abound Inc.
www.aboundweb.com
Copyright (c) Marimer LLC