Default Values for DetailsView



New to ASP.NET so any help you can offer would be great! I have been
searching the web all day to try and figure out how I can add a
default value to a field on a DetailsView control.

I did find a few examples on the web and I understand what the code is
trying to do, but I cannot get it to work for me.

Here is my code:

<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
CellPadding="3" DataKeyNames="DefectID"
DataSourceID="SqlDataSource2" Font-Names="Arial"
Font-Size="8pt" GridLines="Horizontal" Height="50px"
Width="331px" OnPreRender="DetailsView1_PreRender">
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C"
HorizontalAlign="Right" />
<asp:BoundField DataField="DefectID"
HeaderText="DefectID" InsertVisible="False"
ReadOnly="True" SortExpression="DefectID" />
<asp:BoundField DataField="InspID" HeaderText="InspID"
SortExpression="InspID" />
<asp:BoundField DataField="DefectCode"
HeaderText="DefectCode" SortExpression="DefectCode" />
<asp:BoundField DataField="TBL" HeaderText="TBL"
SortExpression="TBL" />
<asp:BoundField DataField="SchematicRef"
HeaderText="SchematicRef" SortExpression="SchematicRef" />
<asp:BoundField DataField="DefectQty"
HeaderText="DefectQty" SortExpression="DefectQty" />
<asp:BoundField DataField="TestSymptom"
HeaderText="TestSymptom" SortExpression="TestSymptom" />
<asp:BoundField DataField="SymptomNumber"
HeaderText="SymptomNumber" SortExpression="SymptomNumber" />
<asp:BoundField DataField="Tech" HeaderText="Tech"
SortExpression="Tech" />
<asp:BoundField DataField="SerialNum"
HeaderText="SerialNum" SortExpression="SerialNum" />
<asp:BoundField DataField="RepairBy"
HeaderText="RepairBy" SortExpression="RepairBy" />
<asp:BoundField DataField="RetestedBy"
HeaderText="RetestedBy" SortExpression="RetestedBy" />
<asp:BoundField DataField="RetestDate"
HeaderText="RetestDate" SortExpression="RetestDate" />
<asp:BoundField DataField="PassFail"
HeaderText="PassFail" SortExpression="PassFail" />
<asp:BoundField DataField="RepairComments"
HeaderText="RepairComments" SortExpression="RepairComments" />
<asp:CommandField ShowEditButton="True"
ShowInsertButton="True" />

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -

Protected Sub DetailsView1_PreRender(ByVal sender As Object, ByVal
e As System.EventArgs)
If DetailsView1.CurrentMode = DetailsViewMode.Insert Then
Dim ThisControl As TextBox
ThisControl = DetailsView1.FindControl("PassFail")
ThisControl.Text = "Pass"
End If
End Sub

*Note - I have tried the code on the following events: PreRender,
DataBinding, and ItemInserting, ensuring that the asp: code is updated

Without the default being set for the PassFail field the update works

This is the error message I'm receiving (Line 83):

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:

Line 81: Dim ThisControl As TextBox
Line 82: ThisControl =
Line 83: ThisControl.Text = "Pass"
Line 84: End If
Line 85: End Sub

Basically what I am trying to do is be able to insert new detail
records. I get the error when I click the "Insert" link at the bottom
of the DetailsView control.

I am using Visual Web Developer 2005 Express Edition, but I do have a
full version of .NET Studio (2005).

Any tips or hints would be appreciated! Thanks in advance!


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