views:

1160

answers:

2

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?

+2  A: 

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.

Rex M
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 M
@RexThanks.This is what I get from the page source. Do you still think we can work around this?<a onclick="ShowEventDetails("<%# Eval("EventID").ToString() %>", "<%# Eval("Title").ToString() %>", and so on.
A: 

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() );
George Stocker