开发者

Autopostback and doPostBack conflict

I have enabled the AutoPostBack property of one of my form's textboxes, which according to w3 schools should only trigger a postback when I press enter or tab.

I am also calling a __doPostBack() on pageLoad, given a user's answer to a javascript prompt. When I do that, the Request.Form['__EventTarget'] is not what开发者_JAVA技巧 I set it to be in the call to __doPostBack.

The real issue to me is that if I set the TextBox's AutoPostBack attribute to false, the problem with the pageload __doPostBack call goes away. This is not behavior I expected. Any ideas about what is causing the problem? Why would the AutoPostBack enabled have any influence?

Here is some of the code:

asp:TextBox runat="server" ID="userName" OnTextChanged="UpdateTable" AutoPostBack="true"
script type="text/javascript"
        //![CDATA[
        var theForm = document.forms['form1'];
        if (!theForm) {
            theForm = document.form1;
        }
        function __doPostBack(eventTarget, eventArgument) {
            if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
                theForm.__EVENTTARGET.value = eventTarget;
                theForm.__EVENTARGUMENT.value = eventArgument;
                theForm.submit();
            }
        }
        //]]
/script
input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value=""
input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value=""
input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" 
function jsPrompt(name) {
  var ans = confirm('really follow ' + name + '?');
  if (ans) {
    __doPostBack('follow', name);
  }
}

Then in the codebehind page:

if (Request.Form["__EventTarget"] == "follow")
  followPerson(Request.Form["__EventArgument"]);                

But, I keep getting that Request.Form["__EventTarget"] is ",", and I've stepped through the javascript in the debugger. Just before form.submit(), the arguments are not ","


OK... where to begin. What exactly are you trying to accomplish here? I can think of no legitimate reason to look at Request.Form["__EventTarget"] in ASP.NET. The only time I've seen this is when an old school ASP or PHP programmer first learns ASP.NET and hasn't quite grasped the event-driven model that underlies ASP.NET.

Also, what do you mean when you say you are calling __doPostBack()? You shouldn't need to do this manually either. If you really want to create a postback in javascript where it normally doesn't happen, you need to use ClientScriptManager.GetPostBackEventReference(). So, use this method and pass it your TextBox to get the proper postback code. Then you can execute it anywhere in your client side script.

Hope this helps. If I've misunderstood the scenario please add a comment and I'll try again. :)


Where is the code you are using? Make sure you are using something like:

var element = $get("<%= textbox1.ClientID %>");
__doPostBack(element.name, ''); // second param is command name/argument separated by $

HTH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜