Passing multiple Eval () functions in JavaScript from ASPX

I am trying to pass a few Eval() arguments to a JavaScript function from an .aspx file, but I keep getting compiler errors. I am new to JavaScript and have never used Eval() before. Where am I mistaken?

NB . The line shown below is actually on the same line, but for clarity it is wrapped:

 <asp:LinkButton runat="server" Text='<%#Eval("Title")%>' OnClick='javascript:ShowEventDetails' CommandArgument='<%# Eval("EventID").ToString() & Eval("Title").ToString() & Eval("Location").ToString() & Eval("StartTime").ToString() & Eval("Description").ToString() & Eval("Link").ToString() & Eval("ContactFirstName").ToString() & Eval("ContactLastName").ToString() & Eval("ContactEmail").ToString() & Eval("InsertionTime").ToString() & Eval("EventAdmin").ToString()%>); ' /> 

Are there any better ways to do this? If so, what are they?

+3
source share
3 answers

OnClick is a property of a control that expects a reference to an event handler method. Enabling JavaScript in the OnClick property will not work.

If you want to execute arbitrary JavaScript at the click of a button, use OnClientClick . I assume that you want to pass evals to ShowEventDetails as arguments (formatted for readability):

 <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("Title")%>' OnClientClick='ShowEventDetails("<%# Eval("EventID").ToString() %>", "<%# Eval("Title").ToString() %>", "<%# Eval("Location").ToString() %>", "<%# Eval("StartTime").ToString() %>", "<%# Eval("Description").ToString() %>", "<%# Eval("Link").ToString() %>", "<%# Eval("ContactFirstName").ToString() %>", "<%# Eval("ContactLastName").ToString() %>", "<%# Eval("ContactEmail").ToString() %>", "<%# Eval("InsertionTime").ToString() %>", "<%# Eval("EventAdmin").ToString() %>");' /> 

Essentially you create one long line:

 ShowEventDetails('123','Event Title','New York' ... etc ... 

Runs in JS when clicking LinkButton.

+1
source

The best way to do this is to set the OnClientClick property to the behind code, for example:

 LinkButton lb = new LinkButton(); //Instantiate the linkbutton wherever you need to 

Once you do, use String.Format to put your objects in the OnClientClick property.

 lb.OnClientClick = String.Format("javascript:ShowEventDetails ( '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}');", EventID.ToString(), Title.ToString(), Location.ToString(), StartTime.ToString(), Description.ToString(), Link.ToString(), ContactFirstName.ToString(), ContactLastName.ToString(), ContactEmail.ToString(), InsertionTime.ToString(), EventAdmin.ToString() ); 
0
source

Another way to pass multiple parameters from an aspx client, a code example is given below

Use two double quotes (+ "," "+ Eval (" xxx "). ToString () +" "") if you want to pass a string parameter

 OnClientClick='<%#"javascript:return OpenDetailsPage(" + Eval("xxx").ToString() + ",""" + Eval("xxx").ToString() + """," _ + Eval("xxx").ToString() + "," + Eval("xxx").ToString() + "," + Eval("xxx").ToString() + "," _ + Eval("xxx").ToString() + ",""" + Eval("xxx").ToString() + """,this);"%> ' 
0
source

All Articles