2013-05-13 1 views
3

인트라넷 웹 사이트에서 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 테이블에 쓰기 전에 데이터를 확인하는 것입니다.

답변

1

항상 공개 메서드를 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); 

희망 여기에 그 모습 수 있는지에 대한 간단한 예입니다.

2

여기서 원하는 것은 사용자 정의 컨트롤을 만드는 것입니다. 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 거의 아무것도 할 수 있습니다.

관련 문제