C# System.Web.UI.WebControl.Unit的行为
我有一个C# System.Web.UI.WebControl.Unit的行为,c#,asp.net,web-controls,C#,Asp.net,Web Controls,我有一个asp:TextBox,我可以通过以下方式设置宽度: <asp:TextBox runat="server" ID="TextBox1" Width="100"></asp:TextBox> 不可能是这样的: TextBox1.Width = "100%"; 我所期望的 TextBox1.Width = new Unit.. ; 问:nUnit为什么会这样?为什么int是可能的,但是string不是,为什么不是Unit的新对象?文本框是一个WebContr
asp:TextBox
,我可以通过以下方式设置宽度
:
<asp:TextBox runat="server" ID="TextBox1" Width="100"></asp:TextBox>
不可能是这样的:
TextBox1.Width = "100%";
我所期望的
TextBox1.Width = new Unit.. ;
问:
nUnit
为什么会这样?为什么int
是可能的,但是string
不是,为什么不是Unit
的新对象?文本框是一个WebControl
,其中Width是一个虚拟属性,定义为:
[DefaultValue(typeof(Unit), "")]
[WebCategory("Layout")]
[WebSysDescription("WebControl_Width")]
public virtual Unit Width
{
get
{
if (!this.ControlStyleCreated)
{
return Unit.Empty;
}
return this.ControlStyle.Width;
}
set
{
this.ControlStyle.Width = value;
}
}
单元
是一个结构(这是您问题的答案):
在此代码中,您可以找到问题的答案。文本框是一个
网络控制
,其中宽度是一个虚拟财产,定义为:
[DefaultValue(typeof(Unit), "")]
[WebCategory("Layout")]
[WebSysDescription("WebControl_Width")]
public virtual Unit Width
{
get
{
if (!this.ControlStyleCreated)
{
return Unit.Empty;
}
return this.ControlStyle.Width;
}
set
{
this.ControlStyle.Width = value;
}
}
单元
是一个结构(这是您问题的答案):
在这段代码中,您可以找到问题的答案。如果您的意思是说,单元是一个结构
- 试试myUnit.Value。它将得到所指出的值
- 然后可以使用
进行转换公共单位(int值)
我认为这应该有帮助。如果你是说,单元是一个结构
- 试试myUnit.Value。它将得到所指出的值
- 然后可以使用
进行转换公共单位(int值)
我认为这应该会有帮助。您可以将百分比设置为这样
TextBox1.Width = System.Web.UI.WebControls.Unit.Percentage(90);
您可以将百分比设置为这样
TextBox1.Width = System.Web.UI.WebControls.Unit.Percentage(90);
public class UnitConverter : TypeConverter
{
public UnitConverter()
{
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null)
{
return null;
}
string str = value as string;
if (str == null)
{
return base.ConvertFrom(context, culture, value);
}
string str1 = str.Trim();
if (str1.Length == 0)
{
return Unit.Empty;
}
if (culture != null)
{
return Unit.Parse(str1, culture);
}
return Unit.Parse(str1, CultureInfo.CurrentCulture);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value == null || ((Unit)value).IsEmpty)
{
return string.Empty;
}
return ((Unit)value).ToString(culture);
}
if (destinationType != typeof(InstanceDescriptor) || value == null)
{
return base.ConvertTo(context, culture, value, destinationType);
}
Unit unit = (Unit)value;
MemberInfo constructor = null;
object[] objArray = null;
if (!unit.IsEmpty)
{
Type type = typeof(Unit);
Type[] typeArray = new Type[] { typeof(double), typeof(UnitType) };
constructor = type.GetConstructor(typeArray);
object[] objArray1 = new object[] { unit.Value, unit.Type };
objArray = objArray1;
}
else
{
constructor = typeof(Unit).GetField("Empty");
}
if (constructor == null)
{
return null;
}
return new InstanceDescriptor(constructor, objArray);
}
}
TextBox1.Width = System.Web.UI.WebControls.Unit.Percentage(90);