[英]‘label’ does not contain a definition for ‘Name’ and no extension method
我在以下代碼上收到此錯誤消息:
“標簽”不包含“名稱”的定義,找不到包含標簽的第一個參數類型的擴展方法“名稱”。 (您是否丟失或使用指令或程序集引用?)
它與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();
}
}
}
該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");
在標簽類中,沒有名為Name的屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.