testbox asp 2.0 databinding

testbox asp 2.0 databinding

Old forum URL: forums.lhotka.net/forums/t/3814.aspx


Wilfred posted on Tuesday, October 30, 2007

Hello,

I am having problems with a textbox in a detailsview, this textbox is bound to a property based on a nullable integer.

But when the textbox dets displayed and should be displaying no data, an empty string, it displays a zero.

I just don't get it, There is no reason for a zero to be displayed, the value in the database is Null upon reading (i have added a method to read the null value in the safedatareader).

The property get method returns an "" when it has no value.

But still i get this 0 on my textbox. When i place the row in editmode the zero is still there. And when I update the zero finds it's way to the database. When I empty the textbox and click update a null finds it's way back to the database.  I don't know where to look, i just can't get the zeroes to disappear, i was already thinking of binding the datasource myself.

 

You can see the datareader methods i use.

Public Function GetInt32Nullable(ByVal i As Integer) As Nullable(Of Integer)

If mDataReader.IsDBNull(i) Then

Return Nothing

Else

Return mDataReader.GetInt32(i)

End If

End Function

 

Public Function GetInt32Nullable(ByVal name As String) As Nullable(Of Integer)

Dim index As Integer = Me.GetOrdinal(name)

Return Me.GetInt32(index)

End Function

 

Thank's for the advice

 

Wilfred

RockfordLhotka replied on Wednesday, October 31, 2007

How is your public property declared? If it is declared as a numeric type, data binding is probaby putting the 0 in there because that is the default numeric value.

You might need to expose the property as type string so data binding understands that "" is a valid value?

If you were in WinForms or Wpf there are formatting/conversion events you can handle to fix this at the UI layer (leaving the object's property as a numeric type). I'm not sure if there's any counterpart in Web Forms - perhaps a more web-savvy person knows?

DavidDilworth replied on Wednesday, October 31, 2007

What is code for your property like?  Is the property a String?

And are you using an MS standard textbox control?  It's not a 3rd party control is it?

Wilfred replied on Wednesday, October 31, 2007

Hello,

I have used integer for the 'Get part' too, but it was also a problem, as for the s'Set part' i know i have left some code out, but when opening a record containing a Null  value (for this property)  in SQL SERVER 2005, a zero gets displayed. In the cod below i use String.Empty, but i also tried "". It makes no difference.

The textbox is MS, I even removed the validator, but to no avail.

 

dim mFunctioneleTaalvaardigheid as Nullable (of Integer)

Public Property FunctioneleTaalvaardigheid() As String

<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _

Get

CanReadProperty(True)

If mFunctioneleTaalvaardigheid.HasValue Then

Return mFunctioneleTaalvaardigheid.ToString

Else

Return String.Empty

End If

End Get

<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _

Set(ByVal value As String)

CanWriteProperty(True)

mFunctioneleTaalvaardigheid = Cint(value)

 PropertyHasChanged()

End Set

End Property

Wilfred replied on Friday, November 02, 2007

Anybody

DavidDilworth replied on Friday, November 02, 2007

Can you post the relevant code from the ASPX that shows the control you are binding to and the object data source?

Wilfred replied on Friday, November 02, 2007

Actually I don't really know what you want, but I added some code, and some markup. The real detailsview has more fields, and the object as well. But i'have added the field that is zero, when it should be blank.

I hope you see more than I can. Thank's for your time.

Bye

Wilfred

Protected Sub LeerlingASPVDataSource_SelectObject( _

ByVal sender As Object, ByVal e As Csla.Web.SelectObjectArgs) _

Handles LeerlingASPVDataSource.SelectObject

Session("currentObject") = Nothing

If Csla.ApplicationContext.User.Identity.IsAuthenticated Then

e.BusinessObject = GetLeerlingASPVRapporten(CInt(Session("llid")))

Session("currentObject") = e.BusinessObject

End If

End Sub

Private Function GetLeerlingASPVRapporten(ByVal leerlingid As Integer) As Rapporten.LeerlingASPVRapporten

Dim businessObject As Object = Session("currentObject")

If businessObject Is Nothing OrElse _

Not TypeOf businessObject Is Rapporten.LeerlingASPVRapporten Then

businessObject = Rapporten.LeerlingASPVRapporten.GetLeerlingASPVRapporten(leerlingid)Session("currentObject") = businessObject

End If

Return CType(businessObject, Rapporten.LeerlingASPVRapporten)

End Function

 

<csla:CslaDataSource ID="LeerlingASPVDataSource" runat="server"

TypeName="Rapporten.LeerlingASPVRapporten" TypeAssemblyName="Rapporten">

</csla:CslaDataSource>

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="LeerlingASPVDataSource" DataKeyNames="Id" Height="50px" Width="125px" AllowPaging="True" EmptyDataText="Nvt">

<Fields>

<asp:TemplateField HeaderText="Functionele taalvaardigheid" SortExpression="FunctioneleTaalvaardigheid" ConvertEmptyStringToNull="False">

<EditItemTemplate>

<asp:RangeValidator ID="RangeValidator1" Type="Integer"

ControlToValidate="TextBox2"

MaximumValue="10"

MinimumValue="0"

runat="server" ErrorMessage="Moet tussen 0 en 10 zijn"></asp:RangeValidator>

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FunctioneleTaalvaardigheid") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label2" runat="server" Text='<%# Bind("FunctioneleTaalvaardigheid") %>' ></asp:Label>

</ItemTemplate>

</asp:TemplateField>

</Fields>

</asp:DetailsView>

 

Wilfred replied on Saturday, November 03, 2007

Problem is out of the way now, forgot to change something in the datareader.

 

Thank's for the attention.

 

bye

Copyright (c) Marimer LLC