0

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
Jamie
  • 1,579
  • 8
  • 34
  • 74

2 Answers2

3

I found this link helps to solve without server side: Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."

the author says that you can use the dollar char ($) to access the inner control.

Ex:

dvSurveyQuestions$hiddenQuestionID

will get the value of hiddenQuestionID that is a inner control of dvSurveyQuestions

Rodrigo Reis
  • 1,097
  • 12
  • 21
1

Try this:

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

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

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I replaced `Dim txtName As TextBox = DirectCast(Page.Form.FindControl("dvSurveyQuestions:hiddenQuestionID"), TextBox)` with your suggestion but I still get the same error. Is there something else I am supposed to add along with that code? – Jamie Feb 01 '12 at 14:03
  • @jlg: Not that i know of. What error do you get and where? Have you debugged the code? Set a breakpoint at this line and experiement around with `FindControl`. – Tim Schmelter Feb 01 '12 at 14:13
  • `Could not find control 'hiddenQuestionID' in ControlParameter 'QuestionID'.` Another option I tried was putting the datasource in my template in the detailsview but that caused a different error saying that HiddenField didn't have a SelectedValue. I corrected the SelectedValue by changing it to Value and now I get `Unable to cast object of type 'System.Web.UI.WebControls.HiddenField' to type 'System.Web.UI.WebControls.TextBox'.` – Jamie Feb 01 '12 at 14:33
  • I changed the code you provided from TextBox to HiddenField and my bulleted list appeared. No more errors. `Dim QuestionID = DirectCast(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField)` – Jamie Feb 01 '12 at 14:40
  • One of the reasons why i would never use any of these XYZ-DataSource controls since you lose control, there is too much that happens in background. A last try: `ControlID="dvSurveyQuestions$hiddenQuestionID"`. But even if it works, that's fiddling around. **Edit**: Glad to hear that it works, the reason seem to be that a Hiddenfield is not a server control and therefor get's no client-id by ASP.NET. – Tim Schmelter Feb 01 '12 at 14:48