Avatar of SmashAndGrab
SmashAndGrab
 asked on

ASP Button question

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?
ASP.NETJavaScript.NET ProgrammingC#

Avatar of undefined
Last Comment
Ramkisan Jagtap

8/22/2022 - Mon
Manoj Patil

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
ASKER CERTIFIED SOLUTION
Ramkisan Jagtap

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes