Passing Eval from ASPX to Javascript function as Parameter

<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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

onclick='<%# "PopulateTicketDiv(" +Eval("SHOW_ID") + " );" %>'

Method 2

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

Method 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.

Method 4

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
    }

Method 5

Pls Check this code

onclick='<%#Eval(“DocumentPath”,”Chk(”{0}”)”) %>’

Method 6

Basically you need to escape the quote

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x