How to get parameter value after submit the form in asp.net c# using jQuery

Ashok Kumar 141 Reputation points
2023-03-02T04:18:42.8066667+00:00
I have created two aspx pages{page1.aspx and page2.aspx} with master page {main.master} and I redirected to page2 from page1 with `<a>` tag button click (in page1) my logic is executing fine but while I'm calling that parameter in page2.aspx using jQuery logic I'm getting null value or Error on console log.

Note :- I have created two aspx pages with master page because in my real time scenario I should work like that.

This is my entire logic

Master page:-


<%@ Master Language="C#" AutoEventWireup="true" CodeFile="main.master.cs" Inherits="main" %>
<!DOCTYPE html>
<html>
<head runat="server">

<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
    <link rel="Stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />

<script type="text/javascript" src="../customjs/custom.js"></script>


<asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>

<body>
    <form id="form1" runat="server">
        <div>

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>

        </div>
    </form>
</body>
</html>


And page1.aspx page :-



<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="page1.aspx.cs" Inherits="page1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<a class="redirectclick">RedirectToPage2</a>
</div>

 <script type="text/javascript">
$(document).ready(function () {
            //"use strict";
           FinalSB();
});
    </script>
</asp:Content>


And page2.aspx page :-



<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="page2.cs" Inherits="page2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script type="text/javascript">
        $(document).ready(function () {
            "use strict";
             FinalOC();
        });
</script>
</asp:Content>


jQuery logic :-


function FinalSB(){

$(".redirectclick").on("click", function (e) {
        e.preventDefault();
        //alert('click');
       redirectopage2();
});

}

function FinalOC(){
//alert('click');


    var p = document.querySelector('message').value;
    alert(p); //null value is coming or value is not defined error coming in console log

    //var searchParams = new URLSearchParams(window.location.search);
    //alert(searchParams);
    //var p = searchParams.get('message'); //null value is coming or value is not defined error coming in console log

}

function redirectopage2() {

    post_to_url('http://localhost:51609/page2.aspx',
    {
        message:"Calling"
        
    }, 'post','_blank');
}

//Form Submit
function post_to_url(path, params, method, targetOpt) {
    method = method || "post";
    targetOpt = targetOpt || "_blank";

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("target", targetOpt);
    form.setAttribute("action", path);

    for (var key in params) {
        if (params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("disabled", 'true');
            //hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);
            form.appendChild(hiddenField);
        }
    }

    document.body.appendChild(form);
    form.submit();

}



The entire logic is above and logic is working fine but the parameter value is not coming when I click on the `<a>` tag button please suggest me how to achieve this and where I did the mistake.

I have did this logic as per my experience and next I'm unable to moving to solve this issue.
Please suggest me how to solve this issue.

Sorry for my bad English.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,238 questions
{count} votes

3 answers

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,096 Reputation points Microsoft Vendor
    2023-03-03T10:26:02.0666667+00:00

    Hi @Ashok Kumar,

    According to your needs, I created simple examples to achieve such requirement. Here is the code sample:

    Page1:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <div>
            <a class="redirectclick" href="page2.aspx">RedirectToPage2</a>
        </div>
        <script type="text/javascript">
    
            $(document).ready(function () {
                //"use strict";
                FinalSB();
            });
        </script>
    </asp:Content>
    

    Page 2:

    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <asp:Label runat="server" ID="MessageLabel" hidden="hidden"></asp:Label>
    
        <script type="text/javascript">
            $(document).ready(function () {
                //"use strict";
                 FinalOC();
            });
            <%--function FinalOC() {
                var p = $('#<%= MessageLabel.ClientID %>').text();
                alert(p);
            }--%>
        </script>
    </asp:Content>
    

    Page2.aspx.cs

    public static string message { get; set; }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    if (Request.RequestType == "POST")
                    {
                        message = Request.Form["Message"];
                        MessageLabel.Text = message;
                    }
                }
            }
    
            [WebMethod]
            public static string getMessage() {
                return message;
            }
    

    custom.js

    function FinalOC() {
        $.ajax({
            url: "page2.aspx/getMessage",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            }
        });
    }
    
    function FinalSB() {
    
        $(".redirectclick").on("click", function (e) {
            e.preventDefault();
            //alert('click');
            redirectopage2();
        });
    
    }
    
    function redirectopage2() {
    
        post_to_url('page2.aspx',
            {
                message: "Calling"
    
            }, 'post', '_blank');
    
    }
    
    //Form Submit
    function post_to_url(path, params, method, targetOpt) {
        method = method || "post";
        targetOpt = targetOpt || "_blank";
    
        var form = document.createElement("form");
        form.setAttribute("method", method);
        form.setAttribute("target", targetOpt);
        form.setAttribute("action", path);
    
        for (var key in params) {
            if (params.hasOwnProperty(key)) {
                var input = document.createElement("input");
                input.setAttribute("name", key);
                input.setAttribute("value", params[key]);
                form.appendChild(input);
            }
        }
    
        document.body.appendChild(form);
        form.submit();
    }
    

    Result:

    result

    The form submission is on the server side, so you can't get the data from the server side from the browser DOM, in this case, you can get it through server controls (e.g. ASP:Label in prevoius example) or Ajax request. On the other hand, as Bruce mentioned, the submitted form does not contain the disaled element.

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Bruce (SqlWork.com) 54,626 Reputation points
    2023-03-03T16:06:21.83+00:00

    The browser does not post disabled fields. Not sure why you would disable a hidden field.

    0 comments No comments