Click here to Skip to main content
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 21:23pm
Edited 2-Aug-11 21: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 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 3 Aug 2011
Copyright © CodeProject, 1999-2014
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