Binding a nullable int to an asp:TextBox

Posted by Slauma on Stack Overflow See other posts from Stack Overflow or by Slauma
Published on 2010-03-25T22:35:09Z Indexed on 2010/03/26 4:13 UTC
Read the original article Hit count: 478

Filed under:
|
|

I have a property int? MyProperty as a member in my datasource (ObjectDataSource). Can I bind this to a TextBox, like

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

Basically I want to get a null value displayed as blank "" in the TextBox, and a number as a number. If the TextBox is blank MyProperty shall be set to null. If the TextBox has a number in it, MyProperty should be set to this number.

If I try it I get an exception: "Blank is not a valid Int32".

But how can I do that? How to work with nullable properties and Bind?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about databinding