简体   繁体   中英

How to call html button onclick javascript function with aspx parameter?

I need to pass parameters from repeater to javascript function. But I can't do it like this:

     onclick="myFunction(<%# ((myObject)Container.DataItem).myValue) %>)" 

It says syntax error.

I need to call myFunction(myValue) javascript function. And the myValue have to come from

     <%# ((myObject)Container.DataItem).myValue) %>

What is the correct way to do this ?

如果您想将所有内容都写在标记中(而不是在rowdatabound或后面的代码中的某处),则可以使用以下语法:

onclick = '<%# "myFunction(" + Container.DataItemIndex + ")" %>'

What you need to do is add a couple of quotes:

onclick="myFunction("<%# ((myObject)Container.DataItem).myValue %>")"

This should work with what you are trying to achieve.

Try this if you pass string

onclick='<%# string.Format("myFunction(\"{0}\")",((myObject)Container.DataItem).myValue)%>'

otherwise use following...

onclick='<%# string.Format("myFunction({0})",((myObject)Container.DataItem).myValue)%>'

尝试从服务器端添加onclick,

Control.Attributes.Add("onclick",((myObject)Container.DataItem).myValue)

Use something like this.Modify this according to your requirement.

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
 {

var jsFunction = String.Format("Javascriptfunctionname('{0}','{1}');", parameter1, parameter2);
Gridcontrol.Attributes.Add("onclick", jsFunction);

}

Hope this will help you.

试试下面的代码。

onclick="javascript:myFunction("<%=((myObject)Container.DataItem).myValue) %>")";

您应该使用=而不是#

onclick = "myFunction(<%= ( (myObject)Container.DataItem).myValue ) %>)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM