banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

[Solved] Issue of LinkButton not working after disabling JS in [Asp.net]

The LinkButton in Asp.net is similar to a regular HyperLink but with the added functionality of the OnClick event. However, upon analyzing the generated code in Chrome or Firefox, it was discovered that the implementation of the OnClick event is done through JavaScript. Therefore, if JavaScript is disabled in the browser, the LinkButton will not work. Here is an example of the code generated for a simple LinkButton:

[LinkButton1](javascript:__doPostBack('ctl00$MainContent$LinkButton1',''))  

The href attribute in the above code indicates that the click event is implemented by posting back to the server. By analyzing the code above, it can be determined that only two control parameters need to be sent to the server to notify it to handle the click event, using the GET method. Therefore, the following code can be used:

Fake LinkButton1

After running the code, it was found that clicking on the code we created resulted in an error on the server. The reason is that client-side event validation is enabled on the server. To disable client-side event validation, add the following attribute code to the header section of the ASP.net aspx page:

EnableEventValidation="false"

This will disable client-side event validation and allow calling server events without using JavaScript.

Complete code for Default.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>

Real LinkButton1  
[Fake LinkButton1](?__EVENTTARGET=ctl00$MainContent$LinkButton1&__EVENTARGUMENT=)

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void LinkButton1_Click1(object sender, EventArgs e)
{
    // This is a test code
    int a = 1;
    int b = 1;
}

}

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.