Solved

OnClientClick not getting fired

Posted on 2006-11-06
6
948 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now