5

I'm creating project in ASP.NET (Framework 4.0). I have used Asp LinkButton in Master Page & it has 2 page linked with it (Home.aspx & service.aspx).

Question As follows : That LinkButton1 works on Home.aspx and doesn't work on service.aspx.

User.master code as follow

<ul class="nav navbar-nav navbar-right">
    <li>
        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" AutoPostBack="true">Signout  
            <i class="glyphicon glyphicon-off"></i>
        </asp:LinkButton>
    </li>
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
            <span>
                <asp:Label ID="lblName" runat="server" Text=""></asp:Label>
            </span>
            <i class="icon-user fa"></i>
            <i class=" icon-down-open-big fa"></i>
        </a>
        <ul class="dropdown-menu user-menu">
            <li class="active">
                <a href="frmUserHome.aspx">
                    <i class="icon-home"></i> My Account 
                </a>
            </li>
            <li >
                <a href="frmUserHome.aspx">
                    <i class="icon-home"></i> Personal Home 
                </a>
            </li>
            <li>
                <a href="#">
                    <i class="icon-hourglass"></i> Pending approval 
                </a>
            </li>
        </ul>
    </li>
</ul>

User.master.cs code for LinkButton1 Click

protected void LinkButton1_Click(object sender, EventArgs e)
    {

         if (Request.Cookies["ASP.NET_SessionId"] != null)
         {
             Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
             Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-20);
         }
         FormsAuthentication.SignOut();
         Session.Abandon();
         Response.Redirect("~/Default.aspx");
    }

While inspect element (using Chrome Browser )on Home.aspx page i found below code

<li>
    <a id="ctl00_LinkButton1" autopostback="true" href="javascript:__doPostBack('ctl00$LinkButton1','')">Signout  
        <i class="glyphicon glyphicon-off"></i>
    </a>
</li>

and while on service.aspx (Chrome Browser inspect element)

<li>
    <a id="ctl00_LinkButton1" autopostback="true" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$LinkButton1", "", true, "", "", false, true))'>Signout  
        <i class="glyphicon glyphicon-off"></i>
    </a>
</li>

Why their is difference between Home.aspx & service.aspx (while inspect element through chrome browser) ?

Ronp
  • 199
  • 3
  • 15
  • 1
    Did you check [What does webform_DoPostBackWithOptions() do?](http://stackoverflow.com/questions/20499444/what-does-webform-dopostbackwithoptions-do)? Is there any server side code on service.aspx page which is modifying postbackoptions of the LinkButton1? – Techie Feb 05 '16 at 17:49
  • Their is no such kind of code on service.aspx – Ronp Feb 05 '16 at 17:53
  • Check for javascript errors – Aristos Feb 05 '16 at 19:02
  • @Nimesh , Thnaks for link [does webform_DoPostBackWithOptions() do](http://stackoverflow.com/questions/20499444/what-does-webform-dopostbackwithoptions-do) – Ronp Feb 06 '16 at 03:37

1 Answers1

0

Thanks for Original Answer writer & thanks for @Nimesh for providing link .

Set the PostBackUrl property for the LinkButton server control, then it means it is cross page posting and then asp.net framework instead of normal __DoPostBack() adds "WebForm_DoPostBackWithOptions".

I'hv made some changes according to my code requirement .

Code for LinkButton1 (User.master) page

    <li>
    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"  PostBackUrl="~/Default.aspx">Signout 
        <i class="glyphicon glyphicon-off"></i>
    </asp:LinkButton>
    </li>

If in your case you have not set the "PostBackUrl", then ASP.NET framework also does not add this by default for Button Control, so this means there has to be another control setting the OnClick attribute value probably using following sever side code .

Code for User.master.cs

protected void LinkButton1_Click(object sender, EventArgs e)
    {

         if (Request.Cookies["ASP.NET_SessionId"] != null)
         {
             Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
             Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-20);
         }
         FormsAuthentication.SignOut();
         Session.Abandon();
         PostBackOptions myPostBackOptions = new PostBackOptions(this);
         myPostBackOptions.ActionUrl = "~/Default.aspx";
         myPostBackOptions.AutoPostBack = false;
         myPostBackOptions.RequiresJavaScriptProtocol = true;
         myPostBackOptions.PerformValidation = true;

         // Add the client-side script to the HyperLink1 control.
         LinkButton1.OnClientClick = Page.ClientScript.GetPostBackEventReference(myPostBackOptions);
         Response.Redirect("~/Default.aspx");
    }
Community
  • 1
  • 1
Ronp
  • 199
  • 3
  • 15
  • Glad that the original post helped you. I'm still confused why it behaved differently on both the child forms without `PostBackUrl` being set in master page? If this has solved your problem you can mark it as accepted. – Techie Feb 06 '16 at 05:44