面临在ASP.NET中创建用户控件的问题 [英] Facing problem in creating user control in ASP.NET

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

问题描述

我正在尝试创建一个用户控件,我在下面给出了键入的代码 -

我首先打开一个网站。然后单击FILE,Take New Project。选择类库,添加引用System.web在it.delete class1页面中添加另一个名为input的类。我在其中键入以下代码 -

使用system; 
使用 system.web.ui.webcontrols;
命名空间 controlLibrary {
class 输入:webcontrol
{
string txt,cap;
public string txt {

get { return txt;}
set {txt = value ;}
}
public 字符串上限{
获取 {返回上限;}
set {cap = value ;}
}
}

视觉工作室给出了变量txt和cap已经存在的错误。并且不要让我的程序构建。

解决方案

对于您的情况,您有两种可能的解决方案。

1。私有变量

string txt,cap; - 将它们重命名为

  string  _txt,_cap; 



和重写属性

  public   string  txt 
{
get { return _txt;}
set {_ txt = value ;}
}
public string cap
{
get { return _cap;}
set {_ cap = 价值;}
}



2。使用自动属性。更好更清洁:



  class 输入:webcontrol 
{
public string txt { get ; set ;}
public 字符串 cap {获取; 设置;}
}


i am trying to create a user control, in which i have typed code given below-
I first open a website.then click FILE ,Take New Project .select class library, add reference of System.web in it.delete class1 page and add another class named input in it.I typed following code in it-

Using system;
using system.web.ui.webcontrols;
namespace controlLibrary{
class input:webcontrol
{
  string txt, cap;
public string txt{

get{return txt;}
set{txt=value;}
}
 public string cap{
get{return cap;}
set{cap=value;}
}
}



in this code visual studio gives error that variables txt and cap are already exist.and dont let my program build.

解决方案

For your case, you have two possible solutions.
1. Private variables
string txt, cap; - rename them to

string _txt, _cap;


and rewrite properties to

public string txt
{
    get{return _txt;}
    set{_txt  = value;}
}
public string cap
{
    get{return _cap;}
    set{_cap  = value;}
}


2. Use auto-properties. Better and cleaner:

class input:webcontrol
{
    public string txt{get;set;}
    public string cap {get;set;}
}


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

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