Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling two functions on asp.net button onclick

Tags:

asp.net

I want to call two functions on button click I tried like this

 <asp:Button ID="Button2" runat="server" Font-Bold="False" onclick="tableShow();Unnamed1_Click" Text="Search" 
         Width="63px">
like image 754
Elvin Avatar asked Aug 15 '12 04:08

Elvin


People also ask

How do you call 2 JavaScript functions onclick of a button in asp net?

Another option is to use the onclientclick event which allows you to run a JS function w/o a server interaction. In the first button (adds a new row to the table), I have a server side onclick="btnAdd_Click" as well as a client side OnClientClick="javascript:ResetSaveFlag()" .

Can we call 2 function onclick?

Greetings! Yes, you can call two JS Function on one onClick.

How do I call multiple functions on a button click?

Given multiple functions, the task is to call them by just one onclick event using JavaScript. Here are few methods discussed. Either we can call them by mentioning their names with element where onclick event occurs or first call a single function and all the other functions are called inside that function.

Can 1 Button have 2 onclick events?

So the answer is - yes you can :) However, I'd recommend to use unobtrusive JavaScript.. mixing js with HTML is just nasty.


Video Answer


2 Answers

you can use the method inside method to do this. First do this

<asp:Button ID="Button2" runat="server" Font-Bold="False" onclick="Unnamed1_Click" Text="Search" 
         Width="63px">

then in code behind

protected void Unnamed1_Click(object sender, EventArgs e)
{
    //call another function here
}
like image 24
عثمان غني Avatar answered Oct 19 '22 22:10

عثمان غني


OnClick is a server side event. Hence you can assign one method and from that method make a call to other method as below.

In asp markup

<asp:Button ID="Button2" runat="server" Font-Bold="False" onclick="Unnamed1_Click" 
 Text="Search" Width="63px">

In code behind

protected void Unnamed1_Click(object sender, EventArgs e)
{
    this.tableShow();
    //Do your actual code here.
}

UPDATE

If tableShow is a javascript method then you can use the below markup

<asp:Button ID="Button2" runat="server" Font-Bold="False" 
 OnClientClick="tableShow();" onclick="Unnamed1_Click" Text="Search"
 Width="63px">
like image 65
Ramesh Avatar answered Oct 20 '22 00:10

Ramesh