How to define a property for a user control (c#:asp.net)

Source: Internet
Author: User
Tags define definition
Asp.net| Control Preface

Asp. NET user control is very powerful, in the Web form to call to believe that everyone will, if you want to give the user control to define attributes, then how to do? As long as you finish this article, you will be able to make a few attributes on your control. This article is not very suitable for beginners. NET friend, involves some object-oriented knowledge, if you do not understand this knowledge, please refer to the related books

Download the E-book here (http://www.cs-aspx.net/down.aspx)

Program
namespace dotnet
{
Using System;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;

<summary>
Summary description of the mycontrol.
</summary>
public class MyControl:System.Web.UI.UserControl
{

protected System.Web.UI.WebControls.DropDownList Drop;
protected System.Web.UI.WebControls.CheckBoxList Check;
protected System.Web.UI.WebControls.RadioButtonList Radio;
protected System.Web.UI.WebControls.Button Btngetvalue;
protected System.Web.UI.WebControls.Label lbmsg;



Private Viewtypes _mytypes;
private string _myvalue = "";
private int _mymaxnumber=0;
private int _myminnumber=0;
The properties we define, the _mytypes enumeration value, the minimum value _mymaxnumber,_myminnumber 3 controls, and the maximum value.
public string myvalue//This is not for the moment.
{

Get
{
return _myvalue;
}
Set
{
_myvalue = value;
}
}
Public enum viewtypes//defines an enumeration value for 3 controls
{
Drop,
Check,
Radio
}
Public Viewtypes Mytypes
{
Get
{
return _mytypes;
}
Set
{
_mytypes= value;
}
}
public int Mymaxnumber
{
Get
{
return _mymaxnumber;
}
Set
{
_mymaxnumber=value;
}
}
public int Myminnumber
{
Get
{
return _myminnumber;
}
Set
{
_myminnumber=value;
}
}

/*
The above Myvalue,mytypes,mymaxnumber,myminnumber four modules are the four properties that are defined for the control, where the get{} section here has the function of reading the value, and set{} is written to the attribute in part. Note: They have to use public definition, absolutely cannot use private,protected definition here involves a see-level problem, not understand friends please refer to the relevant books, but also to note that we can not define the value of the property and the variable name declared above. For example:
private int _intcount;
public int _intcount//Avoid, here must not and above declared variable is the same, here is wrong, to change to public int intcount
{
Get{return _intcount;}
Set{_intcount=value;}
}
Here's the trick, when you declare a variable, you can underline the first letter, or when you define the attribute, capitalize the first letter, such as the private int intcount, and then the public int intcount.
We're talking about what these properties are for: Mytypes is the display of which control (an enumeration is defined here, with three values in it, respectively, a Drop-down list box, check box, and radio button, and don't forget to declare it on the above. Private Viewtypes _mytypes) ; Mymaxnumber,myminnumber is the maximum and minimum value bound to the control;
*/


private void Page_Load (object sender, System.EventArgs e)
{
This. Getcontrolinformation ()//Call function
}
private void Getcontrolinformation ()
{

if (! Page.IsPostBack)
for (int intcount=this._myminnumber;intcount<=this._mymaxnumber;intcount++)
Iterate the bound control, _myminnumber as the starting value, _mymaxnumber the maximum value
{


Switch (this._mytypes)//Judgment enumeration value
{
Case viewtypes.drop://display Drop-down list, bound values, the following are the same ...
This. Drop.visible=true;
This. DROP.ITEMS.ADD (Intcount.tostring ());
Break
Case Viewtypes.check:
This. Check.visible=true;
This. Check.repeatcolumns=this._mymaxnumber;
This. CHECK.ITEMS.ADD (Intcount.tostring ());
Break
Case Viewtypes.radio:
This. Radio.visible=true;
This. Radio.repeatcolumns=this._mymaxnumber;
This. RADIO.ITEMS.ADD (Intcount.tostring ());
Break

}
}

}
The Visible property of the above three controls is set to false.


Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
This.btnGetValue.Click + = new System.EventHandler (This.btngetvalue_click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Btngetvalue_click (object sender, System.EventArgs e)
{
String Str= "";
if (This._mytypes==viewtypes.drop)
{
str= "Drop-down list box";
}
else if (This._mytypes==viewtypes.check)
{
str= "check box";
}
else if (This._mytypes==viewtypes.radio)
{
Str= "radio Button";
}
this.lbmsg.text= control type: +str+ bound data Minimum value: "+this." Myminnumber.tostring () + "Max:" +this. Mymaxnumber.tostring ();
}



/*
<%@ Register tagprefix= "UserControl" src= "Mycontrol.ascx" tagname= "MyControl"%>
<usercontrol:mycontrol id= "Con" runat= "Server" mytypes= "Radio" myminnumber= "1" mymaxnumber= "ten" ></ Usercontrol:mycontrol>
The function of the button is to display the relevant information on the Label control, select which type, and which control is bound by the maximum and minimum value, and then you can call the control now, of course, the robustness of the program has to improve, here just play a role. Everybody has what not to go to http://www.cs-aspx.net to post son, I try to answer everybody's question!!!!!
*/

}
}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.