【问题标题】:Dynamic custom asp.net control : ID property?动态自定义 asp.net 控件:ID 属性?
【发布时间】:2015-01-15 14:07:58
【问题描述】:

我想创建一个自定义控件:一个数字文本框。 我按照本教程(http://www.codeproject.com/Articles/42382/Creating-a-Numeric-TextBox-Control)进行操作,但遇到了问题。 我想像这样动态地创建这些控件:

NumericTextBox _Quantity = new NumericTextBox();
_Quantity.ID = "_Quantity" + id;
_Quantity.Text = "1";

但是,据说NumericTextBox中没有“ID”属性的定义。然而,这个控件是 TextBox 的子控件,所以它应该有 ID 属性,就像 Text 的那个很好用......

using System;
using System.Web.UI;

[assembly: WebResource("CustomControls.Resources.NumericTextBox.js", "text/javascript")]
namespace CustomControls
{
    [ToolboxData(@"<{0}:NumericTextBox ID="""" Text="""" runat=""server""></{0}:NumericTextBox>")]
    public partial class NumericTextBox : System.Web.UI.WebControls.TextBox
    {
        // specific code
    }
}

我错过了什么吗?如果我可以使这个属性起作用,FindControl 方法是否可以与自定义控件一起使用?

非常感谢!

编辑:

我的解决方案中有两个项目:

  • 一个类库“CustomControls”,其中包含 NumericTextBox.cs
  • 还有我的网站。

这是我页面的提取代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using CustomControls;

public partial class MyPage : System.Web.UI.Page
{
    protected void AddItem(Enum type, Enum content)
    {
        string id = (content != null) ? content.ToString() : type.ToString();
        ...

        NumericTextBox _Quantity = new NumericTextBox();
        _Quantity.ID = "_Quantity" + id;
        _Quantity.Text = "1";

        ...
    }
}

【问题讨论】:

    标签: c# asp.net custom-controls


    【解决方案1】:

    这很奇怪,在我的代码中它运行良好:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace ButtonTest2
    {
        [ToolboxData(@"<{0}:NumericTextBox ID="""" Text="""" runat=""server""></{0}:NumericTextBox>")]
        public partial class NumericTextBox : TextBox
        {
    
        }
    }
    

    实例化:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace ButtonTest2
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                NumericTextBox tb = new NumericTextBox();
                tb.ID = "123";
            }
        }
    }
    

    您是否错过了创建NumericTextBox 实例的类中的一些用法?

    编辑 FindControl 应该可以与您的自定义控件一起使用。

    【讨论】:

    • 感谢您的回复!我正在使用 CustomControls 的命名空间,所以我认为它会起作用......我用我的页面代码编辑了我的第一篇文章;)
    • 嗯,如果我将您的代码复制到我的解决方案中,它就可以工作。如果您尝试在您的AddItem 方法中创建一个简单的TextBox 实例会发生什么?你有身份证吗?也许您忘记引用一些程序集...
    • 是的,我有 =( NumericTextBox 中实际上没有 ID 属性,但它应该被继承...我可以使用控件的代码编辑第一篇文章。我添加了对网站项目的 CustomControls 引用,但是最奇怪的是 Text 属性有效,但 ID 属性无效。
    • @Flash_Back 做一个普通的TextBox 有一个ID-属性? (仅用于测试)
    • 是的,正常的TextBox没问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多