简体   繁体   English

LinkBut​​ton不会在asp.net中的第一次单击上回发吗?

[英]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. 我有一个.aspx网页,上面添加了一个UserControlUserControlclicked LinkButton时,第一次尝试不要Postback but when we click again it does the Postback and then only the page redirects don't know why? 但是当我们再次click时,它会执行Postback ,然后只有页面重定向不知道为什么?

Any idea? 任何想法?

In .ASPX Markup: 在.ASPX标记中:

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

In.cs file: In.cs文件:

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: 当我第一次点击浏览器(在Chrome中使用F12)看到标记时,它显示:

<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. 注意:我没有在网页或UserControl中使用任何UpdatePanel。

Help Appreciated! 帮助赞赏!

Your code is not redirecting the page it has just assigning the URL. 您的代码不会重定向仅分配了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));
    }
}

In your markup, there is no PostBackUrl. 在您的标记中,没有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. 然后,在事件处理程序中,设置PostBackUrl。

So the second time somebody clicks the link, it will post to that URL. 因此,有人第二次单击该链接时,它将发布到该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. 编辑:我建议更改为Response.Redirect,但是很难确切知道您的代码应该做什么。

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. 所以我建议请检查相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM