views:

33

answers:

3

I'm trying to do a javascript postback and then re-direct to a different page but it keeps posting back to the current page

Here's my code

$(this).prepend('<a class="booknow2 sidelink sidelinkNew" href="javascript:__doPostBack(\'SetSess\',\'\')"><img src="../../images1/button/leftEdge.png" width="4" height="35" style="float:left; margin:0; padding:0;" alt="book now" /><img src="../../images1/button/rightEdge.png" width="4" height="35" style="float:right; margin:0; padding:0;" alt="book now" /><span>Check availability &raquo;</span></a>');

And here's my SetSess postback command

Sub SetSess()

Session("TenHolStDateNewCheck") = "%"

response.redirect("searchresults/default.aspx")

End Sub

Anyone got any ideas?

Thanks

Jamie

A: 

Hey,

__doPostBack the first parameter populates the __EVENTTARGET parameter... I could be wrong, but I wasn't aware that it automatically invoked methods. I think you have to do that yourself by doing in Load event:

if (request.Form["__EVENTTARGET"] = "SetSess") Then
    SetSess()
End if

HTH.

Brian
Where would I need to put that code? ASP page_load?
Jamie Taylor
Brian This idea works also.
Aristos
Page Load yes, or init would work too.
Brian
A: 

You need to have a hidden button link so the page product the permission to accept this post back

    <asp:LinkButton ID="LinkButton1" runat="server" 
         onclick="LinkButton1_Click" style="display:none;"></asp:LinkButton>

Then you can call your post back.

__doPostBack('<%=LinkButton1.ClientID%>','');

and your code be something like that.

$(this).prepend('<a class="booknow2 sidelink sidelinkNew" 
    href="javascript:__doPostBack(\'<%=LinkButton1.ClientID%>\',\'\')">
    <img src="../../images1/button/leftEdge.png" width="4" height="35" 
    style="float:left; margin:0; padding:0;" alt="book now" /><img src="../../images1/button/rightEdge.png" 
    width="4" height="35" style="float:right; margin:0; padding:0;" 
    alt="book now" /><span>Check availability &raquo;</span></a>');

... and the function on code behind

protected void LinkButton1_Click(object sender, EventArgs e)
{
 // Here the work and the redirect of you
}

Hope this work.

Aristos
I seem to be getting a end of statement error when putting the protected void in?
Jamie Taylor
A: 

I used the jquery ajax function to call a page which set the session and then re-directed

Thanks for the help though

Jamie Taylor