Click here to Skip to main content
15,899,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi how to add onclick function to the dynamic button..

i wrote like this
for loop 
{
  ImageButton btn = new ImageButton();
                    btn.ID = "btn" + i;
                    btn.ImageUrl = "images/edit.jpg";
                    //btn.OnClientClick += new EventHandler(get);

                    btn.Attributes.Add("onclick", "get()");
}

C#
public void get(object sender, EventArgs e)
      {
          this.mopopupeditrule.Show();
      }


in this onclick function not works..how can i give...if i click button one modalpopup want to show..any idea?
Posted
Updated 2-Aug-11 20:36pm
v2

You can not call a server side function from the onclick event. You can call only the Javascript function.
 
Share this answer
 
Comments
suryaswathi 3-Aug-11 2:40am    
not able to add onclick to btn ah?how can i call javascript function
Error: "btn.OnClientClick += new EventHandler(get);"

Try: "btn.OnClick += new EventHandler(get);"

OnClientClick is for Client Side events(Javascript).

If you need client fire client side event, Use "btn.Attributes.Add("onclick","JsFunction()");
 
Share this answer
 
v2
Comments
suryaswathi 3-Aug-11 2:47am    
"btn.OnClick += new EventHandler(get);"

if i wantt to do like this.
btn does not having onclick property

insted i tried like
btn.Attributes.Add("onclick","get()");

but eventhough if i click the function does not show popup


public void get(object sender, EventArgs e)
{
this.mopopupeditrule.Show();
}
this is my get function
if you wants to add a server side function then
btn.OnClick += new EventHandler(btn_Click);

here Attaching a function using delegate.

If you wants to add a Javascript function then try
C#
btn.Attributes.Add("onclick", "javascript function name");
 
Share this answer
 
Comments
thatraja 3-Aug-11 3:09am    
Clear one, 5!
nit_singh 3-Aug-11 3:18am    
Thanks
The way you have added attribute is correct. But you are expecting it to be a sever side method, which is not the case. The attribute 'onclick' has been added at the client side; which means get() should be a javascript method.

It would look something like this:

JavaScript
<script>
function get()
{
    //show modal dialog here...
}
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900