使用c#在asp.net中使用多个控件进行自定义控件 [英] Custom control with multiple controls in asp.net using c#

查看:64
本文介绍了使用c#在asp.net中使用多个控件进行自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2010我是新手来开发自定义控件,实际上我用一个文本框开发了样本自定义控件,我得到了正确的解决方案我想向前迈出一步意味着开发具有多个控件的自定义控件(文本框,下拉列表)列表和复选框)任何人都可以帮我详细说明正确的例子。



以下我添加我的示例自定义控制代码



1)开始 - > vs 2010 - >档案 - >新网站 - >提供名称 - >好的

2)解决方案资源管理器 - > rc解决方案 - >添加 - >网络服务器控制 - >提供名称,然后点击确定

3)它打开窗口,在该窗口粘贴下面代码



使用系统;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Linq;

使用System.Text;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;



命名空间ServerControl1

{



[DefaultProperty(" ch)]]

[ToolboxData("< {0}:ValidTextBox1 runat = server>< / {0}:ValidTextBox1>< {1}:ValidTextBox2 runat = server>< / {1}:ValidTextBox2>" ;)]



公共类ValidTextBox1:TextBox

{

[Bindable(true)]

[类别(外观)]

[DefaultValue("")]

[Localizable(true)]

公共字符串ch

{

get

{

String s =(String)ViewState [" ch"];

return((s == null)? " [" + this.ID +"]" :s);

}

set

{

ViewState [" ch"] = value; < br $>
}

}

private RequiredFieldValidator rfv;

public string NotValid;

public string C_Script =" true";





protected override void RenderContents(HtmlTextWriter op)

{

op.Write(ch);

base.RenderContents(op);

rfv.RenderControl(op);

}

}

}



构建解决方案



将新项目添加到网站



将网络服务器控制的参考添加到网站,如下所示。



右键点击网站 - >添加参考 - >项目 - >选择自定义控件名称 - >单击确定。



新我们可以在网站中使用自定义控件只需将以下代码粘贴到网络表单中



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用ServerControl1;



public partial class _Default:System.Web.UI.Page

{



protected void Page_Load(object sender,EventArgs e)

{

for(int i = 0; i< 10; i ++)

{

ValidTextBox1 txt = new ValidTextBox1();

pnl1.Controls.Add(txt);

}



}



}





构建并执行你将获得一个文本框作为outpot



但我用它来开发自定义控件多个控件可以帮助它紧急完成我的客户需求任务。

I am using VS2010 I am new to develop Custom controls,actually i developed sample custom control with one textbox and i got the proper solution i want to go a step forward means to develop custom control with multiple controls (textboxes, drop down list, and checkbox) can any one help me with proper example in detail.

below i am adding my sample custom control code

1) start -->vs 2010 --> File --> New website --> Provide Name --> Ok
2) solution explorer -->rc on solution --> add --> web server control --> provide name then click on ok
3) it opens the window in that window paste below code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServerControl1
{

[DefaultProperty("ch")]
[ToolboxData("<{0}:ValidTextBox1 runat=server></{0}:ValidTextBox1> <{1}:ValidTextBox2 runat=server></{1}:ValidTextBox2>")]

public class ValidTextBox1 : TextBox
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string ch
{
get
{
String s = (String)ViewState["ch"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["ch"] = value;
}
}
private RequiredFieldValidator rfv;
public string NotValid;
public string C_Script = "true";


protected override void RenderContents(HtmlTextWriter op)
{
op.Write(ch);
base.RenderContents(op);
rfv.RenderControl(op);
}
}
}

Build the solution

add new item to web site

add reference of web server control to the website as given below.

rightclick on website--> add reference --> projects --> select the custom control name -->click on ok.

new we can use the custom control in web site just paste the below code in web form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServerControl1;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
ValidTextBox1 txt = new ValidTextBox1();
pnl1.Controls.Add(txt);
}

}

}


build and execute u wil get one text box as outpot

But i wand to develop custom control with multiple controls can any one help its urgent its my task for the client requirement.

推荐答案

请在这里阅读http://msdn.microsoft.com/en-us/library/ms996431.aspx [ ^ ]


这篇关于使用c#在asp.net中使用多个控件进行自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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