我不使用我的用户控件 [英] i dont work with my user control

查看:87
本文介绍了我不使用我的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



a使用属性创建用户控件,我从usercontrol创建一个实例但是运行时有错误。

我的用户控制代码

hi
a create user control with property,i make a instance from usercontrol but running time have error.
this my user control code

public partial class fact_row : UserControl
   {

       public fact_row()
       {
           InitializeComponent();
       }
       public fact_row(string rowid, string account_no1, string account_no2, string operation, string type,
                       string debtor, string creditor)
       {
           this._row_id = rowid;
           this._account_no1 = account_no1;
           this._account_no2 = account_no2;
           this._operation = operation;
           this._type = type;
           this._debtor = debtor;
           this._creditor = creditor;
       }
       public string _row_id
       {
           get { return lbl_row_id.Text; }
           set { lbl_row_id.Text = value; }
       }
       public string _account_no1
       {
           get { return lbl_account_no1.Text; }
           set { lbl_account_no1.Text = value; }
       }
       public string _account_no2
       {
           get { return lbl_account_no2.Text; }
           set { lbl_account_no2.Text = value; }

       }
       public string _operation
       {
           get { return lbl_operation.Text; }
           set { lbl_operation.Text = value; }

       }
       public string _type
       {
           get { return lbl_type.Text; }
           set { lbl_type.Text = value; }
       }
       public string _debtor
       {
           get { return _unsetmask(lbl_Debtor.Text); }
           set { lbl_Debtor.Text = _setmask(value); }
       }
       public string _creditor
       {
           get { return _unsetmask(lbl_Creditor.Text); }
           set { lbl_Creditor.Text = _setmask(value); }
       }
       public Font _Font { get; set; }
       public Color _foreColor
       {
           get { return this.ForeColor; }
           set { this.ForeColor = value; }
       }
       protected string _setmask(string input)
       {
           string result = "";
           char[] arr = input.ToCharArray();
           Array.Reverse(arr);
           string temp = new string(arr);
           int i = 1;
           foreach (char c in temp)
           {
               result += c.ToString();
               if (i % 3 == 0 && i != temp.Length)
               {
                   result += ",";
               }
               i++;
           }
           char[] arr1 = result.ToCharArray();
           Array.Reverse(arr1);

           return new string(arr1);
       }
       protected string _unsetmask(string input)
       {
           return input.Replace(",", string.Empty);
       }
   }





并在我的项目中使用此用户控件,按钮代码为:



and use this user control in my project that button code is:

private void button1_Click(object sender, EventArgs e)
       {
          fact_row fact_row2=new fact_row("2","","102","1","john","2541000","");
           fact_row2.Height = 33;
           fact_row2.Width = 765;
           fact_row2._row_id = "2";
           fact_row2._debtor = "254622000";
           fact_row2.Show();
           panelEx5.Controls.Add(fact_row2);
           fact_row2.Location=new Point(0,34);
       }





运行时错误是:

对象引用未设置为一个对象的实例。



runtime error is:
Object reference not set to an instance of an object.

推荐答案

看看你的构造函数:

Look at your constructors:
public fact_row()
{
    InitializeComponent();
}
public fact_row(string rowid, string account_no1, string account_no2, string operation, string type,
                string debtor, string creditor)
{
    this._row_id = rowid;
    this._account_no1 = account_no1;
    this._account_no2 = account_no2;
    this._operation = operation;
    this._type = type;
    this._debtor = debtor;
    this._creditor = creditor;
}





只有其中一个调用InitializeComponent - 这是创建用户控件显示的所有控件的方法。如果你不打电话,那么你的文本框,标签和面板永远不会被创建,所以引用为空。

添加调用你的参数化构造函数,问题就会消失!



Only one of them calls InitializeComponent - which is the method which creates all the controls your user control displays. If you don;t call it, then your text boxes, labels, and panels never get created, so the references are null.
Add the call your your parameterized constructor, and the problem will go away!

public fact_row(string rowid, string account_no1, string account_no2, string operation, string type,
                string debtor, string creditor)
{
    InitializeComponent();
    this._row_id = rowid;
    this._account_no1 = account_no1;
    this._account_no2 = account_no2;
    this._operation = operation;
    this._type = type;
    this._debtor = debtor;
    this._creditor = creditor;
}







BTW:标准是私人支持字段以下划线开头,公开属性shoudl

以大写字母开头 - 你应该使用camelCase而不是变量,属性,方法和控件名称中的下划线:




BTW: the standard is that private backing fields start with underline, public properties shoudl
start with an Uppercase letter - and that you should use camelCase instead of underlines in variable, property, method, and control names:

public string RowId
{
    get { return lblRowId.Text; }
    set { lblRowId.Text = value; }
}





错过了下划线:doh:[/ edit]



[edit]Missed an underline :doh:[/edit]


您必须在aspx文件中加载用户控件。

参见示例:http://asp.net-tutorials.com/user-controls/using/ [ ^ ]

ASP .NET中的用户控件 [ ^ ]



如果你想要它代码背后那么你应该使用 LoadControl(用户控件的路径)



-KR
You must load the user control in your aspx file.
See the example : http://asp.net-tutorials.com/user-controls/using/[^]
User controls in ASP .NET[^]

and if you want it in code behind then you should use LoadControl("path to your user control").

-KR


这篇关于我不使用我的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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