2

I'm trying to initiate a postback invoking the click function of a server-running element, as to run some C# code, in the event of a particular jquery dialog button click.

I am using a modal dialog with buttons as per this jQuery UI example. I have attempted using all of the different answers for javascript/jquery postback invocations in this question.

I've set a couple of breakpoints in my C# code to see if these postbacks are getting called, but nothing is getting hit.

I have this dummy element in my ascx file to use:

<a id="anchorId" runat="server" onclick="return true;" onserverclick="TryLogin"></a>

I've attempted to get this postback to occur a few different ways:

$("#anchorId").click(); //just simply does nothing
document.getElementById("anchorId").click(); //This one gives me a null javascript error
$("document").getElementById("#anchorId").click(); //tells me [object] doesn't have a getElementById
__doPostBack('<%=anchorId.UniqueID %>', '');//Also does nothing in the jQuery code, but works in standard javascript code

Lastly I did try retrieving the unique ID in the code behind as:

string id = anchorId.UniqueID;

and replaced in the javascript this way:

__doPostBack('Softening_Main$anchorId', '');//Still does nothing, and no javascript error

I really need some help here, any ideas?

Community
  • 1
  • 1
Dan F
  • 17,654
  • 5
  • 72
  • 110

1 Answers1

3

There are couple of issues in your code.

$("document") will not select document instead will look for document tag element on the page. You should use $(document) removing the quotes.

Also getElementById is a JavaScript method of document object which is used to find element by id. When you use getElementById don't specify # in the id. # is used by jQuyer to differentiate between various selectors.

Remove inline onclick="return true;" from anchor and try this.

$("#anchorId").click(function(e){
    __doPostBack('<%=anchorId.UniqueID %>', '');
    return false;
});
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • `Asp.Net` generates a `UniqueID` which is the control's name appended with all naming containers seperated by colon, it is same as `ClientID`(id attribute of the control) which uses underscore as seperator. – ShankarSangoli Feb 15 '12 at 19:05
  • @DanF - Where do you place this code? Do you see any error on the page? – ShankarSangoli Feb 15 '12 at 19:07
  • I do not get any errors on the page or in my javascript console. in my ascx file I import a scripts file ` ` and in that file I have the dialog code: `$( "#dialog-form" ).dialog({ autoOpen: false, height: 150, width: 350, modal: true, buttons: { "Login": function() { $( this ).dialog( "close" ); $("#anchorId").click(function(e) { __doPostBack('<%=anchorId.UniqueID %>', ''); return false; }); return true; },...` – Dan F Feb 15 '12 at 19:09
  • Since this code is in a js file it is not working. Can you render anchors UniqueID in a variable from your main and and use that variable in this code. – ShankarSangoli Feb 15 '12 at 19:15
  • You mean replace `'<&=anchorId.UniqueID %>'` with its actual unique id gotten in the code behind? I did try that as well but that didn't work either – Dan F Feb 16 '12 at 15:20
  • Did you put the actual uniqueid that gets generated by asp.net? – ShankarSangoli Feb 16 '12 at 15:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7809/discussion-between-dan-f-and-shankarsangoli) – Dan F Feb 16 '12 at 15:37