Here is a simple question that I see pop up on forums from time to time and the solution is brief and fairly easy to expand upon.
How do I set a default value for a bound control?
As you can see, the question looks like it is very important when adorned in italics. Onto the quick win…
First, build your input control without an explicit bind:
<asp:FormView runat="server" ID="fvData" DataSource="odsData" DataKeyNames="ID">
<EditTemplate>
<%-- This would normally contain your Bind("Field") --%>
<asp:TextBox runat="server" id="txtData" Text="0.0" />
</EditTemplate>
</asp:FormView>It feels good to break convention. Next, hook into the Updating event of your data source; in this case an ObjectDataSource is used. For an ODS, you can grab the first InputParameter, which is your data item from the form.
protected void odsData_Updating(object sender, ObjectDataSourceMethodEventArgs e) { ObjectType obj = e.InputParameters[0]; if ( obj != null ) obj.Field = String.IsNullOrEmpty(txtData.Text) ? Double.Parse(txtData.Text) : 0D; }
Use whatever validation methods you wish, either client or server side, and you are all set.
I think I need a nap. Until next time!