Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

c# - gridview row delete with jquery dialog

Everything is working fine except that postback does not occur when I delete the row. I put a debugger break inside the OnRowDeleting and OnRowDataBound The only difference is that you don't have Ajax update panel and script Manger. Below is the image of the Gridview. Nothing happens When I click the ok button.

Below is my code on fresh page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="SidePanelGridViewTest.WebForm3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:IdDatabase %>" 
                    SelectCommand="SELECT TOP 5 ProductID, ProductName FROM [TableA]">
</asp:SqlDataSource>
            <asp:ScriptManager ID="scMan" runat="server"></asp:ScriptManager>
 <asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Always">
            <ContentTemplate>

<asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnRowDeleting="OnRowDeleting"  OnRowDataBound="OnRowDataBound" >
    <Columns>
        <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
            InsertVisible="False" ReadOnly="True"  />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName"
             />
         <asp:TemplateField ShowHeader="False" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="150px" ControlStyle-CssClass="ss-row" >
                        <ItemTemplate>
                              <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete1.png" ToolTip="Click To Delete" AlternateText="Click To delete" OnClientClick="myclick(this.id);return false;"/>
                              <asp:Button ID="MyDelete" runat="server" Text="del" CommandName="MyDelete" CommandArgument='<%# Eval("ProductID") %>' style="display:none"/>

                       </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>
 </ContentTemplate>
     </asp:UpdatePanel>
           
        </div>
        <div id ="mydialog" style="display:none" runat="server">
              <h2>Do you really want to delete?</h2>
        </div>
        <script>
    function myclick(mycontrolid) {
        var mydiv = $('#mydialog');

        mydiv.dialog({
            autoOpen: false, modal: true, title: 'Delete?', width: '25%',
            position: { my: 'top', at: 'top+150' },
            buttons: {
                'ok': function () {
                    vbtns = '#' + mycontrolid.replace('imgbtnDelete', 'MyDelete');
                    $(vbtns).click();
                },
                'cancel': function () {
                    mydiv.dialog('close');
                }
            }
        });
        mydiv.dialog('open');
    }

</script>



    </form>
</body>
</html>

below is the image of the gridview:

enter image description here

when I click on "Ok" button in the delete pop up. postback does not occur. I have a debugger break on both OnRowDeleting and OnRowDataBound. when I start running the code, debugger breaks on OnRowDataBound, but clicking on "OK' button on the pop up does not cause postback.

below is the code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SidePanelGridViewTest
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = Convert.ToInt32(e.RowIndex);


        }

        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            string x = "Test";

        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, I’m going to bite.

And the REASON is that I went through this, and the working examples are HORRID – BEYOND suffering!!

So, let’s work through this.

First up, unlike JavaScript (js) “alert” or “confirm”?

Most web stuff and including jQuery.UI does NOT halt, nor does it block code.

This means our “image” button “on click” can’t be used to return true or false, since our dialog does NOT wait. We thus have to get the results of the dialog AND THEN run that delete button code.

Given above?

Then most simple is placing a hidden delete button into that grid. We then have our jQuery.UI “click” that delete button for us.

jQuery.ui expects to “operate” on a jQuery selector. That means $(“please select this thing”)

Often that is some control or button, but for dialogs, most often that is a div you place on the page (and hide it --- FYI: note how we hid the jQuery.UI dialog "div" and ALSO how we hide the delete button with style="display:none").

So, let’s add our delete button – right below the image button (and by the way, you can use an asp.net button here – you’re not limited to an image button).

So, ONLY job of image button is to NOW call our jquery dialog – but it CAN NOT run our server side delete code.

So, let’s add that delete button.

We MOVE the delete stuff to this button FROM the image button.

So, we have this now:

<asp:TemplateField>
    <ItemTemplate>
        <asp:ImageButton ID="imgbtnDelete" runat="server"
           ImageUrl="~/Images/delete1.png" ToolTip="Click To Delete"
           AlternateText="Click To delete"
           OnClientClick="myclick(this.id);return false;"
        />

    <asp:Button ID="MyDelete" runat="server" Text="del" 
           CommandName="MyDelete"
           CommandArgument='<%# Eval("CartID") %>' 
           style="display:none"/>

  </ItemTemplate>
</asp:TemplateField>

So, we WILL RETURN false and NOT have that image button server side click run, or do a post-back.

So, change the image button (to call our simple js function.

NOTE HOW we moved the features and information to our hidden "my delete" button.

As FYI: Remember in a grid view, the commandName has SPECIAL meaning here. So, if we fill out both CommandName and CommandArgument, then the GridView “row command” event will fire!!!

And it looks like you doing that now anyway (using row command).

And here is the script + markup:

</asp:GridView>   ---- end of your gridview

<div id ="mydialog" style="display:none" runat="server">
     <h2>Do you really want to delete?</h2>
</div>

<script>
   function myclick(mycontrolid) {
      var mydiv = $('#mydialog');

      mydiv.dialog({
         autoOpen: false, modal: true, title: 'Delete?', width: '25%',
         position: { my: 'top', at: 'top+150' },
          buttons: {
             'ok': function () {
              vbtns = '#' + mycontrolid.replace('imgbtnDelete', 'MyDelete');
              $(vbtns).click();
              },
              'cancel': function () {
                        mydiv.dialog('close');
               }
          }
       });
       mydiv.dialog('open');
    }

</script>

And that is ALL you need here (remove that other "initialize" js junk you have).

The result should look like this:

enter image description here

So above is about the least amount of code for this to work. There are a dozen different ways to do this. But above is I think quite simple.

So, I have suggested the above since we in js do a “click()” of our delete button, and thus your existing code stub in the on-command row event can remain in place as you no doubt have it now. (So the suggested solution here takes into account your existing setup).

And for final good measure? our delete command will look like this:

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand

    If e.CommandName = "MyDelete" Then

        Using cmdSQL As New SqlCommand("DELETE FROM tblHotels where ID = " & e.CommandArgument,
                    New SqlConnection(My.Settings.Test3))
            cmdSQL.Connection.Open()
            cmdSQL.ExecuteNonQuery()
        End Using

        LoadGrid()    ' re-load the grid
    End If
End Sub

Note that you probably did call your code to load the grid up on first post (and NOT additonal post backs, but as above shows, WHEN you delete, you need to re-load the grid, since it does have a view state, and without a re-load of the grid view, then that deleted row WILL remain in view.

Edit: ----------------

It looks like the posted code DOES NOT include jQuery.UI.

You need BOTH jQuery, and ALSO need jQuery.UI. They are two separate libriies.

Also, jQuery.UI ALSO needs a style sheet, and it MUST be placed before the jQuery.UI referance.

The screen shots and working test code, starting at the "body" tag in the asp.net web form page looks like this:

<body>
<form id="form1" runat="server">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" SortExpression="HotelName" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="Button2" runat="server" Text="Button" 
                            OnClientClick="myclick(this.id);return false;"
                           />

                        <asp:Button ID="MyDelete" runat="server" Text="Button3" 
                            CommandName="MyDelete"
                            CommandArgument='<%# Eval("ID") %>' 
                            style="display:none"/>

                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

        <div id ="mydialog" style="display:none" runat="server">
            <h2>Do you really want to delete?</h2>
        </div>

    <script>

        function myclick(mycontrolid) {

            var mydiv = $('#mydialog');

            mydiv.dialog({
                autoOpen: false, modal: true, title: 'Delete?', width: '25%',
                position: { my: 'top', at: 'top+150' },
                buttons: {
                    'ok': function () {
                        vbtns = '#' + mycontrolid.replace('Button2', 'MyDelete');
                        $(vbtns).click();
                    },
                    'cancel': function () {
                        mydiv.dialog('close');
                    }
                }
            });
            mydiv.dialog('open');
        }

    </script>
</form>
</body>

Note I did place the jQuery and the jQuery.UI script references right inside of the form tag, but often users will place the references inside of the "head" tag.

but for ease of posting - I have included the jQuery, ad jQuery.UI references inside of the "form" tags.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...