locked
getting the ID of Href link in code behind RRS feed

  • Question

  • User351619809 posted

    In below code, I need to find out which <h ref link has been clicked and store the id of which ever link has been clicked in session variable in ASP.net code behind.

    <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert1.aspx" id="certB">
                        <div class="ss-action-internal">
                            <h1>certificates1</h1>
                            
                        </div>
                    </a>
                
                    <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert2.aspx" id="certX">
                        <div class="ss-action-internal">
                            <h1>certificates2</h1>
                        
                        </div>
                    </a>
                
                    <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert3.aspx" id="certJ">
                        <div class="ss-action-internal">
                            <h1>certificates3</h1>
                            
                        </div>
                    </a>
                
                    <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert4.aspx" id="certG">
                        <div class="ss-action-internal">
                            <h1>certificates4</h1>
                            </div>
                    </a>
    Friday, June 26, 2020 4:26 AM

Answers

  • User-939850651 posted

    Hi anjaliagarwal5,

    You can also use the onclick event in the tag to achieve it, because this event is executed before the page jumps, so I think this can meet your needs.

    As other members have said, you can use ajax to call methods to store ids, please refer to below example:

    Page code:
    
    <head runat="server">
        <link href="Content/bootstrap.min.css" rel="stylesheet" />
        <title></title>
        <script src="Scripts/jquery-3.4.1.js"></script>
        <script>
    $(document).ready(function () {
    $('a[href*="aspx"]').attr("onclick", "saveId(id)");
    }); function saveId(id) { $.ajax({ url: "ClickHref.aspx/saveId", type: "POST", data: "{'id':'" + id + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { if (data.d != null) { alert("save id to sesssion success,now id is -->" + data.d); } else { alert("save failed"); } } }); } </script> </head> <body> <form id="form1" runat="server"> <div> <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert1.aspx" id="certB"> <div class="ss-action-internal"> <h1>certificates1</h1> </div> </a> <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert2.aspx" id="certX"> <div class="ss-action-internal"> <h1>certificates2</h1> </div> </a> <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert3.aspx" id="certJ"> <div class="ss-action-internal"> <h1>certificates3</h1> </div> </a> <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert4.aspx" id="certG"> <div class="ss-action-internal"> <h1>certificates4</h1> </div> </a> </div> </form> </body>
    Code behind:
    
     protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [WebMethod]
            public static object saveId(String id) {
                HttpContext.Current.Session.Add("ClickedHref", id);
                return HttpContext.Current.Session["ClickedHref"];
            }

    Result:

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 26, 2020 7:00 AM

All replies

  • User-2054057000 posted

    When you click an anchor it will do the page postback therefore you have to remove the href link with href="#"

    Next you can do this by performing AJAX code to call C# function. In this C# function you can store the value in session variable.

    Friday, June 26, 2020 5:37 AM
  • User-939850651 posted

    Hi anjaliagarwal5,

    You can also use the onclick event in the tag to achieve it, because this event is executed before the page jumps, so I think this can meet your needs.

    As other members have said, you can use ajax to call methods to store ids, please refer to below example:

    Page code:
    
    <head runat="server">
        <link href="Content/bootstrap.min.css" rel="stylesheet" />
        <title></title>
        <script src="Scripts/jquery-3.4.1.js"></script>
        <script>
    $(document).ready(function () {
    $('a[href*="aspx"]').attr("onclick", "saveId(id)");
    }); function saveId(id) { $.ajax({ url: "ClickHref.aspx/saveId", type: "POST", data: "{'id':'" + id + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { if (data.d != null) { alert("save id to sesssion success,now id is -->" + data.d); } else { alert("save failed"); } } }); } </script> </head> <body> <form id="form1" runat="server"> <div> <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert1.aspx" id="certB"> <div class="ss-action-internal"> <h1>certificates1</h1> </div> </a> <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert2.aspx" id="certX"> <div class="ss-action-internal"> <h1>certificates2</h1> </div> </a> <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert3.aspx" id="certJ"> <div class="ss-action-internal"> <h1>certificates3</h1> </div> </a> <a class="ss-action ss-action-form ss-utility-box ss-action-page-copy" href="cert4.aspx" id="certG"> <div class="ss-action-internal"> <h1>certificates4</h1> </div> </a> </div> </form> </body>
    Code behind:
    
     protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [WebMethod]
            public static object saveId(String id) {
                HttpContext.Current.Session.Add("ClickedHref", id);
                return HttpContext.Current.Session["ClickedHref"];
            }

    Result:

    Hope this can help you.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 26, 2020 7:00 AM