Solved

ASP Button question

Posted on 2016-08-26
2
59 Views
Last Modified: 2016-08-31
Hi,


I have this button.

<asp:Button ID="btnUpdate" OnClientClick="return Validate(this, this.id);" class="btn btn-primary" runat="server" Text="Update" AutoPostBack="true" />

When the user clicks it - it runs a simple javascript.


function Validate(obj, objid) {

  // does some valiudation here then submits
        __doPostBack(objid, '');
    
}

Open in new window


My question is:    How do I get the button run my specific C# function after it has validated?
0
Comment
Question by:SmashAndGrab
2 Comments
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 41771489
If OnClickClient has a return value of true then the codebehind OnClick event will fire. If OnClickClient is returned false then it won't.
If OnClickClient doesn't return anything the codebehind event will fire regardless. So you want..

<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" />

<script language="javascript">
    function Validate() {
        return confirm('Save in db?');
    }
</script>

Open in new window


protected void Page_Load(object sender, EventArgs e)
{
   // set OnClientClick 
   btnConfirm.OnClientClick = "return Validate();";
}

protected void btnConfirm_Click(object sender, EventArgs e)
{
    //save something in database
}

Open in new window


Above code reference is from below link
http://stackoverflow.com/questions/28760687/invoke-onclientclick-of-button-in-code-behind
0
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 500 total points
ID: 41771494
<asp:Button ID="btnUpdate" OnClientClick="return Validate(this, this.id);" class="btn btn-primary" runat="server" Text="Update" onclick="btnUpdate_Click"  />

Open in new window


 
 protected void btnUpdate_Click(object sender, EventArgs e)
 {

        }

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

22 Experts available now in Live!

Get 1:1 Help Now