简体   繁体   English

在BLL ASP.NET中设置TextBox焦点

[英]Setting TextBox focus in BLL ASP.NET

I'm working on an application where the validation (ranges) checks are controlled in the business logic layer. 我正在开发一个在业务逻辑层中控制验证(范围)检查的应用程序。 The code looks similar to this: 该代码类似于以下内容:

public string ValidateRange(int value, int lowRange, int highRange, string fieldDesc, System.Web.UI.WebControls.TextBox txtBox)
{
     string msg = "";

     if (value >= lowRange & value <= highRange)
         msg = "";
     else
     {
         msg = "Please enter a value between " + lowRange + " and " + highRange + " for \"" + fieldDesc + ".\"";
         txtBox.Focus();
     }

     return msg;
}

I'm pretty sure I'm doing this incorrectly so I was hoping someone can explain to me the most efficient way of handling the function and BLL so that it can pass to the Presentation layer nicely. 我敢肯定我做错了,所以我希望有人可以向我解释处理函数和BLL的最有效方法,以便它可以很好地传递到Presentation层。 My hope is that I can limit my interaction with the BLL to ValidateRange checks on the form's TextBox controls with a return for each. 我的希望是,我可以将与BLL的交互限制为对窗体的TextBox控件的ValidateRange检查,并为每个控件返回一个值。 If I'm approaching this incorrectly, please let me know. 如果我处理不正确,请通知我。 If it does work this way, how can I allow the BLL to access the TextBoxes from the Presentation Layer? 如果这样做有效,我如何允许BLL从Presentation层访问TextBox?

Thanks for your help. 谢谢你的帮助。

I wouldn't try to have the BLL mess with textboxes or anything that is presentation related. 我不会尝试将BLL与文本框或与演示相关的任何内容弄混。 Afterall, the BLL is supposed to be presentation agnostic. 毕竟,BLL应该是与呈现无关的。 Otherwise, should you need to write, say, a Windows Forms presentation layer (to go with your ASP.NET one), you'd have to re-write or add all new methods on your BLL to support accepting System.Windows.Forms.TextBox as well! 否则,如果您需要编写一个Windows Forms表示层(与ASP.NET一起使用),则必须在BLL上重新编写或添加所有新方法以支持接受System.Windows.Forms .TextBox也一样! It defeats the whole purpose of n-tier if the two presentation layers cannot share the same BLL methods/code. 如果两个表示层不能共享相同的BLL方法/代码,则将破坏n层的全部目的。

No, you'll want to remove any traces of textboxes and such from the BLL and instead write code for auto-focusing invalid textboxes in the presentation layer itself. 不,您将要从BLL中删除文本框等的任何痕迹,而是在表示层本身中编写代码以使无效文本框自动聚焦。

If you are using ASP.NET's built-in validation stuff (Page.IsValid and CausesValidation and the like), you'll just have to resort to checking which validator came back false and set the focus that way. 如果您使用的是ASP.NET的内置验证工具(Page.IsValid和CausesValidation等),则只需要检查哪个验证器返回假并以这种方式设置焦点即可。 You can still have your BLL provide the error message. 您仍然可以让BLL提供错误消息。

For example, using a CustomValidator, you could have it call your BLL method ValidateRange. 例如,使用CustomValidator,可以让它调用BLL方法ValidateRange。 If the return value is String.Empty, return true (valid). 如果返回值为String.Empty,则返回true(有效)。 If it is non-empty/non-null, you know you got an error, so set the CustomValidator's ErrorMessage and/or Text properties to the string returned and then return false (invalid). 如果它是非空/非空,则说明您遇到了错误,因此请将CustomValidator的ErrorMessage和/或Text属性设置为返回的字符串,然后返回false(无效)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM