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 :(
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";
myButton.OnClientClick = "clientFunction()";
On client side:
function clientFunction() {
}
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");});
精彩评论