Solved

OnClientClick not getting fired

Posted on 2006-11-06
6
954 Views
Last Modified: 2012-06-21
Hello Experts,

I have this button and a dropdownbox:

<asp:DropDownList ID="ddlLookupType" AutoPostBack=true CausesValidation=false OnSelectedIndexChanged="showPickList" runat=server>
                <asp:ListItem>Contacts</asp:ListItem>
                <asp:ListItem>Organization</asp:ListItem>
            </asp:DropDownList>

<asp:Button id="btnSearch" runat="server" Text="Search" />
and on load of the page I am adding the OnClientClick attribute as shown below:

            if (ddlLookupType.SelectedValue == "Organization")
            {
                btnSearch.Attributes.Add("OnClientClick", "openOrganizationPopup(); return false;");

            }
            else
            {
                btnSearch.Attributes.Add("OnClientClick", "openContactPopup(); return false;");
            }
But it is not getting fired. If I specify in the html code instead of code behind it works fine. Whats the mistake I am doing.

Please help

Thanks
0
Comment
Question by:rowmark
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 17886557
What is it you are trying to fire?
Isn't onClientClick for confirmation messages and the like returning some info from the server?
If you want to carry our a client side function why not just put onClick='openContactPopup()' onto your <asp:button tag?
0
 

Author Comment

by:rowmark
ID: 17886569
I want to call diff javascript method based on the value from the dropdown thats why I cant specify in the <asp:button tag itself.

 if (ddlLookupType.SelectedValue == "Organization")
            {
                btnSearch.Attributes.Add("OnClientClick", "openOrganizationPopup(); return false;");

            }
            else
            {
                btnSearch.Attributes.Add("OnClientClick", "openContactPopup(); return false;");
            }
0
 
LVL 29

Expert Comment

by:QPR
ID: 17886592
openPopup(ddlLookupType.SelectedValue);

and then handle it in the JS?
is that possible? off the top of my head - haven't got time to test atm
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:rowmark
ID: 17886703
no this doesnt work.
0
 
LVL 10

Accepted Solution

by:
jnhorst earned 250 total points
ID: 17886787
"OnClientClick" is not a valid HTML attribute for the <input> element.  It is, however, a property of the Button class in ASP.NET 2.0.  If you are working in 2.0, instead of using Attributes.Add(), just set the OnClientClick property:

if (ddlLookupType.SelectedValue == "Organization")
{
     btnSearch.OnClientClick = "openOrganizationPopup(); return false;";
}
else
{
     btnSearch.OnClientClick = "openContactPopup(); return false;";
}

This would effectively be the same as doing this:

btnSearch.Attributes.Add("onclick", "openOrganizationPopup(); return false;");

which is what you have to do if you working with ASP.NET 1.1

John

0
 

Author Comment

by:rowmark
ID: 17886809
Thanks John it worked.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question