20
<a id="lblShowTimings"
     runat="server"
     title='<%# Eval("SHOW_Name") %>'
     onclick='PopulateTicketDiv(<%#Eval("SHOW_ID") %>)'>  <-- this is the problem
  %#Eval("SHOW_Time") %>
</a>

Can Eval be passed as an argument to a javascript function? If so whats the syntax?

6 Answers 6

44

Yes. What you want to do is this, though:

onclick='<%# "PopulateTicketDiv(" +Eval("SHOW_ID") + " );" %>'
Sign up to request clarification or add additional context in comments.

Comments

22

The above solution creates problem when you want to pass the string as parameter, you can use following syntax to get through:

OnClientClick='<%# String.Format("javascript:return displayDeleteWarning(\"{0}\")", Eval("ItemName").ToString()) %>' 

Above line should work irrespective of parameter data type

2 Comments

OnClientClick='<%# String.Format("javascript:return displayDeleteWarning(\"{0}\")", Eval("ItemName").ToString()) %>' of Rohan's post is helpful in a situation where you want to pass 'Eval' value to javascript function as parameter. You can also pass multilple 'Eval' values as parameter.
Yes the accepted solution make an error in client-side.
3

Try

<script type="javascript">
     //Pollute the global namespace
     var ticketDivID = <%= SHOW_ID %>
</script>

<a id="lblShowTimings" runat="server" title='<%# Eval("SHOW_Name") %>' onclick='PopulateTicketDiv(ticketDivID)'> <%#Eval("SHOW_Time") %></a>

On a side note because you've got runat="server" you can set the onclick from the backend in OnRowDataBound if this is in a grid/repeater or on page_load if not.

Comments

2

You can use this syntax within a gridview, repeater or..etc.

<asp:ImageButton  
 ID="Imagebutton1" runat="server"
 ImageUrl="../../common/images/pencil.gif"                
 OnClientClick='<%# String.Format("EditBankAccount(\"{0}\");", Eval("BankAccountID")) %>'
 OnClick="ImgBankAccountsDGEdit_Click"/>

Your JavaScript function would be:

 function EditBankAccount(bankaccountid) {
       // Your code goes here
       // return true OR false based on your requirement
    }

Comments

1

Pls Check this code

onclick='<%#Eval("DocumentPath","Chk(\"{0}\")") %>'

Comments

0

Basically you need to escape the quote

<asp:CheckBox onclick='<%# "ToggleByPassValidationRules(" + "\"" + Eval("Name") + "\"" + ");" %>' ID="chkIsRuleActive" runat="server" Enabled="false" />

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.