but this particular textbox is often forgotten. I want a default value (say "10"). I made the field "not null" with a default value of "10" but it insists on throwing an error when I do not enter anything in the field.
Another problem : the same form, and there is a checkbox :
If your DataBase field does not except null, then you will most certainly receive an error. Here's an example of how to use the FormView.ItemCreated event to set your defaults:
Along the same lines... if my data is coming from an ObjectDataSource (pulling its data from a nice, self-contained Class within the App_Code folder), how can I specify default values for the Insert view of the FormView without hard-encoding them into the
Code Behind?
If you want to use the Bind statement in your TextBoxes, I don't know of any way to do this without coding in the code-behind. If you wanted to bind your controls manually, you could declaratively set the Text property of your TextBox control within the
InsertTemplate.
Protected Sub fvCar_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvCar.ItemCreated
If fvCar.CurrentMode = FormViewMode.Insert Then
Dim YearTextBox As TextBox = DirectCast(fvCar.FindControl("YearTextBox"), TextBox)
Dim IsRunningCheckBox As CheckBox = DirectCast(fvCar.FindControl("IsRunningCheckBox"), CheckBox)
If YearTextBox IsNot Nothing Then
YearTextBox.Text = "2007"
End If
If IsRunningCheckBox IsNot Nothing Then
IsRunningCheckBox.Checked = True
End If
End If
End Sub
I'm interested in this thread. I got a similar code-behind recommendation in another thread from someone else. It appears that setting default values, at least when using datasets, requires the use of codebehind. My question: What is the purpose of the DefaultValue
property for fields in a dataset datatable. The help text that appears when this property is selected appears to indicate that it is used for inserting default text, but my own tests show it doesn't work as expected. Is this property meant for some other purpose?
I guess I'm not sure what you mean as I've never heard of a DefaultValue for use in a DataSet. The only DefaultValue property I know of is used with parameters.
cosmo0
Member
101 Points
39 Posts
Default value for textbox or checkbox in formview
Jan 18, 2007 03:48 PM|LINK
Hello !
Here is a problem I encounter often : I have a field (a textbox for example), like this :
<asp:FormView ID="FormContenus" runat="server" DataKeyNames="contenu_id" DataSourceID="SqlContenusMAJ" DefaultMode="Insert" Width="100%"> <InsertItemTemplate> [snip] <asp:TextBox ID="contenu_ordreTextBox" runat="server" Text='<%# Bind("contenu_ordre") %>' Columns="3" MaxLength="3"></asp:TextBox> [snip] <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insérer"></asp:Button> </InsertItemTemplate> </asp:FormView>but this particular textbox is often forgotten. I want a default value (say "10"). I made the field "not null" with a default value of "10" but it insists on throwing an error when I do not enter anything in the field.
Another problem : the same form, and there is a checkbox :
I want it checked by default. Even if the database field is "true" by default, if the box is not checked, it puts "false"....
How can I put default values and check by default ?
Thanks !
default values for fields checkbox textbox default value
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Default value for textbox or checkbox in formview
Jan 18, 2007 06:51 PM|LINK
If your DataBase field does not except null, then you will most certainly receive an error. Here's an example of how to use the FormView.ItemCreated event to set your defaults:
ASPX
<asp:formview id="fvCar" runat="server" datasourceid="sdsCar" defaultmode="Insert" onitemcreated="fvCar_ItemCreated"> <insertitemtemplate> Make: <asp:textbox id="MakeTextBox" runat="server" text='<%# Bind("Make") %>' /><br /> Model: <asp:textbox id="ModelTextBox" runat="server" text='<%# Bind("Model") %>' /><br /> Color: <asp:textbox id="ColorTextBox" runat="server" text='<%# Bind("Color") %>' /><br /> Year: <asp:textbox id="YearTextBox" runat="server" text='<%# Bind("Year") %>' /><br /> IsRunning: <asp:checkbox id="IsRunningCheckBox" runat="server" checked='<%# Bind("IsRunning") %>' /><br /> <asp:linkbutton id="InsertButton" runat="server" causesvalidation="True" commandname="Insert" text="Insert" /> <asp:linkbutton id="InsertCancelButton" runat="server" causesvalidation="False" commandname="Cancel" text="Cancel" /> </insertitemtemplate> </asp:formview> <asp:sqldatasource id="sdsCar" runat="server" connectionstring="<%$ ConnectionStrings:ASPNETDBConnectionString %>" insertcommand="INSERT INTO Cars(Make, Model, Color, Year, IsRunning) VALUES (@Make, @Model, @Color, @Year, @IsRunning)" selectcommand="SELECT Cars.* FROM Cars"> <insertparameters> <asp:parameter name="Make" /> <asp:parameter name="Model" /> <asp:parameter name="Color" /> <asp:parameter name="Year" /> <asp:parameter name="IsRunning" /> </insertparameters> </asp:sqldatasource>CODE-BEHIND
Microsoft MVP - ASP.NET
cosmo0
Member
101 Points
39 Posts
Re: Default value for textbox or checkbox in formview
Jan 18, 2007 09:07 PM|LINK
scarecrow-ry...
Member
29 Points
14 Posts
Re: Default value for textbox or checkbox in formview
Nov 27, 2007 07:28 PM|LINK
Along the same lines... if my data is coming from an ObjectDataSource (pulling its data from a nice, self-contained Class within the App_Code folder), how can I specify default values for the Insert view of the FormView without hard-encoding them into the Code Behind?
ObjectDataSource FormView
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Default value for textbox or checkbox in formview
Nov 27, 2007 07:43 PM|LINK
If you want to use the Bind statement in your TextBoxes, I don't know of any way to do this without coding in the code-behind. If you wanted to bind your controls manually, you could declaratively set the Text property of your TextBox control within the InsertTemplate.
Microsoft MVP - ASP.NET
Foyman1973
Member
19 Points
13 Posts
Re: Default value for textbox or checkbox in formview
Apr 02, 2008 11:13 PM|LINK
I don't suppose someone could provide the same example in VB please?
-Jason
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Default value for textbox or checkbox in formview
Apr 03, 2008 12:09 PM|LINK
ASPX is the same, here's the code-behind.
Protected Sub fvCar_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvCar.ItemCreated If fvCar.CurrentMode = FormViewMode.Insert Then Dim YearTextBox As TextBox = DirectCast(fvCar.FindControl("YearTextBox"), TextBox) Dim IsRunningCheckBox As CheckBox = DirectCast(fvCar.FindControl("IsRunningCheckBox"), CheckBox) If YearTextBox IsNot Nothing Then YearTextBox.Text = "2007" End If If IsRunningCheckBox IsNot Nothing Then IsRunningCheckBox.Checked = True End If End If End SubMicrosoft MVP - ASP.NET
Foyman1973
Member
19 Points
13 Posts
Re: Default value for textbox or checkbox in formview
Apr 03, 2008 02:50 PM|LINK
Thanks! I was having a syntax issue.... brain and hands were talking on different ports!
-Jason
keuler
Member
29 Points
159 Posts
Re: Default value for textbox or checkbox in formview
Apr 13, 2008 07:03 PM|LINK
ecbruck-
I'm interested in this thread. I got a similar code-behind recommendation in another thread from someone else. It appears that setting default values, at least when using datasets, requires the use of codebehind. My question: What is the purpose of the DefaultValue property for fields in a dataset datatable. The help text that appears when this property is selected appears to indicate that it is used for inserting default text, but my own tests show it doesn't work as expected. Is this property meant for some other purpose?
thanks!
-Kurt
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Default value for textbox or checkbox in formview
Apr 14, 2008 12:16 PM|LINK
I guess I'm not sure what you mean as I've never heard of a DefaultValue for use in a DataSet. The only DefaultValue property I know of is used with parameters.
Microsoft MVP - ASP.NET