Click here to Skip to main content
11,721,671 members (81,513 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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
Edited 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 at 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 at 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 at 3-Aug-11 3:09am
   
Clear one, 5!
nit_singh at 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
0 Sergey Alexandrovich Kryukov 435
1 OriginalGriff 269
2 Mika Wendelius 170
3 cyycoish 70
4 ppolymorphe 66
0 Sergey Alexandrovich Kryukov 2,249
1 OriginalGriff 1,024
2 F-ES Sitecore 690
3 Maciej Los 619
4 Richard MacCutchan 580


Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 3 Aug 2011
Copyright © CodeProject, 1999-2015
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