0

Consider the following codes:

In my .ascx

<a id="1" onclick="registerLastViewed(this.id);">Example</a>
<asp:HiddenField id="hdID" runat="server" />

<script type="text/javascript>
    function registerLastViewed(ID) {
        document.getElementById('<%= hdID.ClientID %>').value = ID;
        "<%= RegisterLastViewed() %>"; //dies here
    }
</script>

In my .ascx.cs

public void RegisterLastViewed() 
{
    //todo required logic        
}

Basically what I plan to do is: Clicking on <a> will call the Javascript function which will invoke my C# function from code behind.

But everytime I try to get into this page, it dies. Redirect loops and dies.

Is there a way to do this without using webmethod?

for reference, this runs fine:

<script type="text/javascript">
var usr = "<%=getUserId() %>";
</script>

public string getUserId() {

    string userId = "";
    if (Session[PropertiesKey.SessionKeys.UserObjLoginUser] != null)
    {
        UserMaster loginUser = (UserMaster)Session[PropertiesKey.SessionKeys.UserObjLoginUser];
        userId = loginUser.UserID;
    }
    return userId;
}
cweiske
  • 30,033
  • 14
  • 133
  • 194
NewbieCoder
  • 676
  • 1
  • 9
  • 32
  • 1
    The functions are both being invoked during page load. _"Is there a way to do this without using `webmethod`?"_ So you're aware of `webmethod` - why not use it? – James Thorpe Apr 13 '16 at 08:31
  • C# is processe don the server, Javascript runs on the client. They can't communicate the way you're expecting them to here – Liam Apr 13 '16 at 08:32
  • 3
    Possible duplicate of [How to call a C# function from javascript?](http://stackoverflow.com/questions/18441194/how-to-call-a-c-sharp-function-from-javascript) – Liam Apr 13 '16 at 08:33

0 Answers0