Dhruv
Dhruv

Reputation:

Passing multiple Eval() to a JavaScript function from ASPX

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?

Upvotes: 3

Views: 11090

Answers (4)

zakk616
zakk616

Reputation: 1542

Eval is Evil try this Life Saving Code

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

Upvotes: 0

Bipinkrishna
Bipinkrishna

Reputation: 1

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);"%> '

Upvotes: 0

George Stocker
George Stocker

Reputation: 57872

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

Upvotes: 0

Rex M
Rex M

Reputation: 144112

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.

Upvotes: 1

Related Questions