0

I have a .aspx Webpage with a UserControl added on it.In UserControl when the LinkButton is clicked it do not Postback on first attempt. but when we click again it does the Postback and then only the page redirects don't know why?

Any idea?

In .ASPX Markup:

 <asp:LinkButton ID="lnkCheckOut" runat="server" 
                        CssClass="button orange" onclick="lnkCheckOut_Click">Checkout</asp:LinkButton>

In.cs file:

protected void lnkCheckOut_Click(object sender, EventArgs e)
    {
        if (Session["UserID"] != null)
        {
            lnkCheckOut.PostBackUrl = "~/checkout.aspx?type=checkout";
            //Response.Redirect("~/checkout.aspx?type=checkout");
            Session["IsQuoteAdded"] = "false";
        }
        //if not logged in user
        else
        {
           lnkCheckOut.PostBackUrl = "~/login.aspx?returnUrl="+HttpUtility.UrlEncode(Request.RawUrl);
        }
    }

When i see markup in browser(using F12 in Chrome) on first click it shows:

<a id="ctl00_ContentPlaceHolder1_shpCart_lnkCheckOut" class="button orange" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$shpCart$lnkCheckOut','')">Checkout</a>

On Second Click:

<a id="ctl00_ContentPlaceHolder1_shpCart_lnkCheckOut" class="button orange" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$shpCart$lnkCheckOut", "", false, "", "login.aspx?returnUrl=%2fNew%2fMyBox.aspx", false, true))'>Checkout</a>

Note:I am not using any UpdatePanel in the Webpage or UserControl.

Help Appreciated!

3
  • I can't be sure why it's happening,but an alternate way to this can be that you use __doPostback function in the onclick event of link button in javascript. Commented Oct 10, 2013 at 10:16
  • Hi Neelam, but what about the session validation? Commented Oct 10, 2013 at 10:25
  • i had this kind of problem way back and this is the solution i used in the end when i couldn't find anything else.you will also need to add EnableEventValidation = false in your .aspx page.I'm not sure about the session validation.You should look to solve this,keep my suggestion as the last resort. Commented Oct 10, 2013 at 10:49

3 Answers 3

1

Your code is not redirecting the page it has just assigning the URL. Use below codes to rectify that.

protected void lnkCheckOut_Click(object sender, EventArgs e)
{
    if (Session["UserID"] != null)
    {
        //lnkCheckOut.PostBackUrl = "~/checkout.aspx?type=checkout";
       Session["IsQuoteAdded"] = "false";
        Response.Redirect(@"~/checkout.aspx?type=checkout");

    }
    //if not logged in user
    else
    {
       Response.Redirect(@"~/login.aspx?returnUrl="+HttpUtility.UrlEncode(Request.RawUrl));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

hi @Avinash Jha, i have already mentioned in above comment that it wont work
0

In your markup, there is no PostBackUrl. So on the first click, it will actually post back to the same page, and your event handler will run.

Then, in your event handler, you are setting the PostBackUrl.

So the second time somebody clicks the link, it will post to that URL. Your code is working as designed :)

Edit: I would suggest changing to Response.Redirect, but it's hard to know exactly what your code is supposed to do.

1 Comment

hi @pattermeister, so what is your recommendation to handle this situation? can we do this in aspx condition markp? any sample code help appreciated! also tried with Response.Redirect() but yet wont work so commented in posted code! anything else?
0

Same issue i was facing. I have link button in grid view. when i clicked the link button its not postback on first click ,but when i clicked again it does. Then i check my code properly and then i find that grid view is placed in update panel hence its not postback on first click.

So i would suggest please check the same.

1 Comment

It's more likely a comment to other post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.