如何为C#Web用户控件创建属性 [英] How to Create Property for a C# Web User Control

查看:88
本文介绍了如何为C#Web用户控件创建属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好......

我有一个C#Web组件(Web用户控件),我自己在Visual Studio 2010中创建它。

现在我想创建它的一些属性。我的意思是一些属性,如ASP.Net

组件的属性。 ASP.Net组件的属性是分类的,可以具有嵌套属性以及可以获取和设置数据列表的属性。我需要一个非常明确的解决方案。

你能不能请别人帮助我。





< b> Web用户控件:包含用于设计的HTML页面和页面后面的代码(C#)



< img src =http://imageupload.co.uk/files/rgrbuqi9fp62aifc1cnc_thumb.pngborder =0alt =图片上传/ >









这是一个ASP.Net图表和图表属性的图像。


我想创建一个Web用户控件,其中包含一些属性,例如ASP.Net Chart的属性



< img src = http://imageupload.co.uk/files/nuces2yemigtptubpzsc_thumb.pngborder =0alt =图片上传/>

Hi everyone ...
I have a C# Web Component(Web User Control) that i made it by myself in Visual Studio 2010 .
Now i wanna create some properties for it . I mean some properties like ASP.Net
component's properties. ASP.Net component's properties are categorized and could have nested properties as well as properties that can get and set a list of data . I need a very clear solution for this purpose .
could you please someone help me .


Web User Control : That contains HTML page for design and a code behind page(C#)

<img src="http://imageupload.co.uk/files/rgrbuqi9fp62aifc1cnc_thumb.png" border="0" alt="Image Upload" />





This is an image of a ASP.Net Chart and Chart's properties .

I wanna create a Web User Control that contains some properties for example like ASP.Net Chart's properties

<img src="http://imageupload.co.uk/files/nuces2yemigtptubpzsc_thumb.png" border="0" alt="Image Upload" />

推荐答案

是否从scratc创建新控件h或从另一个继承,你需要添加将保存属性值的变量,以及将作为方法使用的属性。



假设您的控件将具有属性ControlSize,不是由数字决定,而是从3中选择,例如小,中,大。

你可以允许用户从数字1,2中选择, 3,并使其成为一个int属性,但让我们更好地使用枚举(为ellegance)。



添加:

Whether you create a new control from scratch or inherit from another one, you need to add variables that will hold the property value, and properties which will work as methods would.

Let's say your control will have property ControlSize, not determined by a number, but a choice out of 3, example "Small", "Medium", "Big".
You could allow the user to pick from numbers 1, 2, 3 and make it an int property, but let's better use an enumeration (for ellegance).

Add:
enum Sizes {Small, Medium, Big}



然后是这种类型的变量,但是确保将其设为私有:


And then a variable of this type, but make sure to make it private:

private Sizes controlsize;



那么该物业,实际上是开发商将会看到的物业(因此,这一项上市):




Then the property, which is in fact the one the developers will see (therefore, this one goes public):

public Sizes ControlSize
{
    get { return controlsize; }
    set 
    {
        controlsize = value;
        
        if (controlsize = Sizes.Small)
             this.Size = new Size(10, 10); 
        else if (controlsize = Sizes.Medium) 
             this.Size = new Size(20, 20); 
        else if (controlsize = Sizes.Big)
             this.Size = new Size(30, 30);
    }
}



该属性将导致两者都改变​​属性值以及实际大小。

希望这会有所帮助!



PSSize只存在于System.Drawing库中,所以你需要使用System来获得
。画画;在标题中。


The property will cause both to change the property value as well as the actual size.
Hope this helps!

PS "Size" will only exist from the System.Drawing library, so you'll need
using System.Drawing; in the header.


这篇关于如何为C#Web用户控件创建属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆