简体   繁体   English

将jQuery函数添加到动态生成的控件(ASP.NET)

[英]Add a jQuery Function to a dynamically generated Control (ASP.NET)

I create a Control (Dynamically): Example: 我创建一个控件(动态地):示例:

Button myButton = new Button();
myButton.Id = "button1";

.. and so far.. ..到目前为止。

So, but now, how do I add this Button a jquery Function? 因此,但是现在,如何为这个按钮添加一个jQuery函数?

Something like this didn't work for me: 这样的事情对我不起作用:

$("#button1").live('click', function () {
alert("hi iam getting dynamic added button");});

What can I do? 我能做什么? Hope u guys can help me :( 希望你们能帮助我:(

myButton.OnClientClick = "clientFunction()";

On client side: 在客户端:

function clientFunction() {
}

you could create a generic js function that takes the Id of the control which has been clicked 您可以创建一个通用的js函数,该函数采用已单击的控件的ID

function ShowMessage(){
   alert("hi iam getting dynamic added button");
}

then on the server side 然后在服务器端

myButton.OnClientClick = "ShowMessage()";

if you don't want raise a post back: 如果您不想发回帖子:

myButton.OnClientClick = "ShowMessage();return false";

I believe when the page is rendered to the client, button1 will be changed to a rendered ID. 我相信当页面呈现给客户端时, button1将更改为呈现的ID。 I think it would be better adding a class to the button, and assigning jQuery events to that: 我认为最好在按钮上添加一个类,并为此分配jQuery事件:

C#: C#:

Button myButton = new Button();
myButton.Id = "button1";
myButton.CssClass = myButton.Id;

Javascript: Javascript:

$(".button1").live('click', function () {
alert("hi iam getting dynamic added button");});

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

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