UserControl 中的 Web 控件为空?

发布于 2024-11-04 08:11:04 字数 1353 浏览 3 评论 0原文

我构建了一个小型用户控件,它本质上是一个 DropDownList,其中包含一些基于目标属性设置的预设值。

代码如下:

public partial class Selector : System.Web.UI.UserControl
{
    public string SelectedValue { get {return this.ddl.SelectedValue; } }
    public int SelectedIndex { get { return this.ddl.SelectedIndex; } }
    public ListItem SelectedItem { get { return this.ddl.SelectedItem; } }
    private string target;
    public string Target { get { return this.target; } set { this.target = value; } }
    protected void Page_Load(object sender, EventArgs e)
    {
        ddl.DataSource = target=="Group"?Util.GetAllGroups(Session["sessionId"].ToString()):Util.GetAllUsers(Session["sessionId"].ToString());
        ddl.DataBind();
    }
}

ASP-Markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Selector.ascx.cs" Inherits="InspireClient.CustomControls.Selector" %>
<asp:DropDownList runat="server" ID="ddl">
</asp:DropDownList>

如果我将选择器插入 aspx 页面,它就可以正常工作。 示例:

<SCL:Selector Target="Group" runat="server" />

但是,如果我像这样以编程方式添加它,则

ctrl = new Selector();
ctrl.Target = "User";

DropDownList“ddl”为空,并且应用程序(逻辑上)会引发错误。 Page_Load 是做这种事情的错误方法吗?我做错了什么?

我应该补充一点,“ctrl”是动态类型,不确定这是否与它有关。

提前致谢!

丹尼斯

I've built a small User Control which is essentially a DropDownList with some preset Values based on what the Target-Property is set on.

Here's the Code:

public partial class Selector : System.Web.UI.UserControl
{
    public string SelectedValue { get {return this.ddl.SelectedValue; } }
    public int SelectedIndex { get { return this.ddl.SelectedIndex; } }
    public ListItem SelectedItem { get { return this.ddl.SelectedItem; } }
    private string target;
    public string Target { get { return this.target; } set { this.target = value; } }
    protected void Page_Load(object sender, EventArgs e)
    {
        ddl.DataSource = target=="Group"?Util.GetAllGroups(Session["sessionId"].ToString()):Util.GetAllUsers(Session["sessionId"].ToString());
        ddl.DataBind();
    }
}

ASP-Markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Selector.ascx.cs" Inherits="InspireClient.CustomControls.Selector" %>
<asp:DropDownList runat="server" ID="ddl">
</asp:DropDownList>

If I insert my Selector into an aspx-Page it works just fine.
Example:

<SCL:Selector Target="Group" runat="server" />

However, If I programmatically add it like this

ctrl = new Selector();
ctrl.Target = "User";

the DropDownList "ddl" is null and the application (logically) throws an error. Is Page_Load the wrong Method to do such a thing? What am I doing wrong?

I should add, "ctrl" is of type dynamic, not sure if this has anything to do with it.

Thanks in advance!

Dennis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2024-11-11 08:11:04

由于您动态添加用户控件而不是“简单”Web 控件,因此您应该使用 LoadControl() 方法来实例化它:

protected void Page_Load(object sender, EventArgs e)
{
    Selector yourControl = (Selector) LoadControl("Selector.ascx");
    yourControl.Target = "User";
    Controls.Add(yourControl);
}

Since you're dynamically adding a user control and not a "simple" web control, you should use the LoadControl() method to instantiate it:

protected void Page_Load(object sender, EventArgs e)
{
    Selector yourControl = (Selector) LoadControl("Selector.ascx");
    yourControl.Target = "User";
    Controls.Add(yourControl);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文