Search code examples
vb.netdetailsviewfindcontrolhiddenfield

Could not find control in ControlParameter


My page has a DetailsView that has a hidden field in it that gets referenced by an SQLDataSource to populate a different field in the same DetailsView. I cannot get the codebehind to find the Control, no matter how many different ways I try. I really need to be able to show the TEXT field which is associated with the dsPicklist SqlDataSource. I have marked the code that is causing problems. I would really appreciate some help in trying to display this information.

<asp:Label ID="Label1" runat="server" Text="Select Survey:"></asp:Label>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
    DataSourceID="dsSurvey" DataTextField="SurveyName" DataValueField="SurveyID">
</asp:DropDownList>

<asp:DetailsView ID="dvSurveyQuestions" runat="server" AllowPaging="True" 
AutoGenerateRows="False" CellPadding="4" DataKeyNames="QuestionID" 
DataSourceID="dsSurveyQuestions" ForeColor="#333333" GridLines="None" Height="50px" 
Width="100%">
<Fields>
    <asp:BoundField DataField="QuestionNum" HeaderText="Question Number" 
        SortExpression="QuestionNum" />

    **<asp:TemplateField>
        <ItemTemplate>
            <asp:HiddenField ID="hiddenQuestionID" runat="server" 
            Value='<%# Bind("QuestionID") %>'>
            </asp:HiddenField>
        </ItemTemplate>
    </asp:TemplateField>**

    <asp:TemplateField HeaderText="Question Type" SortExpression="QType">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownList4" runat="server" 
                SelectedValue='<%# Bind("QType") %>'>
                <asp:ListItem Value="Picklist">Picklist</asp:ListItem>
                <asp:ListItem Value="Text">Text</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <InsertItemTemplate>
            <asp:DropDownList ID="DropDownList5" runat="server" 
                SelectedValue='<%# Bind("QType") %>'>
                <asp:ListItem Value="Picklist">Picklist</asp:ListItem>
                <asp:ListItem Value="Text">Text</asp:ListItem>
            </asp:DropDownList>
        </InsertItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblQType" runat="server" Text='<%# Bind("QType") %>'>
            </asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Question" SortExpression="Question">
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
            Text='<%# Bind("Question") %>'>
            </asp:TextBox>
        </EditItemTemplate>
        <InsertItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
            Text='<%# Bind("Question") %>'>
            </asp:TextBox>
        </InsertItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblQuestion" runat="server" Text='<%# Bind("Question") %>'>    
            </asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Answer" SortExpression="PicklistID">
        <EditItemTemplate>
        <!-- put something here after ItemTemplate testing -->
        </EditItemTemplate>
        <InsertItemTemplate>
        <!-- put something here after ItemTemplate testing -->
        </InsertItemTemplate>
        <ItemTemplate>
            <asp:HiddenField ID="hiddenPicklistID" runat="server"  
            Value='<%# Bind("PicklistID") %>' />
            <asp:BulletedList ID="blText" runat="server" DataSourceID="dsPicklist" 
            DataTextField="TEXT">
            </asp:BulletedList>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Answer Type" SortExpression="AnswerType">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownList2" runat="server" 
                SelectedValue='<%# Bind("AnswerType") %>'>
                <asp:ListItem Value="S">Single Choice (radio button)</asp:ListItem>
                <asp:ListItem Value="M">Multiple Choices (checkboxes)</asp:ListItem>
                <asp:ListItem Value="T">Text (textbox)</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <InsertItemTemplate>
            <asp:DropDownList ID="DropDownList3" runat="server" 
                SelectedValue='<%# Bind("AnswerType") %>'>
                <asp:ListItem Value="S">Single Choice (radio button)</asp:ListItem>
                <asp:ListItem Value="M">Multiple Choices (checkboxes)</asp:ListItem>
                <asp:ListItem Value="T">Text (textbox)</asp:ListItem>
            </asp:DropDownList>
        </InsertItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblAnswerType" runat="server" Text='<%# Bind("AnswerType") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:BoundField DataField="Subsequence" HeaderText="Subsequence" 
        SortExpression="Subsequence" />
    <asp:CheckBoxField DataField="Active" HeaderText="Active" 
            SortExpression="Active" />
    <asp:CheckBoxField DataField="Question_Locked" HeaderText="Question Locked" 
            SortExpression="Question_Locked" />
    <asp:BoundField DataField="QHelp" HeaderText="Question Help" SortExpression="QHelp" />
    <asp:BoundField DataField="Script" HeaderText="Script" 
        SortExpression="Script" />
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
        ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="dsPicklist" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
    SelectCommand="SELECT p.TEXT 
                   FROM PICKLIST p 
                   JOIN C_Survey_Questions c 
                   ON p.PICKLISTID = c.PicklistID 
                   AND c.QuestionID = @QuestionID 
                   AND c.SurveyID = @SurveyID 
                   WHERE p.PICKLISTID IS NOT NULL 
                   AND c.PicklistID IS NOT NULL">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="SurveyID" 
            PropertyName="SelectedValue" Type="Int32" />
        **<asp:ControlParameter ControlID="hiddenQuestionID" Name="QuestionID" 
            PropertyName="SelectedValue" Type="Int32" />**
    </SelectParameters>
</asp:SqlDataSource>

<asp:SqlDataSource ID="dsSurvey" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
    SelectCommand="SELECT [SurveyID], [SurveyName] 
                   FROM [C_Survey] 
                   ORDER BY [SurveyName]">
</asp:SqlDataSource>

<asp:SqlDataSource ID="dsSurveyQuestions" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
    DeleteCommand="DELETE FROM [C_Survey_Questions] WHERE [QuestionID] = @QuestionID" 
    InsertCommand="INSERT INTO [C_Survey_Questions] ([SurveyID], [Question], [QType],
                   [PickListID], [QuestionNum], [Subsequence], [Active], [Script], 
                   [Question_Locked], [QHelp], [Createdate], [Modifydate],
                   [AnswerType]) 
                   VALUES (@SurveyID, @Question, @QType, @PickListID, @QuestionNum, 
                   @Subsequence, @Active, @Script, @Question_Locked, @QHelp, getdate(), 
                   getdate(), @AnswerType)" 
    SelectCommand="SELECT * FROM C_Survey_Questions WHERE SurveyID = @SurveyID" 
    UpdateCommand="UPDATE [C_Survey_Questions] 
                   SET [SurveyID] = @SurveyID, [Question] = @Question,
                   [QType] = @QType, [PickListID] = @PickListID, 
                   [QuestionNum] = @QuestionNum, [Subsequence] = @Subsequence, 
                   [Active] = @Active, [Script] = @Script, 
                   [Question_Locked] = @Question_Locked, 
                   [QHelp] = @QHelp, [Modifydate] = getdate(), 
                   [AnswerType] = @AnswerType WHERE [QuestionID] = @QuestionID">
    <DeleteParameters>
        <asp:Parameter Name="QuestionID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="SurveyID" Type="Int32" />
        <asp:Parameter Name="Question" Type="String" />
        <asp:Parameter Name="QType" Type="String" />
        <asp:Parameter Name="PickListID" Type="String" />
        <asp:Parameter Name="QuestionNum" Type="Int32" />
        <asp:Parameter Name="Subsequence" Type="Int32" />
        <asp:Parameter Name="Active" Type="Boolean" />
        <asp:Parameter Name="Script" Type="String" />
        <asp:Parameter Name="Question_Locked" Type="Boolean" />
        <asp:Parameter Name="QHelp" Type="String" />
        <asp:Parameter Name="Createdate" Type="DateTime" />
        <asp:Parameter Name="Modifydate" Type="DateTime" />
        <asp:Parameter Name="AnswerType" Type="String" />
    </InsertParameters>
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="SurveyID" 
            PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="SurveyID" Type="Int32" />
        <asp:Parameter Name="Question" Type="String" />
        <asp:Parameter Name="QType" Type="String" />
        <asp:Parameter Name="PickListID" Type="String" />
        <asp:Parameter Name="QuestionNum" Type="Int32" />
        <asp:Parameter Name="Subsequence" Type="Int32" />
        <asp:Parameter Name="Active" Type="Boolean" />
        <asp:Parameter Name="Script" Type="String" />
        <asp:Parameter Name="Question_Locked" Type="Boolean" />
        <asp:Parameter Name="QHelp" Type="String" />
        <asp:Parameter Name="Modifydate" Type="DateTime" />
        <asp:Parameter Name="AnswerType" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>




Protected Sub dvSurveyQuestions_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvSurveyQuestions.ItemInserting
    'The DetailsView does not include SurveyID column...we need to set this column during INSERT operations because each question must belong to some survey.
    e.Values("SurveyID") = DropDownList1.SelectedValue
End Sub
Protected Sub dvSurveyQuestions_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles dvSurveyQuestions.ItemUpdating
    'The DetailsView does not include SurveyID column...we need to set this column during UPDATE operations because each question must belong to some survey.
    e.NewValues("SurveyID") = DropDownList1.SelectedValue
End Sub
Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvSurveyQuestions.DataBound
    'The event handler checks the row count of the DetailsView control. If it is zero then the mode of the DetailsView is changed to Insert using ChangeMode() method.
    If dvSurveyQuestions.Rows.Count = 0 Then
        dvSurveyQuestions.ChangeMode(DetailsViewMode.Insert)
    End If
    If dvSurveyQuestions.CurrentMode = DetailsViewMode.[ReadOnly] Then
        Dim txtName As TextBox = DirectCast(Page.Form.FindControl("dvSurveyQuestions:hiddenQuestionID"), TextBox)
    End If
End Sub
End Class

Solution

  • Try this:

    Dim txtName = DirectCast(dvSurveyQuestions.FindControl("hiddenQuestionID"), TextBox) 
    

    Since the NamingContainer of the TextBox is the DetailsView not the Page.