Set default value to detailsView

This is an ASP.net issue. User control is great, when it works. But you have to spend more time on it when some feature you are not sure how to. Like this one, how to set the default value for a new insert record on detailsView?

This won’t be a problemfot regular textbox, but for user control I had to look into the junk of properties. Finally, I did it. The trick is, “Convert this field into a TemplateField” in edit fields detailsView.

Original, the layout code was:

<asp:BoundField DataField=”OrganizationStatus” HeaderText=”OrganizationStatus”
SortExpression=”OrganizationStaus” />

After conerting:

<EditItemTemplate>
<asp:TextBox ID=”TextBox1″ runat=”server”
Text='<%# Bind(“OrganizationStatus”) %>’></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID=”TextBox1″ runat=”server”
Text='<%# CurrentOrganization.OrganizationStatus %>’></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID=”Label1″ runat=”server” Text='<%# Bind(“OrganizationStatus”) %>’></asp:Label>
</ItemTemplate>

Notice I’ve already changed the bind value in ‘InsertItemTemplate’ from Bind(“OrganizationStatus”) to CurrentOrganization.OrganizationStatus, which is a property of View. What a brilliant idea, MVP!

Credits to this post.

Other solutions like calling “DetailsView1.Rows[n].Cells[n].Text = xxx” in DetailsView1_PreRender didn’t fix the problem perfectly. They migaiccly changed the textbox to lable!

Advertisements