Execute server-side function using javascript onLoad event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marknut
    New Member
    • Apr 2010
    • 42

    Execute server-side function using javascript onLoad event

    I have nested dynamic gridviews that I want to add “delete” and “edit” functionality to. The problem is that I can't use the addhandler event due to the postbacks (as I understand it anyway).

    So my question is, how can I run a server-side function using the ImageButton_Del ete.Attributes. Add("onClick", "...") functionality?

    I assume it's a __doPostBack function but I'm not sure.

    **The title is inaccurate, I wrote "onLoad" instead of "onClick"**
    Last edited by Marknut; May 9 '11, 06:16 PM. Reason: Wrong Title
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    #2
    Do these two things:
    1. Assign commandname properties to youe buttons "edit" and "delete" respectively.
    2. Call your databind event.

    You'll find that you can now utilize the rowedititing and rowdeleting events now.

    Comment

    • Marknut
      New Member
      • Apr 2010
      • 42

      #3
      Thanks for the feedback. I just tried it, and it didn't work for me. That may set everything up correctly and try to run the code, but I guess there's another problem with the nested gridviews. When I try to run server-side code that's initiated by a nested gridview, the postback causes the gridview to vanish (because it’s dynamic and not being rebuilt) and it still doesn't run the code that’s in the RowEditing or RowDeleting events.

      For anybody that’s stuck on a similar problem, I do have a work-around until I can figure out how to fix it. I created a hidden textbox and a hidden button. I put the server-side code that I want to run in the hidden_button_c lick event and I use the hidden_textbox to store values from the gridview for editing and deleting purposes. On the RowDatabound event of the nested gridview, I added javascript to the "onClick" event of the delete_button and the edit_button that will fill the hidden_textbox and "click" the hidden_button to run the events. The key is to finish the "onCick" events with "return false;" so the gridview is not initiating the postback.

      Here’s the code:

      Code:
      Dim ImageButton_Edit As ImageButton = CType(e.Row.Cells(2).Controls(0), ImageButton)
      
      Dim ImageButton_Delete As ImageButton = CType(e.Row.Cells(3).Controls(0), ImageButton)
      
      ImageButton_Edit.Attributes.Add("onClick", "document.getElementById('" & TextBox_SC_Edit_Delete.ClientID & "').value = " & e.Row.Cells(4).Text.ToString() & "; document.getElementById('" & Button_SC_Edit.ClientID & "').click(); return false;")
      
      ImageButton_Delete.Attributes.Add("onClick", "if (confirm('Are you sure you want to delete this Scorecard?')==false) return false; document.getElementById('" & TextBox_SC_Edit_Delete.ClientID & "').value = " & e.Row.Cells(4).Text.ToString() & "; document.getElementById('" & Button_SC_Delete.ClientID & "').click(); return false;")
      
      Private Sub Button_SC_Edit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_SC_Edit.Click
      Response.Redirect("SCWizard.aspx?Edit_SC_Key=" & TextBox_SC_Edit_Delete.Text)
      End Sub
      
      Private Sub Button_SC_Delete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_SC_Delete.Click
      Response.Redirect("Default.aspx?Delete_SC_Key=" & TextBox_SC_Edit_Delete.Text)
      End Sub

      Comment

      Working...