Search code examples
asp.netanchorlinkbuttonhidden-field

Whats the difference if I set hidden field value through html anchor OR asp.net linkbutton?


The problem is, I have a set of links onclick of those links I am setting the linkId into a Hidden field. First my link were asp:linkbutton ans onClientClick I was setting the hiddenfield value.that time I was able to get the hidden field value from code behind but when I changed my links to HTML anchor and onClick I set the hidden field value , I am not getting hidden field with blank. when I debug JavaScript it is perfectly setting the hidden field value but why I am not getting it in code behind---my code-

<a href="./ContentPage.aspx"  data-flexmenu='flexmenu1' onclick="javascript:setPageLinkId(1);">

<script type="text/javascript">
    function setPageLinkId(lnkPageId) {
        debugger;
        alert(lnkPageId);
        document.getElementById('<%=hdnSelectedLink.ClientID %>').value = lnkPageId.toString();            
    }      

</script>

//code behind- here I get blank hidden field
if (hdnSelectedLink.Value != null && hdnSelectedLink.Value != "")
        {               
            GetLinkPage(Convert.ToInt32(hdnSelectedLink.Value));

        }

Whats the problem ,Please suggest?


Solution

  • My theory is that the click on the anchor doesn't cause a postback to the page. Rather a HTTP GET Request to "ContentPage.aspx" is issued, meaning that any form values are not posted to the server.

    You need to use a control that causes a postback to the page...for example ASP:LinkButton as you had before.