I am using a formview. I do some validation before the update button click and trying to add a "onClientClick" to a update button from codebehind. It does not look like "onClientClick" can be added from codebehind. Is it possible to add "onClientClick" to
a Image button from code behind. Thanks. Please help.
I am sorry for the late reply. I tried adding "onClientClick" to a button from code behind. It did not work. I am not sure why. May be because I am using a Formview. If I use the onclientclick from the frontend like <asp:imagebutton onClientClick="return
savechanges();" > this works fine. But If I try from code behind it does not work for me. Anybody know why?
I tried adding "onClientClick" to a button from code behind. It did not work. I am not sure why. May be because I am using a Formview.
You should extract the control first before adding an attribute to a control that resides within a DataBound control.. try this sample snippet below:
protected void FormView1_DataBound(object sender, EventArgs e)
{
//if (FormView1.CurrentMode == FormViewMode.Edit)
//if (FormView1.CurrentMode == FormViewMode.Insert)
if (FormView1.CurrentMode == FormViewMode.Edit)
{
ImageButton btn = (ImageButton)FormView1.Row.Cells[0].FindControl("ImageButton1"); //just change the index of cells to where the control is located
if (btn != null)
{
btn.Attributes.Add("onclick", "YourJavaScriptFunctionName()");
}
}
}
string javaScript =
"<script language='JavaScript'>";
// The following line can be replaced by any javascript commands.
javaScript += "window.open('myPage.aspx');";
MadhuThiru
Member
8 Points
31 Posts
add onClientClick from code behind to image button
Jan 15, 2009 05:17 PM|LINK
I am using a formview. I do some validation before the update button click and trying to add a "onClientClick" to a update button from codebehind. It does not look like "onClientClick" can be added from codebehind. Is it possible to add "onClientClick" to a Image button from code behind. Thanks. Please help.
Madhu
budugu
All-Star
41108 Points
6019 Posts
Re: add onClientClick from code behind to image button
Jan 15, 2009 05:35 PM|LINK
Like this..
ImageButton1.Attributes.Add("onClientClick", "YourJavaScriptFunction();");"Don't be afraid to be wrong; otherwise you'll never be right."
karan@dotnet
All-Star
26228 Points
4596 Posts
Re: add onClientClick from code behind to image button
Jan 15, 2009 05:36 PM|LINK
button1.Attributes.Add("OnClientClick","function()");
Karan
~ Blog ~
Remember To Mark The Post(s) That Helped You As The ANSWER
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: add onClientClick from code behind to image button
Jan 15, 2009 05:37 PM|LINK
Something like this?
protected void Page_Load(object sender, EventArgs e) { ImageButton1.Attributes.Add("onclick", "YourJavaScriptFunctionName"); }MessageBox Controls for WebForms | Blog | Twitter | Linkedin
MadhuThiru
Member
8 Points
31 Posts
Re: add onClientClick from code behind to image button
Jan 20, 2009 06:53 PM|LINK
I am sorry for the late reply. I tried adding "onClientClick" to a button from code behind. It did not work. I am not sure why. May be because I am using a Formview. If I use the onclientclick from the frontend like <asp:imagebutton onClientClick="return savechanges();" > this works fine. But If I try from code behind it does not work for me. Anybody know why?
Thanks for you all for the reply.
Madhu
izharulislam
Participant
1498 Points
296 Posts
Re: add onClientClick from code behind to image button
Jan 20, 2009 06:58 PM|LINK
Have you tried
MyButton1.Attributes.Add("onclick", "Myfunc()");
Izhar Ul Islam Khan
Microsoft Certified Technology Specialist
MadhuThiru
Member
8 Points
31 Posts
Re: add onClientClick from code behind to image button
Jan 20, 2009 07:06 PM|LINK
I tried that too. It did not work.
karan@dotnet
All-Star
26228 Points
4596 Posts
Re: add onClientClick from code behind to image button
Jan 20, 2009 07:19 PM|LINK
Button myButton = this.FormView1.FindControl("myButtonId"); myButton.Atttibutes.Add("onClientClick","myJavascriptFunction();");Karan
~ Blog ~
Remember To Mark The Post(s) That Helped You As The ANSWER
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: add onClientClick from code behind to image button
Jan 20, 2009 07:28 PM|LINK
You should extract the control first before adding an attribute to a control that resides within a DataBound control.. try this sample snippet below:
protected void FormView1_DataBound(object sender, EventArgs e) { //if (FormView1.CurrentMode == FormViewMode.Edit) //if (FormView1.CurrentMode == FormViewMode.Insert) if (FormView1.CurrentMode == FormViewMode.Edit) { ImageButton btn = (ImageButton)FormView1.Row.Cells[0].FindControl("ImageButton1"); //just change the index of cells to where the control is located if (btn != null) { btn.Attributes.Add("onclick", "YourJavaScriptFunctionName()"); } } }MessageBox Controls for WebForms | Blog | Twitter | Linkedin
JoeBlow9878
Member
353 Points
100 Posts
Re: add onClientClick from code behind to image button
Jan 20, 2009 08:23 PM|LINK
Button1.OnClientClick = myFunction();
protected string myFunction(){
string javaScript = "<script language='JavaScript'>"; // The following line can be replaced by any javascript commands. javaScript += "window.open('myPage.aspx');";javaScript +=
"</"; javaScript += "script>";return javaScript();}
Hope that helps!Joe