How to show initial default values in a detailsview form

M

Morris Neuman

I am trying to display default values in bound fields on a detailsview form
when the page is first displayed for insterting a new record. The user can
then override the defaults and click insert to actually add the record. I
currently have the insert command with the corresponding desired default
values. So if the user does not enter anything in the field the record gets
added with the correct default value. However these default values are not
displayed when the page is first displayed.

How do I set the initial default values so it shows on the page prior to
insert?
 
S

Steven Cheng[MSFT]

Hello Morris,

As for the setting default/inital value for TextBox in the DetailsView's
Insert Mode. I would suggest you consider converting the certain Field into
TemplateField. Thus, the TextBox has a definte ID we can used to reference
it in code. And we can put some code in the DetailsView.ItemCreated Event,
if the "CurrentMode==Insert", you can find that TextBox and set a initial
value on it. Here is a simple test page fragment demonstrate on this:

======================
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="ProductCategoryID"
DataSourceID="SqlDataSource1" Height="50px" Width="125px"
OnItemCreated="DetailsView1_ItemCreated">
<Fields>
<asp:BoundField DataField="ProductCategoryID"
HeaderText="ProductCategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductCategoryID" />
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("Name") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
==========================

==============
protected void DetailsView1_ItemCreated(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
{
TextBox txt = DetailsView1.FindControl("TextBox1") as TextBox;

if (txt != null)
{
txt.Text = "Default text value.....";
}
}
}
===================

Hope this helps some. If you have any other concerns here, please feel free
to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hello Morris,

Have you got any progress on this issue? Does the suggestion in my last
reply helps some?

If you have any further questions, please feel free to let me know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Morris Neuman

Hi Steven,

Just reading the posts. Very sorry that I did not get back to you. We plan
on creating a table for the default values and use it to populate the insert
form.

Thanks for your help and sorry for not getting back to you sooner.
 
S

Steven Cheng[MSFT]

Hi Morris,

Glad to hear from you. No problem, please feel free to post here at your
convenience.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top