tags:

views:

488

answers:

2

I am having an ASP.NET page with one Asp.net button control and a normal html link (anchor tage) I want to invoke the postbackl event of asp.net button control when someone clicks on the link.

I used the below code

 <a href="javascript:myFunction();" class="checkout" ></a>
<asp:Button ID="btnCheckout" runat="server" Visible="false" 
onclick="btnCheckout_Click" />

and in my javascript i have

 function myFunction() 
    { 
        var strname; 
        strname = "Test"; 

        __doPostBack('btnCheckout','OnClick');
    }

But when runnin gthis , i am getting an error like __doPostBack is undefined

Can any one tell me why it is ?

Thanks in advance

+1  A: 

This anyway wouldn't have worked. When you make your .NET control invisible by using 'Visible="false"' it isn't rendered, that means not available at the client.

Back to your question. 1- Where is myFunction defined? Between the tag? 2- Are there more .NET controls on the page? If there aren't any other .NET controls, .NET doesn't add all the scripts that are required for postbacks and stuff.

Why not do the following (based on TheVillageIdiot answer):

<asp:LinkButton ID="lbtnCheckout" runat="server" CausesValidation="false" OnClick="lbtnCheckout_Click" CssClass="checkout" />

With the above example you don't need the fake button and make it invisble. You still can do your postback. Way more cleaner approach I would say.

Joop
A: 

First of all I tried your code and also not get anything like __doPostBack, then I added another button on the page which was visible but it was all the same. Then I added a LinkButton and got __doPostBack method. You can do post back from javascript but then EventValidation is problem, as it does not allow this kind of thing. I had to use the following to overcome it and it worked:

    protected override void Render(HtmlTextWriter writer)
    {
        ClientScript.RegisterForEventValidation(
                  new PostBackOptions(btnCheckout, "OnClick"));
        base.Render(writer);
    }

I think I'm bit incoherent in answering so I'll mark it as wiki :)

TheVillageIdiot