gpt4 book ai didi

c# - C# 编译器无法识别的某些控件元素

转载 作者:行者123 更新时间:2023-11-29 14:51:45 26 4
gpt4 key购买 nike

我有一个使用 aspdotnetstorefront 平台的站点,尽管这应该适用于任何 C# 站点。在名为 Account 的 dll 中有一个自定义控件。这有几个元素,包括供客户输入姓名、电话号码等的文本框。

我已经使用 DotPeek 检查了源代码并验证了我使用的是正确的命名约定。

我正在尝试使用 javascript onChange 事件在选中复选框(帐户信息与账单相同)时将名字、姓氏和电话号码复制到下方框。因此,如果客户选择相同的信息,那么当他们从一个盒子移到另一个盒子时,这些信息将自动复制。

奇怪的是,这适用于某些文本框,但不适用于其他文本框。为了简单起见,我删除了复制内容的JS,并将其替换为用于测试的弹出框。

这有效,当我更改文本时,我得到一个“Hello World”弹出框:

ctrlAccount.txtFirstName.Attributes.Add("onchange", "alert('hello,world')");

但这不是:

ctrlAccount.txtPhone.Attributes.Add("onchange", "alert('hello,world')");

我得到的错误是:

CS1061: 'AspDotNetStorefrontControls.Account' does not contain a definition for 'txtPhone' and no extension method 'txtPhone' accepting a first argument of type 'AspDotNetStorefrontControls.Account' could be found

所以看起来编译器无法识别电话文本框。当我查看呈现的页面代码时(当然,当错误已被删除时)框就在那里并且 ID 是正确的。

使用 DotPeek 阅读源代码我看到:

public Account()
{

this._txtFirstName = new TextBox();
this._txtLastName = new TextBox();
this._txtEmail = new TextBox();
this._txtPassword = new TextBox();
this._txtPasswordConfirm = new TextBox();
this._txtPhone = new TextBox();

}

private void AssignClientReferenceID()
{
this._txtFirstName.ID = "txtFirstName";
this._txtLastName.ID = "txtLastName";
this._txtEmail.ID = "txtEmail";
this._txtPassword.ID = "txtPassword";
this._txtPasswordConfirm.ID = "txtPasswordConfirm";
this._txtPhone.ID = "txtPhone";
}

(为了可读性,我删除了很多其他字段)。但在我看来,电话号码的文本框应该有“txtPhone”和“txtFirstName”和“txtLastName”的想法工作得很好,那么为什么这只会在电话框上失败?

最佳答案

您添加到问题中的代码仅显示 ID 的分配,没有其他内容。我看不到 Account 控件的完整源代码,但通常带有前导下划线的字段是 protected 或私有(private)的,为了公开这些字段,使用了公共(public)属性,所以请检查如果它存在一个名为 txtPhone公共(public)属性

编辑

ctrlAccount.txtFirstName.Attributes.Add("onchange", "alert('hello,world')");
ctrlAccount.txtPhone.Attributes.Add("onchange", "alert('hello,world')");

尝试按 F12 并在上面的行中插入 txtFirstNametxtPhone 以查看这些属性的定义位置。

附言:不要混淆 id 的名称和类的公共(public)属性

关于c# - C# 编译器无法识别的某些控件元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24612609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com