Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trigger codebehind from JavaScript 2

Status
Not open for further replies.

DaveC426913

Programmer
Joined
Jul 28, 2003
Messages
274
Location
CA
It's the basics that mess me up. I'm doing client-side field validation. Once done, I want to trigger the codebehind. How?

.aspx:
Code:
  <script>
  function validateInput(){ 
      if (blah blah isn't valid){
          alert("Not valid input!")
      }
      else{
          //good
          validateAgainstDB(); 
      }
  }
  </script>
.aspx.vb:
Code:
  public sub validateAgainstDB()
      ....
  End Sub
 
OK, I've gotten a little further, but I'n not sure if I'm on the right track.

.aspx
Code:
  function validateInput(){ 
      if (blah blah isn't valid){
          alert("Not valid input!")
          return false;
      }
      else{
          //good
          return true;
      }
  }

.aspx.vb
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Not Page.IsPostBack) Then
            btnRegister.Attributes.Add("onclick", "return validateInput();")
        End If
    End Sub

...

    Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click

So, now the btnRegister_Click is triggered, all I need to do is determine whether true or false was returned from validateInput() to decide whether or not to execute the code.

Again though, I'm not sure if the onclick tag on the Register button is the way to go.
 
>>So, now the btnRegister_Click is triggered, all I need to do is determine whether true or false was returned from validateInput() to decide whether or not to execute the code.


use hiddden fields, try this:
add this field to ur page:
<input type="hidden" name="hField1" value="">

ur JS:
function validateInput(){
if (blah blah isn't valid){
alert("Not valid input!")
}
else{
//good
document.forms[0].hField1.value="Yes"
return true;
}
}


in ur CB:
Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
if(request.forms("hField1")="Yes") then
EXECUTE CODE
end if
end sub

Known is handfull, Unknown is worldfull
 
Oh. Yeah. That'd work.

I guess I was looking for something more ... textbook.

Thanks.

Actually, I think it triggers the button click event before it runs through the JavaScript, so it won't work quite as listed. But it can be modified so that the hidden fields directly triggers the code.
 
This is what I use...it disables the button for only 1 click access then does my javascript checking and posts the form if all passed...

Code:
Dim sb As New System.Text.StringBuilder
sb.Append("this.disabled = true;")
sb.Append("if (checkdropdown(" & Me.dd.UniqueID & ",'Charge Type!')")
sb.Append("&& checkcurrency(" & Me.txtAmt.ClientID & ",'Enter valid amount!')")
sb.Append(" && checktext(" & Me.txtDescr.ClientID & ",'Enter a description!') ==true) {")
sb.Append(Me.Page.ClientScript.GetPostBackEventReference(Me.btnSave, ""))
sb.Append(";}else{ this.disabled = false; return false; }")

Me.btnSave.Attributes.Add("onclick", sb.ToString)

this line is what grabs the PostBack event of the button
Me.Page.ClientScript.GetPostBackEventReference(Me.btnSave, "")

"...your mom goes to college..."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top