简体   繁体   English

点击()带有PARAM的javascript事件?

[英]click() Event in javascript with a PARAM?

I'm using the event click() to call a method in Code Behind, like this: 我正在使用事件click()来调用Code Behind中的方法,如下所示:

HTML HTML

<asp:button bordercolor="White" id="btnAddGS" onclick="AddGSBandeira" runat="server">

JAVASCRIPT JAVASCRIPT

$("#ctl00_ContentPlaceHolder1_btnAddGS").click();

C# C#

public void AddGSBandeira(object sender, EventArgs e)
{

}

Its work normally, but I need to pass a param in the javascript call, like this: 它的工作正常,但我需要在javascript调用中传递一个参数,如下所示:

$("#ctl00_ContentPlaceHolder1_btnAddGS").click("param"); $( “#ctl00_ContentPlaceHolder1_btnAddGS”)点击( “参数”);

But I do not know how this works ...can anybody help? 但我不知道这是如何工作的......任何人都可以帮忙吗?

The best thing to do is create a hidden control and populate it's value with JavasScript on the click event. 最好的办法是创建一个隐藏控件,并使用JavasScript在click事件上填充它的值。 Your code behind will be able to access that value on your postback (AJAX or otherwise). 您的代码将能够在回发(AJAX或其他)上访问该值。

Markup 标记

<asp:HiddenField ID="myHiddenField" runat="server" />
<asp:button bordercolor="White" id="btnAddGS" 
            onclick="AddGSBandeira" 
            onclientclick="SetHiddenValue()" runat="server">

JavaScript JavaScript的

function SetHiddenValue()
{
    document.getElementById("<%=myHiddenField.ClientID%>").value = "[Your value here]";
}

C# C#

public void AddGSBandeira(object sender, EventArgs e){}
{
    var jsVal = myHiddenField.Value;
}

You can do this with trigger . 您可以使用trigger执行此操作。

http://api.jquery.com/trigger/ http://api.jquery.com/trigger/

$("#ctl00_ContentPlaceHolder1_btnAddGS").trigger("click",["param"]);

I believe the 2nd parameter to trigger should be an array of arguments to pass to the function. 我相信触发的第二个参数应该是一个传递给函数的参数数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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