How do I set a value in the insert template of a details view control?

Hi Experts.  This should not be so difficult but I am tearing my hair out.  The basic concept is: UserID is stored in a hidden field when page is loaded.  Dropdown list generates a gridview. Select in gridview generates a details view for that record.  All is fine until I try to set a couple of defaults in the insert template of the details view.  I believe the error is saying the control is not found.  Where am I missing something?  Thanks.
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        DetailsView1.Visible = True
        Dim specID As Integer = DropDownList1.SelectedValue
        Dim spDDL As DropDownList = CType(DetailsView1.FindControl("DropDownList7"), DropDownList)
        spDDL.SelectedValue = specID
 
        Dim tbUpdater As TextBox = CType(DetailsView1.FindControl("updatedByTextBox"), TextBox)
        tbUpdater.Text = hfpersonID.Value
    End Sub

Open in new window

AJ0718Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Juan_BarreraCommented:
Hi there,
What's the error you are getting? Which control is not being found?
0
AJ0718Author Commented:
The error message is "Object reference not set to an instance of an object" and it stops on this line of code: "spDDL.SelectedValue = specID".  When I comment out the two lines of code related to the dropdown value, it then chokes on "tbUpdater.Text = hfpersonID.Value".  By commenting both, the page renders just fine so I would say it is not finding the controls that are inside the insert templates of DetailsView.  I presume this is because they are children of the DetailsView control but I don't know how to reference them.

0
Juan_BarreraCommented:
Yes, it's very likely.
I've just noticed the line "DetailsView1.Visible = True" in your code. Do you have the DetailsView1 not visible before selecting something in your grid?
If so, that's probably why you can't find the controls.
Also, I don't see where you are rebinding your DetaisView. Try to do it before finding the controls and see what happens.

0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

AJ0718Author Commented:
Still no luck.  I am making the DetailsView visible when the GridView is rendered.  On select I then rebind my DetailsView and try to find the controls in the templates.  Same error.  I wonder if rather than doing this when the cotrol first renders if I should be doing it on a mode change?
0
Juan_BarreraCommented:
Ok, can you post your DetailsView markup please?
0
AJ0718Author Commented:
Here you go...

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        CellPadding="4" DataKeyNames="recID" DataSourceID="dsRecordDetail" 
        ForeColor="#333333" GridLines="None" Height="50px" Width="238px">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <EmptyDataTemplate>
            Select a record to view details.
        </EmptyDataTemplate>
        <Fields>
            <asp:BoundField DataField="recID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="recID" Visible="False" />
            <asp:TemplateField HeaderText="Species" SortExpression="speciesID">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="dsSpecies" 
                        DataTextField="SpeciesName" DataValueField="speciesID" 
                        SelectedValue='<%# Bind("speciesID") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList7" runat="server" DataSourceID="dsSpecies" 
                        DataTextField="SpeciesName" DataValueField="speciesID" 
                        SelectedValue='<%# Bind("speciesID") %>'>
                    </asp:DropDownList>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("speciesID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="RecordNo" HeaderText="Record No" 
                SortExpression="RecordNo" />
            <asp:BoundField DataField="obsDate" HeaderText="Date(s)" 
                SortExpression="obsDate" />
            <asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
            <asp:TemplateField HeaderText="County" SortExpression="County">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="dsCounty" 
                        DataTextField="coName" DataValueField="coNo" 
                        SelectedValue='<%# Bind("County") %>'>
                    </asp:DropDownList>
                    
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="dsCounty" 
                        DataTextField="coName" DataValueField="coNo">
                    </asp:DropDownList>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("County") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Citation" SortExpression="Citation">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Citation") %>' 
                        MaxLength="500" Rows="4" TextMode="MultiLine" Width="300px"></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Citation") %>' 
                        MaxLength="500" Rows="4" TextMode="MultiLine" Width="300px"></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Citation") %>' 
                        Width="300px"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Notes" SortExpression="Notes">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Notes") %>' 
                        MaxLength="250" Rows="3" TextMode="MultiLine" Width="300px"></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Notes") %>' 
                        MaxLength="250" Rows="3" TextMode="MultiLine" Width="300px"></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Notes") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Updated By" SortExpression="updatedBy">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("updatedBy") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("updatedBy") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("updatedBy") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <EditItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="True" 
                        CommandName="Update" Text="Update" />
                    &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" 
                        CommandName="Cancel" Text="Cancel" />
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="True" 
                        CommandName="Insert" Text="Insert" />
                    &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" 
                        CommandName="Cancel" Text="Cancel" />
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="False" 
                        CommandName="Edit" Text="Edit" />
                    &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" 
                        CommandName="New" Text="New" />
                    &nbsp;<asp:Button ID="Button3" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete" />
                    <ajaxToolkit:ConfirmButtonExtender ID="Button3_ConfirmButtonExtender" 
                        runat="server" ConfirmText="Are you sure you want to delete this record?" 
                        Enabled="True" TargetControlID="Button3">
                    </ajaxToolkit:ConfirmButtonExtender>
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:DetailsView>

Open in new window

0
Juan_BarreraCommented:
What happens if you try this? Do you get a null object or it finds the label?

Dim label1 As Label = CType(DetailsView1.FindControl("Label1"), Label)
      

Open in new window

0
AJ0718Author Commented:
It must have found the label because it didn't throw an exception.  Actually, just for fun, I set the text for label1 to "12345" and that worked fine.
0
Juan_BarreraCommented:
Then it means that you need to find the DropDown and the TextBox in the ItemInserting event, which is maybe unnecessary, as you have the "e" parameter witth an e.Values collection that you can query.
0
AJ0718Author Commented:
That was making some sense so I tried it with an update where I grabbed the logged-in userID for my textbox while updating.  Got the following:
Line 63:     Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Line 64:         Dim tbUpdater As TextBox = CType(FormView1.FindControl("TextBox5"), TextBox)
Line 65:         tbUpdater.Text = hfpersonID.Value.ToString()
Line 66:     End Sub

It still blows up when trying to set a value.  Commenting out  Line 65 allowed the update to happen so it must have found the control.  Maybe I am making this too hard - maybe the values should be set in the templates somehow rather than in the code behind?
0
Juan_BarreraCommented:
The fact that it didn't blow up by commenting out line 65 doesn't mean that it found the control. Almost for sure tbUpdater is Nothing after trying to find it, and that's why line 65 crashes.:
I'm not sure if this is a typo, but it says:

  Dim tbUpdater As TextBox = CType(FormView1.FindContro
l("TextBox5"), TextBox)

Are you sure you are looking for the control un the right place?

Have a look at this before trying something else.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AJ0718Author Commented:
Thanks - that was one too many copy and pastes.  Now line 65 does not crash but the correct userID doesn't seem to pass anywhere.  I can update bound fields and manually change the 14 to 20 for a userID but am still not passing a different default value to the database.  Will play around a bit but I think you have me on the right track.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.