인트라넷 웹 사이트에서 ASP.NET과 함께 C# 사용. 여러 개의 "Admin"페이지가있어 필드를 검사하여 날짜가 있는지 확인하고 필드를 확인하여 사용자 이름이 있는지 확인한 다음 필드가 비어 있으면 채 웁니다. 이 모든 페이지에는 동일한 이름의 필드가 있습니다. 내가 코드 숨김에서 사용하고 코드 블록은 다음과 같습니다 :여러 페이지에 반복되는 작업이 있습니까?
//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는 내가 만든 클래스입니다.
편집 : 나는 단지 뭔가 명확히하고 싶다. 이 코드는 코드 숨김에 있으며 사용자가 "저장"버튼을 누르면 호출됩니다. 데이터를 SQL Server 테이블에 쓰기 전에 데이터를 확인하는 것입니다.