C#控件的名称 [英] c# control names
问题描述
有没有办法控制的.Net如何设置名称
和 ID
我控制的财产?我有我需要的名字是相同的一些单选按钮。但是,如果我设置了 ID
单选按钮控件的属性它集名称
和 ID
。我看没有办法和名称
分别对待 ID。所以,现在所有按钮的ID是一样的好。
Is there a way to control how .Net sets the Name
and ID
property of my controls? I have some radio buttons for which I need the name to be the same. However if I set the ID
property of the radio button control it sets the Name
and the ID
. I see no way to treat the ID
and Name
separately. So now all button's ids are the same as well.
如果您认为我应该使用单选按钮列表
来实现这一点,你可能是对的,但我还没有找到一种方法,包括在动态创建的表结构单选按钮列表
。
If you are thinking that I should be using a RadioButtonList
to achieve this you may be right, but I have not found a way to include table structure in a dynamically created RadioButtonList
.
感谢
推荐答案
是
从声明,也就是说,一个文本框,其覆盖的UniqueID和ClientID的属性子类。然后,使用(在这个例子)MyControls.Textbox代替内置的文本框。
Declare a descendant class from, say, a textbox, which overrides the UniqueID and ClientID properties. Then, use (in this example) MyControls.Textbox instead of the built-in textbox.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyControls
{
public class Textbox : System.Web.UI.WebControls.TextBox
{
public override string ClientID
{
get
{
return ID;
}
}
public override string UniqueID
{
get
{
return ID;
}
}
}
public class Button : System.Web.UI.WebControls.Button
{
public override string ClientID
{
get
{
return ID;
}
}
public override string UniqueID
{
get
{
return ID;
}
}
}
}
请注意,你需要在web.config中注册您的控件:
Note that you'll need to register your controls in web.config:
<pages>
<controls>
<add tagPrefix="mycontrols" namespace="MyControls" assembly="MyAssembly" />
</controls>
</pages>
然后你就可以在你的标记中引用它们:
Then you can reference them in your markup:
<mycontrols:TextBox id="myID" runat="server"/>
这篇关于C#控件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!