LinkButton not triggering on Master Page when accessed from Second Child Page in ASP.NET

I am currently working on a project in ASP.NET (Framework 4.0) where I have implemented an Asp LinkButton in the Master Page that is linked to two different pages (Home.aspx and service.aspx).

The question at hand is: The LinkButton1 functions properly on Home.aspx but not on service.aspx.

Code for User.master:

<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 handling LinkButton1 Click event:

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

Upon inspecting elements using Chrome Browser, it was observed that on Home.aspx page the following code was present

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

While on service.aspx, the following code was found during inspection:

<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 is there a discrepancy between the code for Home.aspx and service.aspx when inspected through the Chrome browser?

Answer №1

Gratitude to the original answer contributor and a special thanks to @Nimesh for sharing the link.

To enable cross-page posting in ASP.NET framework, set the PostBackUrl property for the LinkButton server control. Instead of the usual __DoPostBack(), "WebForm_DoPostBackWithOptions" is added automatically.

I have customized the code according to my specific requirements.

Code snippet for LinkButton1 on 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 you haven't set the "PostBackUrl", the ASP.NET framework won't add this by default for Button Control. In that case, another control must be setting the OnClick attribute value likely using the following server-side code.

Code within User.master.cs file:

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 client-side script to the LinkButton1 control.
     LinkButton1.OnClientClick = Page.ClientScript.GetPostBackEventReference(myPostBackOptions);
     Response.Redirect("~/Default.aspx");
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble retrieving the background color of an element when the mouse hovers over it in C# Selenium

I've encountered a strange issue in my code: var firstCategoryTitle = pageTypeCategoryDiv.FindElement(By.ClassName("result.firstCategory")); // this is definitely selecting the correct element Actions action = new Actions(Driver); action ...

Issue encountered while attempting to utilize setStart and setEnd functions on Range object: Unhandled IndexSizeError: Unable to execute 'setEnd' on 'Range'

Every time I attempt to utilize a range, an error message appears in the console: Uncaught IndexSizeError: Failed to execute 'setEnd' on 'Range': The offset 2 is larger than or equal to the node's length (0). This is the script I ...

Displaying the identical date on two separate Ajax Calendar Extender controls in Asp.net

I am facing an issue with some ajax calendar extenders. I have two similar calendars and two textboxes, each linked to one calendar. <asp:TextBox ID="txt1" runat="server"</asp:TextBox> <cc1:CalendarE ...

A guide on switching out an HTML element with an AJAX response

How can I dynamically replace an HTML element with Ajax response? I know how to remove the element, but I'm unsure how to then insert the new content from the Ajax call. For instance, let's say I have the following code: <ul id="products"> ...

Utilizing a custom function to filter Firestore collection data based on location proximity

I have a question about filtering a Firestore collection using a function where the values of the documents in the collection are used as arguments. Let's say we have a Firestore collection with documents structured like this: { pointOfInterest: "S ...

What is the best way to fetch images from a JSON object in a React application?

I have been trying to load images from an array of objects in React, but I keep encountering a "module not found" error. This issue has been frustrating me for the past couple of days. Can someone please help me troubleshoot this problem or provide some su ...

Kendo Template Function: Angular JS version 1.6

I'm working with a currency column that looks like this: { field: 'INVOICE_AMOUNT_ORIGINAL', title: $translate.instant('invoiceAmount'), format: '{0:n}', template: '#= currency(dataItem.INVOICE_AMOUNT_ORIGIN ...

Is there a way to modify the style value within AngularJS?

<div class="meter"> <span style="width: 13%"></span> </div> I have the following HTML code snippet. I am looking to update the width value using AngularJS. Does anyone have a solution for this issue? ...

Identify the mouse's location in relation to its parent element, not the entire webpage

A script I've been working on detects the mouse position relative to the page, taking into account a movement threshold of 100px before triggering certain actions. // Keep track of last cursor positions var cursorDistance = 0; var lastCursorX = null; ...

How to optimize updating object properties with setState?

Is there a more efficient way to rewrite this code? For example, would using setState(ContentStore.getAll()) achieve the same outcome? I believe this approach appears overly cluttered and any method to simplify the code for readability would be greatly app ...

Where can Vue.js be found?

After dedicating an hour to watching instructional YouTube videos on Vue.js, I am still struggling to grasp the language! In the past, I have worked with Node.js, Jquery, and Mongodb to develop websites... I believe that web applications require multiple ...

What is the best way to horizontally align my divs and ensure they stack properly using media queries?

My layout is not working as expected - I want these two elements to be side by side in the center before a media query, and then stack on top of each other when it hits. But for some reason, it's not behaving the way I intended. I've tried to ce ...

The subscription for the second Observable in RxJS concatMap is triggered individually

I am currently developing an Angular 6 application. I want the app to display a loading animation whenever there is a change in the route or if there are any pending HTTP requests. To achieve this, I have set up two Observables as follows: For httpPendingR ...

Insert data into a drop-down menu and organize it

When I choose a value in one Select, the other options are removed and the previous value is added to them. How can I use JQuery to add the previous value to Select and then sort it alphabetically? ...

Tips for redirecting a page in React by forcing a route

Attempting to implement the guidance from this Stack Overflow solution on how to "go back home" after closing a Modal... import React, { Suspense, useState } from 'react'; import { BrowserRouter, Route, Switch, useHistory } from "react-route ...

Exploring the world of React.js through the exchange and manipulation of data

Essentially, I am utilizing refs to obtain component dimensions in the componentDidMount() lifecycle method. When I log this information, I can see the width that I desire (refer to code). However, when I attempt to access and log this data in the render() ...

Proxy/firewall causing interference with socket connection from chrome extension

I have encountered an issue with my web app in JavaScript that connects to a socket using socket.io and a Chrome Extension that also connects in the same way to the same server. While everything works smoothly on most computers and internet connections, th ...

Preventing Vue.js SPA from accessing cached version when JWT expires: the solution

I'm encountering an issue with my Vue.js / Express application that I can't seem to resolve. Here's how the process unfolds: An unauthenticated user logs into the app and is presented with the login page. Once successfully authenticated, t ...

Solution for Organizing Tables

I've sourced data from various JSON API links and displayed it in a table. Currently, my code looks like this: <script src="js/1.js"></script> <script src="js/2.js"></script> Above this code is the table structure with <t ...

navigating through an array with a loop (for)

I created a basic phone book program where users can input a name and it will search an array of objects for the corresponding phone number. If the name is found, it will display the name and phone number. However, I am facing an issue where even though ...