protected Control LoadSkin() { Control skin; string skinPath = Globals.GetSkinPath() + "/Skins/" + SkinFilename.TrimStart('/'); string defaultSkinPath = Globals.ApplicationPath + "/Themes/default/Skins/" + SkinFilename.TrimStart('/'); // Do we have a skin? if (SkinFilename == null) throw new Exception("You must specify a skin."); // Attempt to load the control. If this fails, we're done try { skin = Page.LoadControl(skinPath);//skinPath是控件的url } catch (FileNotFoundException) { // Ok we couldn't find the skin, let's attempt to load the default skin instead try { skin = Page.LoadControl(defaultSkinPath); } catch (FileNotFoundException) { throw new Exception("错误:皮肤文件 " + skinPath + " 没有找到. 皮肤必须被指定,以用于控件的呈现."); } } return skin; }可以参考一下 那个asp.net论坛
{
Control skin;
string skinPath = Globals.GetSkinPath() + "/Skins/" + SkinFilename.TrimStart('/');
string defaultSkinPath = Globals.ApplicationPath + "/Themes/default/Skins/" + SkinFilename.TrimStart('/'); // Do we have a skin?
if (SkinFilename == null)
throw new Exception("You must specify a skin."); // Attempt to load the control. If this fails, we're done
try {
skin = Page.LoadControl(skinPath);//skinPath是控件的url
}
catch (FileNotFoundException) { // Ok we couldn't find the skin, let's attempt to load the default skin instead
try { skin = Page.LoadControl(defaultSkinPath);
}
catch (FileNotFoundException) {
throw new Exception("错误:皮肤文件 " + skinPath + " 没有找到. 皮肤必须被指定,以用于控件的呈现.");
}
} return skin;
}可以参考一下 那个asp.net论坛
是不是这样的:
我这里是动态产生 checkBox
先定义: CheckBox[] myChkW = new CheckBox[24];#region 创建周选择checkBox的代码
private void ShowchkWeekArray()
{
for(i = 0; i < 24; i++)
{
//GroupBox gB1=new GroupBox();
numberTochina();
myChkW[i] = new CheckBox(); //创建对象的实例24, 32
myChkW[i].Location = new System.Drawing.Point(28 + 90 * (i % 6),50 + 25 * (i / 6));
myChkW[i].Name = "checkBox";
myChkW[i].Size = new System.Drawing.Size(86,24);
myChkW[i].Text ="第"+ chinaNumber +"周";
myChkW[i].FlatStyle=FlatStyle.Popup;
myChkW[i].Click += new System.EventHandler(this.myChkW_CheckedChanged); //统一的事件处理
this.Controls.Add(myChkW[i]); //在窗体上呈现控件
}
} #endregion
然后调用就是
a.用户控件(ASP创建页面一样)
(I). 创建页面,拖入控件,设置属性/方法. <% @Control Language="C#" Debug="True" %>中的@Control指令来定义此页 将包含控件代码
(II) 保存为*.ascx文件,如a.ascx.
(III).使用: 头<%@Register Tagprefix="MyFirstControl" TagName="MyLbl" Src="a.axcs" %>
//Tagprefix为控件的前缀,像ASP:TextBox中的ASP
//TagName用于指定自定义控件的名称
//Src指定控件文件源
身体:<MyFirstControl:MyLbl runat="Server" id="AllMine" MyText="成功了" />
b.使用C#创建自定义控件
(I). 创建纯代码文件,继承基类Control,并保存为*.cs,如a.cs.
(II).将代码编译生成程序集: csc /t:library /r:System.dll,System.Web.Dll a.cs
//library告诉C#编译器生成程序集
// /r:System.dll System.Web.Dll告诉C#编译器引用指定的程序集
(III).将生成dll文件放在bin目录中
(IV).使用: <% @Register TagPrefix="Mine" Namespace="MyOwnControls" Assembly="a" %>