Solved

How to pass javascript parameter to C# fucntion parameter

Posted on 2010-09-09
8
1,003 Views
Last Modified: 2012-05-10
Hii Dear
I have problem to call c# function from javascript with parameter which is pass in javascript function.

Its work fine if there is no parameter. But I want to pass javascript parameter value to C# function.

I have try following way.

<script type="text/javascript">    
function EventTrack(pIntEventID,pIntCategoryID,pLngMemberID,pStrAttributeA,pStrAttributeB,pStrAttributeC,pStrAttributeD,pStrAttributeE)
        {  
          var  lIntEventID=pIntEventID;            
          var  lIntCategoryID = pIntCategoryID;
           var lLngMemberID = pLngMemberID;
           var lStrAttributeA = pStrAttributeA;
           var lStrAttributeB = pStrAttributeB;
           var lStrAttributeC = pStrAttributeC;
           var lStrAttributeD = pStrAttributeD;
           var lStrAttributeE = pStrAttributeE;            
            //This is the function in C# page code
            <% trackEvent(%> lIntEventID <%=,%> lIntCategoryID <%=,%> lLngMemberID <%=,%> lStrAttributeA <%=,%> lStrAttributeB <%=,%> lStrAttributeC <%=,%> lStrAttributeD <%=,%> lStrAttributeE <%=);%>
           
            return true;
        }
    </script>

Thanks in advance
0
Comment
Question by:avacaralife
  • 4
  • 2
8 Comments
 
LVL 30

Expert Comment

by:MlandaT
ID: 33636235
I am doubtul about your approach here. You need ot realise that your Javascript code executes at the client side, and the c# code on the server side. You cannot pass 'parameters' in the manner you are attempting.

You need to perhaps create a number of textboxes or hidden input controls on the form and set the values of those textboxes, then initiate a postback which causes the necessary processin to be done on the server side.
0
 
LVL 2

Expert Comment

by:SandyAgo
ID: 33636276
Hi avacaralife,

Have you seen anything about PageMethods before? I was looking to call C# functions using javascript not too long ago and this is the best solution I could find.

Hava a look at these for some examples and info:
http://aspalliance.com/1922_PageMethods_In_ASPNET_AJAX.2
http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
0
 

Author Comment

by:avacaralife
ID: 33636283
Thanks MlandaT,
Ok I will try the way suggest.
But if possible then I need to pass from java script.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Accepted Solution

by:
MlandaT earned 500 total points
ID: 33636447
In aspx page you define a hidden input control (you would have one for each of the values you want to pass back):

<input type="hidden" name="htxtEventID" id="htxtEventID" value="" runat=server>
<!--We will use the txtFlag textbox to determine when we should execute the C# function-->
<asp:Textbox runat="server" ID="txtFlag" Width="0px" Text=""></asp:Textbox>

In the javascript you set the value of the input control and initiate a postback:

function EventTrack(pIntEventID,pIntCategoryID,pLngMemberID,pStrAttributeA,pStrAttributeB,pStrAttributeC,pStrAttributeD,pStrAttributeE)
        {  
                var  lIntEventID=pIntEventID;            
                //the rest of your variable declarations here

                document.getElementById("htxtEventID").value = lIntEventID;
                //set the values for the corresponding hidden input for each of your variables here

//by setting a value in the textbox txtFlag, we cause a TextChanged event to fire on the server. In this event we will call our c# function
                document.getElementById("<%#txtFlag.ClientID%>").value = "CallFunction";
                document.forms[0].submit();
        }

And in the txtFlag_TextChanged event handler in the .aspx.cs file you can check:

protected void txtFlag_TextChanged(object sender, EventArgs e)
{
      If (txtFlag.Text == "CallFunction") {

            //here call your C# function, taking values from the javascript... expand for all the variables you need.
            myCFunction(htxtEventID.Text);

      };
}


0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33636471
SandyAgo's approach is also interesting.. I actually wasnt aware you could do that myself.
0
 

Author Comment

by:avacaralife
ID: 33636490
Thanks MlandaT,

Ok I will try this and reply you.
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33896067
Solutions were provided but no feedback was eventually given as to whether they helped find a working solution
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now