Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态自定义asp.net控件:ID属性?_C#_Asp.net_Custom Controls - Fatal编程技术网

C# 动态自定义asp.net控件:ID属性?

C# 动态自定义asp.net控件:ID属性?,c#,asp.net,custom-controls,C#,Asp.net,Custom Controls,我想创建一个自定义控件:一个数字文本框。 我遵循了本教程()但我面临一个问题。 我希望动态创建这些控件,如下所示: NumericTextBox _Quantity = new NumericTextBox(); _Quantity.ID = "_Quantity" + id; _Quantity.Text = "1"; 但是,据说NumericTextBox中没有“ID”属性的定义。然而,这个控件是TextBox的子控件,所以它应该具有ID属性,就像文本的一样,它工作得很好 using Sy

我想创建一个自定义控件:一个数字文本框。 我遵循了本教程()但我面临一个问题。 我希望动态创建这些控件,如下所示:

NumericTextBox _Quantity = new NumericTextBox();
_Quantity.ID = "_Quantity" + id;
_Quantity.Text = "1";
但是,据说NumericTextBox中没有“ID”属性的定义。然而,这个控件是TextBox的子控件,所以它应该具有ID属性,就像文本的一样,它工作得很好

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
    }
}

这很奇怪,在我的代码中,它工作得非常完美:

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
    {

    }
}
在创建
NumericTextBox
实例的类中,是否缺少一些用法

编辑
FindControl
应与您的自定义控件配合使用。

感谢您的回复!我正在使用CustomControls的命名空间,所以我认为它会起作用。。。我用我的页面代码编辑了我的第一篇文章;)嗯,如果我把你的代码复制到我的解决方案中,它就会工作。如果尝试在
AddItem
-方法中创建简单
文本框的实例,会发生什么情况?你有身份证吗?也许你忘了引用一些组件…是的,我有=(NumericTextBox中实际上没有ID属性,但它应该被继承……我可以用控件的代码编辑第一篇文章。我向网站项目添加了CustomControls引用,但最奇怪的是文本属性有效,但没有ID属性。@Flash_Back正常的
文本框
ID
-属性吗?(仅用于测试)是的,正常文本框没有问题
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";
        }
    }
}