无法在 CodeBehind 文件中引用隐藏字段

发布于 2024-12-23 04:51:05 字数 984 浏览 0 评论 0原文

我的 ASP.NET 2.0 Web 表单中有以下 HiddenField:

<asp:HiddenField ID="HiddenModel" runat="server" 
OnValueChanged="HiddenModel_ValueChanged" />

我的代码隐藏在此 if..else 语句中引用了 HiddenModel:

if (serial.Text.ToString() != "0")
{
CarpetMultiView.ActiveViewIndex = 1;
HiddenModel.Value = model.SelectedItem.Text.ToString();
LabelCurrent.Text = "Your Current Selection is :
Make-" + make.SelectedItem.Text.ToString() + " Model-" + 
model.SelectedItem.Text.ToString() + " S/N-" + serial.SelectedItem.Text.ToString();
Page.Title = make.SelectedItem.ToString() + " " + model.SelectedItem.ToString() + " " + 
serial.SelectedItem.ToString() + " " + "Carpet";
}

当我使用 VWD 2005 调试解决方案时,我收到以下错误:

The name 'HiddenModel' does not exist in the current context    

我的 @Page 指令继承了与代码隐藏匹配的地毯_模板类:

public partial class carpet_template : System.Web.UI.Page

我该如何解决这些错误和其他类似错误?

I have the following HiddenField in my ASP.NET 2.0 webform:

<asp:HiddenField ID="HiddenModel" runat="server" 
OnValueChanged="HiddenModel_ValueChanged" />

My codebehind references HiddenModel in this if..else statement:

if (serial.Text.ToString() != "0")
{
CarpetMultiView.ActiveViewIndex = 1;
HiddenModel.Value = model.SelectedItem.Text.ToString();
LabelCurrent.Text = "Your Current Selection is :
Make-" + make.SelectedItem.Text.ToString() + " Model-" + 
model.SelectedItem.Text.ToString() + " S/N-" + serial.SelectedItem.Text.ToString();
Page.Title = make.SelectedItem.ToString() + " " + model.SelectedItem.ToString() + " " + 
serial.SelectedItem.ToString() + " " + "Carpet";
}

When I debug the solution using VWD 2005, I receive the following error:

The name 'HiddenModel' does not exist in the current context    

My @Page directive inherits carpet_template which matches the codebehind class:

public partial class carpet_template : System.Web.UI.Page

How can I resolve these and other similar errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹉夏雨初晴づ 2024-12-30 04:51:05

从下面的链接:
如果您在 IIS 中进行测试,则应转到 VWD 中的网站菜单,单击启动选项,单击左侧窗格中树上的“构建”,然后在标记为“运行启动页面之前”的下拉列表中选择“无构建” ”。

全文此处。

From the link below:
"If you are testing in IIS, you should go to the website menu in VWD, click start options, click "build" on the tree in the left pane, and on the dropdown list labelled "Before running startup page" select "No Build".

Full story HERE.

∞梦里开花 2024-12-30 04:51:05

很难确定代码何时脱离页面生命周期的上下文,但听起来 asp.net 根本不接受该字段当前存在。

最合乎逻辑的原因是隐藏字段在页面生命周期中尚不存在(请参阅页面生命周期) - 代码是在页面加载之前还是在控件呈现之前运行?

或者,如果表单存在问题,您可能会出现此行为(例如,如果隐藏字段不在表单标记内)。

Its difficult to be sure when the code is out of context of the page life cycle, but it sounds like asp.net simply doesn't accept that the field currently exists.

The most logical reason is that the hidden field doesn't yet exist in the page life cycle (see page lifecycle) - is the code run before the page has been loaded or before the control has been rendered?

Alternatively if there is a problem with the form you could get this behaviour (eg if the hidden field is not within the form tags).

婴鹅 2024-12-30 04:51:05

删除相同文件的所有备份副本。删除这些备份文件后,我在同一文件夹中拥有同一文件的备份副本 - 解决方案编译时没有任何错误。

从下面的链接得到了这个解决方案:[slash84]

http://forums.asp.net/post/3060379.aspx

Delete all the backup copies of the same files. I had a backup copy of the same file in same folder, after deleting those backup files - solution is compiling without any errors.

Got this solution from link below: [slash84]

http://forums.asp.net/post/3060379.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文