在 ASP.NET 中将内容 (aspx) 与代码 (aspx.cs) 分离

发布于 2024-09-01 07:32:54 字数 679 浏览 4 评论 0原文

我想知道将 aspx 页面 (ASP.NET 3.5) 的内容与代码(我使用 C#)分离的最佳实践是什么。我有一个用户可以在其中输入数据的表单 - 例如,他们可以输入百分比。如果他们输入无效的数据,他们会收到一条错误消息:

<p class="errormsg" id="percenthigh">Please enter a percent below 100</p>
<p class="errormsg" id="percentnegative">Percent cannot be below 0</p>
<p class="errormsg" id="percentnot">This is not a percent</p>

所以本质上我隐藏了错误消息并根据用户输入的内容显示错误消息。

我相信这是将内容与背后的代码分开的最佳方法。但是,如何根据用户输入选择元素并隐藏/取消隐藏它们?我知道我可以对元素执行 runat="server" 但问题是我无法按类进行选择并且仅限于 ID。

您推荐什么解决方法?除了将值放入后面的代码之外,这是众所周知的难以调试的问题。

这在 ASP.NET 4 中也被“修复”了吗?我只对通过 C#/ASP.NET 执行此操作感兴趣,因为有些人禁用了 JavaScript。这意味着我必须检查客户端和服务器端的错误。

I would like to know what is the best practice on separating the content of an aspx page (ASP.NET 3.5) from the code (I'm using C#). I have a form that users can type data in - for example they are allowed to enter a percent. If they enter data that's not valid they would get an error message:

<p class="errormsg" id="percenthigh">Please enter a percent below 100</p>
<p class="errormsg" id="percentnegative">Percent cannot be below 0</p>
<p class="errormsg" id="percentnot">This is not a percent</p>

So in essence I'm hiding the error messages and showing one depending on what the user input is.

I believe this is the best way to seperate the content from the code behind. However, how do I select elements and hide/unhide them depending on the user input? I'm aware I can do a runat="server" on the elements but the problem is that I can't select by class and am limited only to ID's.

What workarounds do you recommend? Aside from putting in the values in code behind which is notoriously difficult to debug.

Also has this been "fixed" in ASP.NET 4? And I'm interested in doing this only via C#/ASP.NET as some people have JavaScript disabled. This means that I would have to check errors on both client side and server side.

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

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

发布评论

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

评论(3

迷你仙 2024-09-08 07:32:54

为此,请使用 ASP.Net Validation 控件。这将为您处理代码中的接线。您对问题中的每个段落标签使用不同的验证控件。

Use an ASP.Net Validation control for this. That will take care of the wiring in the code for you. You use a different Validation control for each of the paragraph tags in your question.

山川志 2024-09-08 07:32:54

您应该看看 asp net 验证器。在大多数情况下,这些已经足够好了。

如果 ASP NET 验证器因任何原因不适合,您可以检查 jQuery 解决方案,例如 this< /a> one

无论如何,我建议您避免花时间解决已经以许多不同(好的)方式解决的问题。

You should take a look to asp net validators. In most of the cases these are good enough.

If ASP NET validators are not suitable for any reason you could check a jQuery solution like this one

In any case, I'd recommend you to avoid spending time solving problems already solved in many different (good) ways.

兔小萌 2024-09-08 07:32:54

当您想要唯一的元素时,选择应该按 ID 进行,ID 在所有元素中应该是唯一的。类的使用类似于 HTML 中的类型,通常用于样式化。

另外,asp.net 项目中的文本应该保存在资源文件中。这样可以轻松更改语言。

Selectionm should be by ID when you want unique elements, ID should be unique across all of your elements. Class is used like a type in HTML, and is generally used for styling.

Also, text in asp.net projects should be saved in resource files. This allows easy changing of languages.

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