Click here to Skip to main content
13,195,379 members (61,442 online)
Rate this:
 
Please Sign up or sign in to vote.
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()");
}

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 2-Aug-11 20:23pm
Updated 2-Aug-11 20:36pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can not call a server side function from the onclick event. You can call only the Javascript function.
  Permalink  
Comments
suryaswathi 3-Aug-11 2:40am
   
not able to add onclick to btn ah?how can i call javascript function
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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()");
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
btn.Attributes.Add("onclick", "javascript function name");
  Permalink  
Comments
thatraja 3-Aug-11 3:09am
   
Clear one, 5!
nit_singh 3-Aug-11 3:18am
   
Thanks
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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:

<script>
function get()
{
    //show modal dialog here...
}
</script>
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.171019.1 | Last Updated 3 Aug 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100