如何在C#.Net中创建用户控件 [英] How to create user control in C# .Net

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

问题描述

我想在C#.Net Web应用程序中创建一个具有2个下拉菜单的用户控件& 1文本框.我想访问我的.CS页上的控件.如何为控件分配属性?

I want to create a user control in C# .Net Web Application with 2 drop downs & 1 Text Box.I want to access that control on my .CS Page.How to assign propeties to a control?

推荐答案

我假设您是说这是ASP .NET吗?

您只需将控件放入用户控件中即可.然后,在用户控件类中创建属性,这些属性映射到控件的属性,例如

< pre>公共字符串TextBoxText
{
得到{return TextBox1.Text; }
设置{TextBox1.Text = value; }
}</pre>
然后,您可以在aspx中设置TextBoxText,也可以访问后面的代码中的属性.
I assume you meant to say this is ASP.NET ?

You just put the controls inside your user control. Then you create properties inside the user control class, which map to the properties of the controls, such as

<pre>public string TextBoxText
{
get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}</pre>
Then you can set TextBoxText in your aspx, or you can access the properties in your code behind.


上面给出的答案是正确的,但是您将遇到其他问题,无法发送事件从UserControls到正在使用UserControl的网页.

只需结帐代理"即可.
The answer given above is right, but you will be confronted other question, you cannot send events from UserControls to the webpages where your usercontrols are being using.

Just Checkout "Delegate".


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

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