无法在ControlParameter中找到控件

时间:2012-01-31 22:38:16

标签: vb.net detailsview findcontrol hiddenfield

我的页面有一个DetailsView,其中有一个隐藏字段,由SQLDataSource引用以填充同一DetailsView中的不同字段。无论我尝试多少种不同的方式,我都无法获得代码隐藏来找到控件。我真的需要能够显示与TEXT SqlDataSource关联的dsPicklist字段。我已经标记了导致问题的代码。我真的很感激在尝试显示这些信息方面提供了一些帮助。

<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

2 个答案:

答案 0 :(得分:2)

我发现此链接有助于在没有服务器端的情况下解决:Solving the error "Could not find control 'xxx' in ControlParameter 'xxx'."

作者说您可以使用美元字符( $ )来访问内部控件。

前:

dvSurveyQuestions$hiddenQuestionID

将获取hiddenQuestionID的值,该值是dvSurveyQuestions的内部控件

答案 1 :(得分:1)

试试这个:

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

由于TextBox的NamingContainer是DetailsView而不是Page。