【问题标题】:‘label’ does not contain a definition for ‘Name’ and no extension method“标签”不包含“名称”的定义,也没有扩展方法
【发布时间】:2017-02-01 16:17:56
【问题描述】:

我在以下代码中收到此错误消息:

‘label’不包含‘Name’的定义,也没有扩展名 可以找到接受第一个参数类型标签的方法“名称”。 (您是否缺少或使用指令或程序集引用?)

这是关于 lbl.Name 和 lbl.Location 但我不知道为什么。

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

namespace budgetTracker
{
    public partial class Default : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        static int i = 1;

        protected void saveSalaryButton_Click(object sender, EventArgs e)
        {
            monthlySalaryLabel.Text = "$" + salaryInput.Text;
        }

        private void addExpense()
        {
            Label lbl = new Label();
            lbl.Name = "expense" + i.ToString();
            lbl.Text = expenseNameInput.Text;
            lbl.Location = new Point(15, 15);
            this.Controls.Add(lbl);
            expenseNameInput.Text = String.Empty;
        }

        protected void addExpenseButton_Click(object sender, EventArgs e)
        {
            addExpense();
        }
    }
}

【问题讨论】:

    标签: c# asp.net visual-studio


    【解决方案1】:

    asp:Label 属于名称空间System.Web.UI.WebControls,在该类中没有名为Name 的属性。要唯一标识该控件,您应该使用 ID 代替 Name,因此代码将是:

    Label lbl = new Label();
    lbl.ID= "expense" + i.ToString(); // Change is here
    lbl.Text = expenseNameInput.Text;  
    this.Controls.Add(lbl);
    

    为了固定容器中控件的位置,我认为对您来说更好的选择是使用样式。试试这样的:

    lbl.Style.Add("width","40px");
    lbl.Style.Add("top", "10px");
    

    【讨论】:

    【解决方案2】:

    标签类中没有名为Name的属性

    【讨论】:

    猜你喜欢
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2018-09-24
    • 1970-01-01
    • 2014-03-14
    • 2018-12-13
    相关资源
    最近更新 更多