Default Values for DetailsView

Discussion in 'ASP .Net' started by esi, Apr 28, 2008.

  1. esi

    esi Guest

    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"
    AutoGenerateRows="False"
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
    BorderWidth="1px"
    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" />
    <Fields>
    <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" />
    </Fields>

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

    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
    correctly.

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

    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 =
    DetailsView1.FindControl("PassFail")
    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!
     
    esi, Apr 28, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nirmal Singh
    Replies:
    1
    Views:
    1,342
    Richard Humphrey
    Oct 13, 2010
  2. David H
    Replies:
    0
    Views:
    455
    David H
    Dec 22, 2008
  3. mt
    Replies:
    1
    Views:
    312
    Phillip Williams
    Apr 10, 2006
  4. Replies:
    0
    Views:
    276
  5. Morris Neuman

    How to show initial default values in a detailsview form

    Morris Neuman, Jan 20, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    328
    Steven Cheng[MSFT]
    Feb 15, 2007
Loading...

Share This Page