SHEKHAR SHETE
SHEKHAR SHETE

Reputation: 6066

LinkButton doesn't Postback on first click in asp.net?

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!

Upvotes: 0

Views: 7631

Answers (3)

Appu
Appu

Reputation: 39

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.

Upvotes: 0

Avinash Jha
Avinash Jha

Reputation: 127

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));
    }
}

Upvotes: 1

pattermeister
pattermeister

Reputation: 3232

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.

Upvotes: 0

Related Questions