[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1080
  • Last Modified:

I am using ASP.Net button control , how can I associate a javaScript function beside a C# method in the OnClick event?

I am using ASP.Net button control , how can I associate a javaScript function beside a C# method in the OnClick event?
e.g.
<asp:Button ID="Button1" runat="server" Text="do"  OnClick="Button1_Click" />
How can I add javaScript function to the OnClick event?
0
mazin_kf
Asked:
mazin_kf
3 Solutions
 
Bob LearnedCommented:
button1.Attributes["onClick"] = "javascript:alert('Test');return true;";

Bob
0
 
DropZoneCommented:
There is no way to do this from the template file, as it will always give preference to the server-side event handler.  The solution is, like TheLearnedOne shows, to add the "onClick" attribute from our own code, prior to rendering the button.  This will inject the attribute into the HTML tag upon rendering it.

In fact, any control descending from WebControl will take any non-recognize attributed manually added to the Attributes collection and inject it verbatim into the HTML tag.

As an alternative, you can create a custom class descending from WebControl.Button and add a new property, say, "ClientOnClick".  Then you override the AddAttributesToRender() method and add the "onClick" attributes to the Attributes collection from there.  This way you could add the script directly from the page template like:

    <CustomCtrl:MyButton
        ID="Button1"
        runat="server"
        Text="do"
        OnClick="Button1_Click"
        ClientOnClick="javascript:alert('Test!');return true;"
    />

      -dZ.
0
 
unmeshdaveCommented:
Hi,
hope folllowing will help u.
<script type="text/javascript" >
    <!--
        function test()
        {
            alert("I am in Test");
        }
-->
</script>

Edit your Button control line as follows:
<asp:Button ID="Button1" runat="server" Text="do"  OnClick="Button1_Click"  OnClientClick="test()"/>

It will call both the function, test as well as Button1_Click(), if this is what u want.
If u want only client side function to call (javascript)
use your button control as follows:
<asp:Button ID="Button1" runat="server" Text="do"   OnClientClick="test()"/>

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DropZoneCommented:
Duh! I didn't think to ask if he was using ASP.NET 2.0, but that version does include an OnClientClick attribute already.  I use 1.1, so assumed he used the same.  Sorry.

    -dZ.
0
 
unmeshdaveCommented:
I guess answer is already given for this question. The author should revert back. so Ball is with Admin now.
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now