简体   繁体   中英

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?

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

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. I think it would be better adding a class to the button, and assigning jQuery events to that:

C#:

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

Javascript:

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

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