In EndRequest, sender._postBackSettings.sourceElement will give you information about which element's partial postback is completing.
Thanks, that helps alot! The only draw back I see is that it only reports the id if the control is inside an updatepanel, is it possible for it to report the id of a control such as a button, that is outside an updatepanel?
edit: Sorry, I think I missunderstood. I need to know which control initially caused the postback, not which one is being updated.
On EndRequest, sourceElement will let you know which element triggered the partial postback that has, at this point, come to an end. I'm pretty sure that's what you're asking, right?
Also, it will be valid regardless of the location of the trigger. You can place a Button outside of an UpdatePanel, set it as an async trigger, and it'll come through sourceElement the same as if it were inside the UpdatePanel.
Ok, this is an odd result then. I have a button that causes the postback, but sourceElement is the value of the textbox1, which is the control beign updated. The click is being triggered a little abnormal. The button actually has a style of "hidden" so it is not visible. Button.click() is called through a javascript. The javascript function that calls document.getElementById('Button1').click() is triggered when an image is clicked (javascript: onclick), and when the enter button is clicked in the textboxl (onkeyup). When the image is clicked, sourceElement is undefined, but when "enter" is his inside the textbox (which is inside an updatepanel), i get "Textbox1" back as the sourceElement.
edit: Ok, i think I figured out why the TextBox1 is being reported, but not the image. I am assuming that it is because with the textbox, I wired up the onkeyup event through the javascript xAddEventListener function, and with the image I just added "onclick" to the images properties. Am i on the right track? Below is the javascript I am using for the textbox.
function calledKey(e) { if(e.target) { if (e.target.id && e.target.id != "") { if(e.target.id=='textbox1') { if( (e.keyCode == 13) & (e.keyCode != 8)) { HiddenBtnClick(); } } } } else { if( (e.keyCode == 13) & (e.keyCode != 8)) { HiddenBtnClick(); } }} function xGetElementById(e){ if(typeof(e)=='string') { if(document.getElementById) e=document.getElementById(e); else if(document.all) e=document.all[e]; else e=null; } return e;} function xAddEventListener(e,eT,eL,cap){ if(!(e=xGetElementById(e)))return; eT=eT.toLowerCase(); if(e.addEventListener)e.addEventListener(eT,eL,cap||false); else if(e.attachEvent)e.attachEvent('on'+eT,eL); else { var o=e['on'+eT]; e['on'+eT]=typeof o=='function'?function(v){ o(v); eL(v); }:eL; }} xAddEventListener(document,'keyup',calledKey,false);
edit 2: Ok, i tried adding:
xAddEventListener(document,'click',callJSClick,true);
function callJSClick(e)
{
if(e.target)
{
if(e.target.id && e.target.id != "")
{
if(e.target.id == 'm_SendImg')
{
HiddenBtnClick();
}
}
}
else
{
alert("Else Triggered")
}
}
and still got nothing. My EndRequestHandler looks like this:
function EndRequestHandler(sender, args)
{
alert(sender._postBackSettings.sourceElement.id);
}
the alert is only triggered when an async postback is triggered by "textbox1", when it my image is clicked i get a JS error: Error: sender._postBackSettings.sourceElement has no properties
any ideas?
You aren't doing anything I can see to cancel the postback generated by hitting enter. So, even though you're firing that button, you're getting another postback from the enter key submitting the form on the browser (which is why the source element is showing at the textbox -- that's the postback that's taking precedence). Try something like this:
$addHandler($get('textbox1'), 'keydown', textbox1_keydown);
function textbox1_keydown(evt)
{
if (evt.keyCode == 13)
{
evt.preventDefault();
HiddenBtnClick();
}
}Also, I've found that _doPostBack() is more reliable for triggering postbacks than using hidden buttons. Something easy like __doPostBack('HiddenButtonClientID', '') will trigger a partial postback if HiddenButtonClientID is an async trigger of an UpdatePanel.
do i use the code above in place of the xAddEventListener, or in addition to it?
I believe you can replace it.
Does it matter where it is placed? I tried just replacing it (in the html Head), but nothing was triggered. Also, are you suggesting replacing documuent.getElementById("button1").click() with __doPostBack('HiddenButtonClientID', '') in the HiddentBtnMethod?
Did you get any JavaScript errors? $addHandler may need to either be called in an AppInit handler, or placed below the ScriptManager. It may not be available yet in the head.
Yes, you can completely replace the .click() with __doPostBack(). Often you can circumvent the extra hidden control completely, for example:http://encosia.com/index.php/2007/07/13/easily-refresh-an-updatepanel-using-javascript/
ok, thanks so much for your help. I appreciate it. The only thing I have failed to clear up is the keyup event for the text box, it fires the first time "enter" is hit, but any subsequent hits do not fire the event. The textbox is a multiline textbox (i know it renders differently than a single line one). I have put alert() in the event method, and sure enough it doesnt make it. (no js errors either)
What happens the first time the event fires? If it's a partial postback that affects the textbox, your handler may be getting lost when the new HTML comes in for the UpdatePanel. You can move your $addHandler to a pageLoad() function to re-hook it after each partial postback.
you were right, the event was needing to be rehooked. Your idea of readding it in pageLoad() worked fine in IE, but didnt play well in Firefox. To get around it I just rehook the event in the Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
this seemed to work perfect in both IE and FF, and works in my situation as I am only allowing for one async request out at a time for a specific updatepanel (I am using beginRequest and endRequest to track when this happens)
Thank you so much for your time, and if you are ever in Tulsa,Ok let me know and I will buy you a drink.
One last question. I have a timer on the page, and the updatepanel's load event is fired with each tick even if the updatepanel is not wired to the tick event. I have two updatepanels, one wired to the tick event, the other uses _dopostback() (as discussed in this thread). The update panel wired to the tick event is sent to udpate always and the one without the tick event is set to conditional. Is there a way to prevent the "conditional update/not wired to tick event" updatepanel's load event from firing?
You could check Request["__EVENTTARGET"] in your conditional/non-timer UpdatePanel's load event, and only execute your code if it matches your UpdatePanel's ClientID.
No comments:
Post a Comment