Emad-ud-deen
Emad-ud-deen

Reputation: 4864

Populating a variable with DetailsView textbox data value

When an ASP.Net DetailsView data is first displayed, can you tell me how to populate the variable shown in this coding?

I already tried this in the code-behind file but was shown an error that:

Object reference not set to an instance of an object.

This is the coding:

Protected Sub DetailsViewDetails_DataBound(sender As Object, e As EventArgs) Handles DetailsViewDetails.DataBound

    Dim txtOriginalRegistrationFee As TextBox

    If DetailsViewDetails.CurrentMode = DetailsViewMode.Edit Then
        txtOriginalRegistrationFee = FindControl("TextBoxRegistrationFee")

        If String.IsNullOrEmpty(txtOriginalRegistrationFee.Text) = False Then
            MsgBox(txtOriginalRegistrationFee)
        End If
    End If
End Sub

This is from the aspx file:

<asp:TemplateField HeaderText="RegistrationFee" SortExpression="RegistrationFee">
   <EditItemTemplate>
      <asp:TextBox ID="TextBoxRegistrationFee" runat="server" Text='<%# Eval("RegistrationFee") %>'></asp:TextBox>
   </EditItemTemplate>

   <InsertItemTemplate>
      <asp:TextBox ID="TextBoxRegistrationFee" runat="server" Text='<%# Bind("RegistrationFee") %>'></asp:TextBox>
   </InsertItemTemplate>

   <ItemTemplate>
      <asp:Label ID="LabelRegistrationFee" runat="server" Text='<%# Bind("RegistrationFee", "{0:c}") %>'></asp:Label>
   </ItemTemplate>

   <ItemStyle ForeColor="Blue" />
</asp:TemplateField>

* Update *

I tried using this coding updated based on your help but still get the "Object reference not set to an instance of an object." error when clicking the edit button of the DetailsView.

Upvotes: 1

Views: 1557

Answers (2)

nickles80
nickles80

Reputation: 1101

The FindControl function needs a control to search otherwise it will be searching the page (or content) level controls.

Try

txtOriginalRegistrationFee = DetailsViewDetails.FindControl("TextBoxRegistrationFee")

By the way your line with the MsgBox function will not work either. MsgBox is for windows forms and will not work on the web. You must use javascript for that type of functionality. Plus, that function takes a string, not a control.

Upvotes: 1

andleer
andleer

Reputation: 22578

I don't work in VB so bear with me.... The template is only rendered if your details view is in insert mode.

Dim txtOriginalRegistrationFee As TextBox

If DetailsViewDetails.CurrentMode = DetailsViewMode.Insert Then
    txtOriginalRegistrationFee = FindControl("TextBoxRegistrationFee")

    If String.IsNullOrEmpty(txtOriginalRegistrationFee.Text) = False Then
        MsgBox(txtOriginalRegistrationFee)
    End If
End If

Upvotes: 0

Related Questions