Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to Cancel a DataView's ButtonField PostBack?

How to Cancel a DataView's ButtonField PostBack?

Scheduled Pinned Locked Moved ASP.NET
javascripthtmltutorialquestionlearning
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dr_X
    wrote on last edited by
    #1

    I have tried combinations of the following to cancel the postback of a ButtonField. I am obtaining the data to view via Ajax and do not want the postback. Any ideas? Code Behind in the dgData.RowDataBound event:

    If e.Row.RowType = DataControlRowType.DataRow Then
    e.Row.Attributes.Add("onclick", "javascript:return viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
    End If

    Or without the "return"

    e.Row.Attributes.Add("onclick", "javascript:viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")

    HTML:

    <asp:ButtonField Visible="true" ImageUrl="../images/ViewSearch.gif" ButtonType="Image" Text="View Record" >
    <ItemStyle HorizontalAlign="Center" Width="20px" />
    </asp:ButtonField>

    JavaScript

    function viewRow(id, row) {
    try {
    if (!(previousRow==null)) previousRow.backcolor= row.backcolor; //reset the previous row's backcolor
    previousRow = row;
    row.style.background="lightblue";
    document.getElementById("<%=tblEdit.ClientID %>").style.display="none"; //Hide and display HTML
    document.getElementById("<%=tblView.ClientID %>").style.display="inline"; //tables from the client
    requestSimpleService = CourseList.GetCourse(id, populateCourse, onError); //Ajax call
    return true; //Tried false as well
    } catch (e) { alert(e.message);
    }
    }

    Note I am using the "<%=tblView.ClientID %>" due to using masterpages. Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

    M N 2 Replies Last reply
    0
    • D Dr_X

      I have tried combinations of the following to cancel the postback of a ButtonField. I am obtaining the data to view via Ajax and do not want the postback. Any ideas? Code Behind in the dgData.RowDataBound event:

      If e.Row.RowType = DataControlRowType.DataRow Then
      e.Row.Attributes.Add("onclick", "javascript:return viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
      End If

      Or without the "return"

      e.Row.Attributes.Add("onclick", "javascript:viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")

      HTML:

      <asp:ButtonField Visible="true" ImageUrl="../images/ViewSearch.gif" ButtonType="Image" Text="View Record" >
      <ItemStyle HorizontalAlign="Center" Width="20px" />
      </asp:ButtonField>

      JavaScript

      function viewRow(id, row) {
      try {
      if (!(previousRow==null)) previousRow.backcolor= row.backcolor; //reset the previous row's backcolor
      previousRow = row;
      row.style.background="lightblue";
      document.getElementById("<%=tblEdit.ClientID %>").style.display="none"; //Hide and display HTML
      document.getElementById("<%=tblView.ClientID %>").style.display="inline"; //tables from the client
      requestSimpleService = CourseList.GetCourse(id, populateCourse, onError); //Ajax call
      return true; //Tried false as well
      } catch (e) { alert(e.message);
      }
      }

      Note I am using the "<%=tblView.ClientID %>" due to using masterpages. Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi Michael, You need to add the onlick attribute to the ImageButton (the ButtonField with the ButtonType="Image") instead of the the row (tr) element. So there are two ways here to cancel the postback of the ButtonField: + Get reference to the ImageButton which is automatically added to the cell:

      if (e.Row.RowType == DataControlRowType.DataRow)
      {
      ImageButton btn = e.Row.Cells[cellIndex].Controls[0] as ImageButton;
      if(btn != null)
      btn.On_Client_Click = "clientsidefunction(); return false;";
      }

      + Use the TemplateField with the ImageButton in the ItemTemplate instead of the ButtonField:

      asp:TemplateField
      <ItemTemplate>
      <asp:ImageButton On_Client_Click="clientsidefunction(); return false;" ... />
      </ItemTemplate>
      </asp:TemplateField>

      1 Reply Last reply
      0
      • D Dr_X

        I have tried combinations of the following to cancel the postback of a ButtonField. I am obtaining the data to view via Ajax and do not want the postback. Any ideas? Code Behind in the dgData.RowDataBound event:

        If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Attributes.Add("onclick", "javascript:return viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
        End If

        Or without the "return"

        e.Row.Attributes.Add("onclick", "javascript:viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")

        HTML:

        <asp:ButtonField Visible="true" ImageUrl="../images/ViewSearch.gif" ButtonType="Image" Text="View Record" >
        <ItemStyle HorizontalAlign="Center" Width="20px" />
        </asp:ButtonField>

        JavaScript

        function viewRow(id, row) {
        try {
        if (!(previousRow==null)) previousRow.backcolor= row.backcolor; //reset the previous row's backcolor
        previousRow = row;
        row.style.background="lightblue";
        document.getElementById("<%=tblEdit.ClientID %>").style.display="none"; //Hide and display HTML
        document.getElementById("<%=tblView.ClientID %>").style.display="inline"; //tables from the client
        requestSimpleService = CourseList.GetCourse(id, populateCourse, onError); //Ajax call
        return true; //Tried false as well
        } catch (e) { alert(e.message);
        }
        }

        Note I am using the "<%=tblView.ClientID %>" due to using masterpages. Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)

        N Offline
        N Offline
        NeverHeardOfMe
        wrote on last edited by
        #3

        Like this: (replace the X in cells(X) with the correct column number for you case) Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim c As Button = e.Item.Cells(X).Controls(0) c.Attributes.Add("onclick", "return confirm('Do you really want to click this?')") End If End Sub cheers P

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups