2011-11-23 1 views
2

UpdatePanel이 업데이트되는 동안 업데이트하는 동안 JQuery UI 대화 상자를 표시하는 ClientScript.RegisterOnSubmitStatement에 연결된 함수가 있습니다 (업데이트하는 데 시간이 걸립니다). 페이지에는 2 개의 버튼이 있으며 클릭 한 버튼에 따라 대화 상자에 다른 텍스트를 표시하고 싶습니다.페이지를 제출하는 버튼을 찾는 방법은 무엇입니까?

서버 측

ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen", "ShowSplashScreen(this)"); 

클라이언트 측

function ShowSplashScreen(source) { 
// Do stuff depending on the button that was clicked 
} 

는 현재, "소스"는 DOM 창없는 버튼입니다 : 이런 식으로 뭔가를 할 수있는 방법이 있나요.

+0

당신은 항상'hidden' HTML 필드를 사용할 수 있습니다 버튼 ID를 포함하는 당신이 양식 제출 후 결정할 수 있습니다. – ServAce85

+0

어떤 단추를 클릭했는지에 따라 시작 화면의 텍스트를 변경하기 때문에 양식을 제출하기 전에 필요합니다. 그것은 클라이언트 유효성 검사 후 클라이언트 측에서 발생해야합니다 (이것이 RegisterOnSubmitStatement 일 필요가있는 이유입니다).하지만 asyc 포스트 백 중에 표시 할 모달 스플래시 화면이 필요하기 때문에 양식이 실제로 제출되기 전에 필요합니다. – user1004944

답변

3

당신은 포스트 백을 시작한 제어 찾기 위해 __EVENTTARGET를 사용할 수 있습니다

/// <summary> 
/// Retrieves the control that caused the postback. 
/// </summary> 
/// <param name="page"></param> 
/// <returns></returns> 
private Control GetControlThatCausedPostBack() 
{ 
    Control ctrl = null; 

    //use the event target to get the control that initiated the postback 
    string ctrlName = Page.Request.Params.Get("__EVENTTARGET"); 
    if (!String.IsNullOrEmpty(ctrlName)) 
     ctrl = Page.FindControl(ctrlName); 

    //return the control to the calling method 
    return ctrl; 
} 
+0

+1 그게 굉장하고, 그걸 쓸 수있을 줄은 몰랐다. –

+0

@rick schott : 포스트 백 후에 동적 컨트롤의 탭 위치를 유지하려고 할 때 나는 잠깐 생각 나는 멋진 멋진 트릭이다. –

+0

나는 이것을 필요로한다. 서버 측이 아닌 클라이언트 측 – user1004944

0

확인을, 나는 해결 방법을 알아 냈어. 나는 처음에는 짧은 설명을하고, 앞으로는이 페이지를 볼 수있는 장래의 사람들을 위해 훨씬 더 긴 설명을 할 것입니다. 왜냐하면 저를 좋아하는 취미 자들이 앞뒤로이 물건을 알지 못할 수도 있기 때문에 이것을 사용하여 도움을 얻을 수 있기 때문입니다. (왜냐하면 나는이 물건을 인터넷 검색으로 살고 있기 때문이다.)

내가 찾던 기능은 클릭 한 버튼에 따라 innerHTML이 다른 모달 div가있는 것이었다. 또한 페이지가 유효하고 페이지의 모든 버튼이 유효성 검사를 수행하지 않을 경우에만 div를 표시하기를 원했습니다.

운동은 "ButtonClicked"라는 전역 변수를 만드는 것이 었습니다. 그런 다음 페이지의 모든 버튼에 ButtonClicked 변수를 버튼의 id로 설정하는 onclick 속성이 할당 된 javascript가 있어야합니다. onclick에 할당 된 스크립트는 페이지 유효성 검사 전에 실행됩니다. 그런 다음 ClientScript.RegisterOnSubmitStatement를 사용하여 페이지의 유효성을 검증 한 후 실제로 페이지를 제출하기 직전에 호출 할 함수를 지정했습니다. 그런 다음 "ButtonClicked"이벤트에 액세스하여 방금 호출 된 버튼을 확인한 다음 모달 div의 innerHTML을 변경 한 다음 표시합니다. (그런 다음 asyc 포스트 백을 수행 한 다음 포스트 백 이후에 모달 div를 제거하십시오.)

모달 div의 innerHTML을 단추가 자신을 호출하는 함수에서 설정하지 못하는 이유는 무엇입니까? 왜냐하면 내가 삽입하고있는 innerHTML은 유효한 페이지에 달려 있으며 ShowSplashScreen 기능을 사용할 때 페이지가 유효한지 만 알 수 있습니다. 당신은 왜 버튼이 호출하는 javascript 함수 내에서 유효성을 검사 할 페이지를 호출하지 않는지 물어볼 수도 있습니다. 이렇게하면 유효성 검사가 두 번 호출되므로 페이지에 대한 정보가 너무 많아서 페이지를 확인하는 데 거의 1 초가 걸리며 클라이언트 측 유효성 검사 함수 자체에 한 번만 호출 할 수있는 몇 가지 사항이 있으므로 유효성 검사 도중.

결과적으로이 모든 결과는 function1과 function 2가 모두 유효성 검사 전에 클릭 될 때 해당 버튼에 의해 호출되며 ShowSplashScreen은 유효성 검사 후 (페이지가 유효 할 때만) 버튼을 호출 한 다음 전역 변수에 액세스하여 어떤 변수가 클릭되었는지 확인하십시오.

그래서 전체 것은 같을 것이다 :

HTML

<!-- This is a simplified version of the HTML that <asp:Button> is going to 
    output in the actual HTML --> 
<input type="submit" id="Button1" value="Load Page" onclick="function1(this)"> 
<input type="submit" id="Button2" value="Save Page" onclick="function2(this)"> 

자바 스크립트

var ButtonClicked = ""; //Needs to be global 

//It is not necessary to have a different function for each button, 
//I just needed to for the needs of my page. If Button2 calls function1 
//instead of function2, ButtonClicked is still set to "Button2". 
//It is very important that EVERY button on the page calls something 
//that sets ButtonClicked or you will get an bug if the user ever 
//clicks a button that sets ButtonClicked, and then clicks a button 
//that does not set ButtonClicked (the final function will still 
//think that the first button had just been clicked) 

function function1(source){ 
    ButtonClicked = source.id; 
    //whatever else Client Script that needs to be run from this button 
} 

function function2(source){ 
    ButtonClicked = source.id; 
    //whatever else Clinet Script that needs to be run from this button 
} 

function ShowSplashScreen(){ 
    if(ButtonClicked == "Button1") 
     //Use JQuery to access the dialog <div> and set the innerHTML 
     //to whatever 
    else if(ButtonClicked == "Button2") 
     //Use JQuery to access the dialog <div> and set the innerHTML 
     //to something else 
} 

서버 측이

//Use this code to set a function to be called after the page has 
//been successfully validated. If a button does not cause validation, 
//then that button will always call the function set here 
// 
//You should also check to see if the script has already been registered 
//for speed purposes, but I'm just demonstrating particular 
//functionality here. 
protected void Page_Load(object sender, EventArgs e) 
{ 
    ClientScript.RegisterOnSubmitStatement(this.GetType(), "ShowSplashScreen", 
              "ShowSplashScreen()"); 
}