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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With