在多个页面上重复任务

本文关键字:任务 | 更新日期: 2023-09-27 18:13:24

使用c#和ASP。NET在内网网站上。我有多个"管理"页面,我检查字段,以确保一个日期是存在的,检查字段,以确保用户名是存在的,并在这些字段填写,如果他们留下空白。在所有这些页面上,都存在具有相同名称的字段。我在代码后面使用的代码块如下:

        //If the User fields are empty, set them to the current user
        string ActiveUser = System.Web.HttpContext.Current.User.Identity.Name;
        string LoginID = ActiveUser.Right(6);
        var txtLoadedBy_chk = string.IsNullOrEmpty(str5);
        if ((txtLoadedBy_chk == true))
        {
            str5 = LoginID;
        }
        var txtUpdatedBy_chk = string.IsNullOrEmpty(str7);
        if ((txtUpdatedBy_chk == true))
        {
            str7 = LoginID;
        }
        var txtFlgUpdatedBy_chk = string.IsNullOrEmpty(str9);
        if ((txtFlgUpdatedBy_chk == true))
        {
            str9 = LoginID;
        }
        // If the date fields are NULL, set them to today's date
        var txtLoadedOn_chk2 = string.IsNullOrEmpty(str6);
        if ((txtLoadedOn_chk2 == true))
        {
            str6 = DateTime.Today.ToString();
        }
        var txtUpdatedOn_chk2 = string.IsNullOrEmpty(str8);
        if ((txtUpdatedOn_chk2 == true))
        {
            str8 = DateTime.Today.ToString();
        }
        var txtFlgUpdatedOn_chk2 = string.IsNullOrEmpty(str10);
        if ((txtFlgUpdatedOn_chk2 == true))
        {
            str10 = DateTime.Today.ToString();
        }
        // Check to make sure the dates entered are valid.  If not, let the user know and
        //   then exit out of the code so the record is not saved
        var txtLoadedOn_chk = DateTimeHelpers.IsValidSqlDateTimeNative(str6);
        var txtUpdatedOn_chk = DateTimeHelpers.IsValidSqlDateTimeNative(str8);
        var txtFlgUpdatedOn_chk = DateTimeHelpers.IsValidSqlDateTimeNative(str10);
        if ((txtLoadedOn_chk == false) || (txtUpdatedOn_chk == false) || (txtFlgUpdatedOn_chk == false))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "<script>alert('WARNING !! One of your date fields is invalid')</script>");
            return;
        }

我在想,既然我对所有这些形式做了完全相同的检查,我应该能够把这个代码块放在某个地方,只是引用它,而不是把它放在每个形式。我该怎么做呢?如果我把它放在一个单独的页面或CS文件,它将如何知道哪个表单调用它,所以它读取适当的字段?如果您能提供一些示例代码,那将是一个巨大的帮助。蒂娅!

哦,DateTimeHelpers是我创建的一个类,如果你想知道的话。

编辑:我只是想弄清楚一些事情;该代码位于代码隐藏中,当用户按下"Save"按钮时调用。它只是在试图将数据写入SQL Server表之前验证数据。

在多个页面上重复任务

这里要做的是创建一个用户控件。你可以使用Visual Studio提供的"新建文件"功能将其中一个添加到你的项目中。在这个答案中,我将称该用户控件为Fields.ascx

您的答案中的代码进入该用户控件(Fields.ascx.cs)的代码隐藏。表单元素,你有在每一页去Fields.ascx

一旦你这样做了,你引用用户控件在你的页面的顶部,像这样:

<%@ Register TagPrefix="user2174085" TagName="MyFields" Src="~/Path/To/Fields.ascx" %>

然后使用以下代码将字段添加到页面的正确位置:

<user2174085:MyFields runat="server" />

<%@ Register %>部分,你可以使TagPrefixTagName几乎任何你想要的,只要确保他们匹配当你使用控制

您总是可以在cs文件中编写一个公共方法,并将控件作为引用变量传递。下面是一个简短的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace mol_intranet
{
    public class ClassTesting
    {
        //pass the fields to be checked/updated here as ref variables
        public void checkFields(ref TextBox loadedby, ref TextBox updatedby)
        {
            string ActiveUser = System.Web.HttpContext.Current.User.Identity.Name;
            string LoginID = ActiveUser.Right(6);
            var txtLoadedBy_chk = string.IsNullOrEmpty(loadedby.Text);
            if ((txtLoadedBy_chk == true))
            {
                loadedby.Text = LoginID;
            }
            var txtUpdatedBy_chk = string.IsNullOrEmpty(updatedby.Text);
            if ((txtUpdatedBy_chk == true))
            {
                updatedby.Text = LoginID;
            }
        }
    }
}

然后在你的代码中调用这个方法并传递你的控件:

    ClassTesting t = new ClassTesting();
    t.checkFields(txtLoadedBy, txtUpdatedBy);

希望这对你有帮助。