views:

100

answers:

2

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 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 ","

A: 

Hey,

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.

Brian
Actually, the postback doesn't specify a control. I just set the event target and event arguments to things that I can use (or could have used) in the codebehind page.
+1  A: 

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. :)

Bryan
Say somebody clicks a button, and the complex logic to decide what should be done is in the c#. There was a postback. Now, in certain cases, you might want to prompt the user if it's ok to go ahead and do what they suggested - the only way I know to do that is to use javascript, which means on pageload the javascript should check a hidden value that was set by the C# code. Now, if the user clicks "ok, that's what I wanted to do", then it should postback and do it. Plus, I don't see any reason *not* to call __doPostBack, since it's available to us to use.
Hmmmm... why not to call it manually... Maybe for the reason you posted about? As for doing a confirm before you postback... I usually do something like this on a button: OnClientClick="return confirm('Are you sure you want to delete this item?');" Which does not interfere or change the default postback behavior, just cancels it if the user clicks Cancel.
Bryan
I can see what you're saying, but I don't really want to go into whether or not I should (I believe I have good reason to do it the way I'm doing it.) I just want to know why the AutoPostBacking screws it up. Thanks for your help though.
Maybe it's because there are no events that would normally cause a postback from a TextBox? I have never used AutoPostBack with a textbox, so I'm not sure. Maybe you could hook OnTextChanged instead of relying on the __EventTarget?
Bryan
+1 For the discussion. I think the OnTextChanged is only triggered when something else posts the form back (which happens if you hit enter in the textbox too), where it then it checks to see if the text has changed and triggers that event if it has. By setting AutoPostBack to be true, it makes it so that if the user changes the text, then clicks outside it, a postback occurs (and then the OnTextChanged event will be handled.)