gpt4 book ai didi

c# - ControlParameter 找不到 UserControl 中的控件

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:16 25 4
gpt4 key购买 nike

我是 ASP.NET 的新手,但一直在顺利进行,通过一些研究能够克服迄今为止的所有障碍。但是我遇到了一个似乎无法解决的小问题,涉及我创建的自定义控件。

该控件是一个通过 JQuery 弹出的小部件,包含允许用户过滤结果集的控件。我将此弹出窗口的所有元素嵌入到两个特定页面中,并且这些页面都运行良好。我决定创建一个自定义控件,并且据我所知已经正确地完成了,在目标页面上使用带有 Control' 指令和 Register' 指令的 ascx 文件,所以:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PopUp_TopicFilterControl.ascx.cs" Inherits="POD.PopUp_TopicFilterControl" %>

<%@ Register TagPrefix="popup" TagName="TopicFilterControl" Src="~/Controls/PopUp_TopicFilterControl.ascx" %>

“主”页面包含一个对象数据源,它需要在我的自定义控件中引用控件以获取其选择参数。这是数据源:

<asp:ObjectDataSource ID="TopicDataSource" runat="server" SelectMethod="GetRecentTopics" TypeName="POD.App_Objects.Topic">
<SelectParameters>
<asp:Parameter DefaultValue="12" Name="select_limit" Type="Int32" />
<asp:ControlParameter DefaultValue="" ControlID="TopicCategoryFilterList" Name="category" Type="String" />
<asp:ControlParameter DefaultValue="" ControlID="TopicCreatorFilterList" Name="creator" Type="Int32" />
<asp:ControlParameter DefaultValue="" ControlID="TopicDateAfterFilterText" Name="date_after" Type="String" />
<asp:ControlParameter DefaultValue="" ControlID="TopicDateBeforeFilterText" Name="date_before" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>

这是自定义控件的主体:

<asp:Panel id="TopicFilter_Panel" runat="server" CssClass="PopUp" ClientIDMode="Static" EnableTheming="False" EnableViewState="False" ViewStateMode="Disabled">
<asp:Panel runat="server" CssClass="PopUp_Heading">
<h1><a id="TopicFilter_Close" href="#">Close [X]</a></h1>
</asp:Panel>
<asp:Panel runat="server" CssClass="PopUp_Body">
<asp:Table runat="server">
<asp:TableRow runat="server">
<asp:TableCell runat="server" HorizontalAlign="Right">
By Category:
</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList
ID="TopicCategoryFilterList"
runat="server"
DataSourceID="TopicCategoryDataSource"
DataTextField="value"
DataValueField="value"
AppendDataBoundItems="True"
AutoPostBack="False">
<asp:ListItem Selected="True" Value="">&mdash; All &mdash;</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server" HorizontalAlign="Right">
By Author:
</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList
ID="TopicCreatorFilterList"
runat="server"
DataSourceID="UserDataSource"
DataTextField="person_name_full"
DataValueField="user_id"
AppendDataBoundItems="True"
AutoPostBack="False">
<asp:ListItem Selected="True" Value="">&mdash; All &mdash;</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server" HorizontalAlign="Right">
By Date (After):
</asp:TableCell>
<asp:TableCell runat="server">
<asp:TextBox ID="TopicDateAfterFilterText" runat="server" />
<ajaxToolkit:CalendarExtender ID="TopicDateAfterCalendarExtender" TargetControlID="TopicDateAfterFilterText" PopupPosition="BottomRight" runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server" HorizontalAlign="Right">
By Date (Before):
</asp:TableCell>
<asp:TableCell runat="server">
<asp:TextBox ID="TopicDateBeforeFilterText" runat="server" />
<ajaxToolkit:CalendarExtender ID="TopicDateBeforeCalendarExtender" TargetControlID="TopicDateBeforeFilterText" PopupPosition="BottomRight" runat="server" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="2" VerticalAlign="Bottom" HorizontalAlign="Center">
<asp:Button runat="server" Text="Apply" CssClass="PopUp_Apply" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
</asp:Panel>

我得到的错误只是对象数据源没有看到我的自定义控件内的控件(例如,“在 ControlParameter 'category' 中找不到控件 'TopicCategoryFilterList'”)。显然这是一件非常简单的事情,但我似乎无法为我的生活简单地弄清楚如何使这项工作。我很确定这里一定已经解决了这个问题,但我找不到示例。

最佳答案

这是我自己对这个“问题”的解决方案,以防有人遇到同样的困难并像我一样疯狂地转动轮子来寻找答案。首先,这是返回自定义控件属性的代码片段。我将这段代码放在 ascx.cs 文件中:

public partial class PopUp_TopicFilterControl : System.Web.UI.UserControl
{

public string TopicCategory
{
get { return TopicCategoryFilterList.SelectedValue.ToString(); }
}

}

这里是对 ObjectDataSource 中的 ControlParameter 的更改:

<asp:ControlParameter
DefaultValue=""
ControlID="TopicFilterSelection"
PropertyName="TopicCategory"
Name="category"
Type="String" />

请注意,我所要做的就是将 ControlID 更改为我的自定义控件的名称,并添加 PropertyName 属性,该属性指的是我在 ascx.cs 文件中定义的属性。

这就是它的全部。当您看到它时很明显,但它让我难住了好几个小时。

关于c# - ControlParameter 找不到 UserControl 中的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10017948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com