3

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

NB: The line shown below is actually all on one line, but is wrapped here for clarity:

<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 better ways of doing this? If so, what are they?

1
  • Let me know if the answer I gave helped you. Commented Jul 29, 2009 at 18:13

4 Answers 4

1

OnClick is a property on the control which expects a reference to an event handler method. Putting JavaScript in the OnClick property will not work.

If you want to execute arbitrary JavaScript when the button is clicked, use OnClientClick. I presume you want to pass the evals into 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 are constructing one long string:

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

Which is executed in JS when the LinkButton is clicked.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response. The compiler now complains that the server tag is not well formatted.
@Dhruv OK, this method of constructing arguments is a bit error-prone since there are so many places for syntax typos. I've redone it and verified it works.
@Rex Thanks. This is what I get from the page source. Do you still think we can work around this? <a onclick="ShowEventDetails(&quot;<%# Eval(&quot;EventID&quot;).ToString() %>&quot;, &quot;<%# Eval(&quot;Title&quot;).ToString() %>&quot;, and so on.
0

The best way to do this is to set the OnClientClick property in the code-behind, like so:

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

Once you do, use String.Format to put your items into 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() );

Comments

0

Another way to pass multiple parameter from aspx client, sample code 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);"%> '

Comments

0

Eval is Evil try this Life Saving Code

onClick=='<%# string.Format("javascript:ShowEventDetails({0},{1},{2});return false;", Eval("Title"), Eval("Location"), Eval("StartTime")) %>'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.