Link to home
Start Free TrialLog in
Avatar of AJ0718
AJ0718

asked on

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

Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

Hi there,
What's the error you are getting? Which control is not being found?
Avatar of AJ0718
AJ0718

ASKER

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.

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.

Avatar of AJ0718

ASKER

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?
Ok, can you post your DetailsView markup please?
Avatar of AJ0718

ASKER

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

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

Avatar of AJ0718

ASKER

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.
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.
Avatar of AJ0718

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AJ0718

ASKER

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.