在 ControlParameter 中找不到控件
我的页面有一个详细信息视图,其中有一个隐藏字段,该字段由 SQLDataSource 引用以填充同一详细信息视图中的不同字段。无论我尝试多少种不同的方法,我都无法通过代码隐藏来找到控件。我确实需要能够显示与 dsPicklist
SqlDataSource 关联的 TEXT
字段。我已经标记了导致问题的代码。我真的很感谢在尝试显示此信息时提供一些帮助。
<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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现此链接有助于在没有服务器端的情况下解决问题: 解决错误“无法” find control 'xxx' in ControlParameter 'xxx'."
作者说可以使用美元字符($)来访问内部控件。
例如:
dvSurveyQuestions$hiddenQuestionID
将获取hiddenQuestionID的值,它是dvSurveyQuestions的内部控件
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
试试这个:
因为 TextBox 的 NamingContainer 是 DetailsView 而不是 Page。
Try this:
Since the NamingContainer of the TextBox is the DetailsView not the Page.