2012-08-17 3 views
0

이봐, 난이 모두 여기에이 코드 :웹 UI 컨트롤 클래스에 전달되는

ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "showNotifier", ";$(function() {showNotifier(3000,'#cf5050','" & msg & "');});", True) 

난과 같이 클래스 파일 내부에 배치 걸려 :

Public Class topMsgNotifyer 
    Public Shared Sub show(ByVal delay As Integer, ByVal colorOfBox As String, ByVal message As String) 
     Try 
      ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "showNotifier", ";$(function() {showNotifier(" & delay & ",'" & colorOfBox & "','" & message & "');});", True) 
     Catch ex As Exception 
     End Try 
    End Sub 
End Class 

그리고 물론 클래스 파일 안에있는 Me.Page에 문제가 있습니다.

내가이 일을하여 현재 페이지의 이름을 얻을 수 있습니다 : 페이지 자체에 호출 대신 asp.net 페이지에서 해당 클래스의 서브를 호출 할 때

Dim pageName = Path.GetFileName(Request.Path) 

가 어떻게이 문제를 해결 할 수 있습니다?

답변

2

Page 개체를 show() 메서드에 전달할 수 있지만 더 나은 방법은 페이지가 상속되는 BasePage 클래스 인 클래스를 만드는 것입니다. 그 수업에서 show() 메소드를 만들자. 따라서이 BasePage를 상속받은 모든 페이지는 해당 메서드에 액세스 할 수 있으며 Me.Page를 사용할 수 있습니다.

Page 개체를 전달 :

Public Class topMsgNotifyer 
    Public Shared Sub show(ByVal delay As Integer, ByVal colorOfBox As String, ByVal message As String, ByVal page as System.Web.UI.Page) 
     Try 
      ScriptManager.RegisterClientScriptBlock(page, GetType(String), "showNotifier", ";$(function() {showNotifier(" & delay & ",'" & colorOfBox & "','" & message & "');});", True) 
     Catch ex As Exception 
     End Try 
    End Sub 
End Class 

전화를 :

topMsgNotifyer.show(30, "Red", "You did something wrong", Me.Page) 
+0

페이지 개체를 어떻게 전달합니까? – StealthRT

+0

권하지 않습니다. 페이지를 전달하고 기본 클래스를 만드는 것을 보여줄 수 있습니까? – TheGeekYouNeed

+0

그게 효과가 있었어. 감사합니다 TheGeekYouNeed – StealthRT

1

는 것은 이러한 종류 데는 보통 페이지에 액세스 할 수 있습니다 사용자 컨트롤에서 수행된다.

다른 옵션으로 Page 변수를 매개 변수로 함수에 전달해야합니다.
또는 함수 대신 스크립트로 String을 반환 한 다음 수행중인 것처럼 RegisterClientScriptBlock을 호출하십시오.

1

사람들이 페이지의 인스턴스를 전달하지 않는다고 말하는 이유는 무엇입니까? 나는이 문제 아무것도 볼 :

public static void clientOnLoadScript(System.Web.UI.Page instance, string script, bool addScriptTag = false) 
{ 
    string toWrite = "$(window).load(function() { \n" + script + "\n });"; 

    instance.ClientScript.RegisterStartupScript(instance.GetType(),"OnLoad", toWrite,addScriptTag); 
} 

그럼 그냥 함수를 호출 :

clientOnLoadScript(this,"script",true); 

이 나를 위해 작동합니다. 행운을 빕니다.

+0

페이지는 꽤 큰 물체입니다. 통과 할 필요가 없습니다. – TheGeekYouNeed