locked
Cancel Update Gridview RRS feed

  • Question

  • User1028962535 posted

    Hello  I trying to prevent my gridview from updating, when a textbox outside of the gridview is empty but it's not working. See code below, any ideas what I am doing wrong?

    Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    Select Case e.CommandName

    Case "Edit"

    Case "Cancel"

    Case "Update"
    If TextBox1.Text = "" Then
    CustomersGridView.UpdateRow(CustomersGridView.EditIndex, False)
    Response.Write("<script type=""text/javascript"">alert(""Empty textbox cannot save"");</script")
    Else
    Response.Write("<script type=""text/javascript"">alert(""Data saved ."");</script")
    CustomersGridView.UpdateRow(CustomersGridView.EditIndex, True)
    End If

    End Select

    thanks

    Dan

    Friday, November 3, 2017 11:21 AM

Answers

  • User1028962535 posted

    Thanks for the reply, removed the logic but it still updates, tried placing the code here instead and its seems to work now

    Protected Sub CustomersGridView_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)

    If TextBox1.Text = "" Then

    e.Cancel = True
    Response.Write("<script type=""text/javascript"">alert(""Empty textbox cannot save"");</script")
    Else
    e.Cancel = False
    Response.Write("<script type=""text/javascript"">alert(""Data saved ."");</script")

    End If

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 3, 2017 12:26 PM

All replies

  • User475983607 posted

    The code explicitly updates the record then alerts the user.  Remove the update logic.

    Case "Update"
    	If TextBox1.Text = "" Then
    		Response.Write("<script type=""text/javascript"">alert(""Empty textbox cannot save"");</script")
    	Else
    		Response.Write("<script type=""text/javascript"">alert(""Data saved ."");</script")
    		CustomersGridView.UpdateRow(CustomersGridView.EditIndex, True)
    	End If

    Keep in mind the code shown above runs on the server first, then the client alert fires.

    Friday, November 3, 2017 11:52 AM
  • User1028962535 posted

    Thanks for the reply, removed the logic but it still updates, tried placing the code here instead and its seems to work now

    Protected Sub CustomersGridView_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)

    If TextBox1.Text = "" Then

    e.Cancel = True
    Response.Write("<script type=""text/javascript"">alert(""Empty textbox cannot save"");</script")
    Else
    e.Cancel = False
    Response.Write("<script type=""text/javascript"">alert(""Data saved ."");</script")

    End If

    End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 3, 2017 12:26 PM