Solved

Display Popup / MessageBox / Alert AFTER GridView Add / Edit / Delete in VB.Net

Posted on 2012-12-29
8
1,694 Views
Last Modified: 2013-04-19
I would like to display a pop-up / alert / confirmation box AFTER a user performs an add / edit / delete on a GridView in VB.Net, such as:

1. Your new event has been added successfully
2. Your event has been modified successfully
3.  Your event has been deleted successfully

I already have a "Are you sure you want to Delete this Item" which is displayed before a deletion takes place, but I need to have something AFTER the code is processed.

So I need the server to trigger something on the client side, when a Subroutine completes.

How can I do this ?

My code is below.

Thanks, Peter


<script runat="server">
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)        
        
        If Not Page.IsPostBack Then

            MultiView1.ActiveViewIndex = 0
            
        End If

        ' Bind the datagrid        
        BindData
           
    End Sub

    Sub BindData()

        ' Prepare the datagrid
        Editgrid.Columns(0).Visible = "false"
        EditGrid.Columns(1).Visible = "false"
        EditGrid.Columns(2).HeaderText = Ttext(26)
        EditGrid.Columns(3).HeaderText = Ttext(27)
        EditGrid.Columns(4).HeaderText = Ttext(28)
        EditGrid.Columns(5).HeaderText = Ttext(29)
        EditGrid.Columns(6).HeaderText = Ttext(30)
        EditGrid.Columns(7).HeaderText = Ttext(13)

        ' Get data to fill datagrid
        Dim MySQL5 As String = "SELECT g.gamenum, l.shortname as Gamelocation, g.gamedatetime as Gamedates, g.gametype, lgh.groupname + ' : Practice', ct.lastname + ', ' + ct.firstname as Snackresp, ct1.lastname + ', ' + ct1.firstname as Refresp FROM games g, contacts ct, contacts ct1, locations l, leagues lgh, leagues lga, leaguesetup ls WHERE g.gamelocnum = l.locnum and g.snacknum = ct.usernum and g.refereenum = ct1.usernum and lgh.groupnum = g.hometeamnum and lga.groupnum = g.awayteamnum and g.leaguenum = " & Session("leaguenum") & " order by g.gamedatetime desc"
        Dim objCmd5 As New SqlDataAdapter(MySQL5, MyConn)
        objCmd5.Fill(ds, "gametable")
        EditGrid.DataSource = ds.Tables("gametable").DefaultView
        EditGrid.DataBind()
    End Sub
        
    
    Protected Sub AddItem(ByVal sender As Object, ByVal e As System.EventArgs)
         
	    Gamedates = picker2.Text
	    Gametimes = Gametime.Text
        
        cmd.Connection = MyConn
        cmd.Connection.Open()
        
        ' Insert the new item                    
        cmd.CommandText = "Insert into games (Leaguenum, Gametype, Gamelocnum, Gamedatetime, Hometeamnum, Awayteamnum, needref, refereenum, needsnack, snacknum, Gamestatus) values (" & Session("Leaguenum") & ", '" & Inputtype & "', " & Rinklocation.SelectedItem.Value & ",Convert(datetime,'" & gamedates & " " & Gametimes & "'), " & Hometeam.SelectedItem.Value & ", " & Awayteam.SelectedItem.Value & ", '" & needrefs.SelectedItem.Text & "', " & Refentry & ", '" & needsnacks.SelectedItem.Text & "'," & Snackentry & ", 'Not played')"
        cmd.ExecuteNonQuery()
        
        cmd.Connection.Close()
	
         BindData
        
    End Sub
    
    Protected Sub EditItem(ByVal sender As Object, ByVal e As System.EventArgs)
 
            cmd.Connection = MyConn
            cmd.Connection.Open()

            Gamenum = Request.QueryString("gnum")
		
            gamedates = picker2.Text
            Gametimes = Gametime.text
    
            cmd.CommandText = "Update games set Gametype =  '" & Gametype.SelectedItem.Text & "', Gamelocnum = " & Rinklocation.SelectedItem.Value & ", Gamedatetime = Convert(datetime,'" & gamedates & " " & Gametimes & "'), Gamestatus = 'Not played',  Eventtitle = '', Eventdesc = '', Hometeamnum = " & Hometeam.SelectedItem.Value & ", Awayteamnum = " & Awayteam.SelectedItem.Value & ", needref = '" & needrefs.SelectedItem.Text & "', refereenum = " & Refentry & ", needsnack = '" & needsnacks.SelectedItem.Text & "', snacknum = " & Snackentry & " where Gamenum = " & Session("myitemnum")
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
            
             BindData
        
    End Sub
    
    Protected Sub DeleteItem(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
 
        ' Get the gamenum
        Dim selectedRecord As DataKey = EditGrid.DataKeys(EditGrid.SelectedIndex)
        Gamenum = selectedRecord.Value
    
        cmd.Connection = MyConn
        cmd.Connection.Open()
    
        ' Delete game 
        cmd.CommandText = "Update games set gamestatus = 'disabled' WHERE gamenum = " & Gamenum
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

         BindData
        
    End Sub
        
    Protected Sub LoadItem(ByVal sender As Object, ByVal e As EventArgs)
  
        Dim selectedRecord As DataKey = EditGrid.DataKeys(EditGrid.SelectedIndex)
        MyItemnum = selectedRecord.Value
        Session("myitemnum") = MyItemnum

        cmd.Connection = MyConn
        cmd.Connection.Open()
        cmd.CommandText = "Select Gametype, Gamelocnum, right(Convert(varchar,gamedatetime,0),7) as MyTime, Convert(varchar,gamedatetime,101)as DTT, Hometeamnum, Awayteamnum, needref, Refereenum, needsnack, snacknum from games where Gamenum = " & Session("myitemnum")
               MyReader = cmd.ExecuteReader()
		
                While MyReader.Read()
                    Agametype = MyReader("Gametype").ToString()
                    TGamelocnum = MyReader.GetInt32(1)
                    Gametime.Text = MyReader("MyTime").ToString()
                    picker2.Text = MyReader("DTT").ToString()
                        THometeamnum = MyReader("Hometeamnum")
                        TAwayteamnum = MyReader("Awayteamnum")
                        Tneedref = MyReader("needref")
                        Tneedsnack = MyReader("needsnack")
                        Tsnacknum = MyReader("snacknum")

                End While
		
                MyReader.Close()
                cmd.Connection.Close()
            
            ' Fill the datagrid
            BindData
 
    End Sub
     
</script>

<asp:Content ID="Content3" ContentPlaceHolderID="Mycontent" runat="Server">   
    
    <table>
        <tr>
            <td class="WizardTitle">
                <asp:Label ID="Pagetitle" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="WizardInstructions">
                <asp:Label ID="Helporerror" runat="server"></asp:Label></td>
        </tr>
        <tr>
            <td class="WizardContent">
                <div align="center">
                <table>
                    <tr>
                        <td style="padding: 5 0 20 0">
                            <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
                                <asp:View ID="Instructions" runat="server">
                                    <h5>
                                        <asp:Label ID="GeneralInstructions" runat="server" />
                                    </h5>
                                </asp:View>
                                <asp:View ID="Managepages" runat="server">
                                        <asp:Panel ID="ShowForm" runat="server">
                                        <table class="InsideBox">
                                            <tr>
                                                <td colspan="3" class="InsideboxSubTitle">
                                                    <asp:Label ID="eventTitle" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="aptfont2" style="padding-top: 12px; width:200px">
                                                    <asp:Label ID="eventtypeLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Gametype" OnSelectedIndexChanged="CheckEvent" AutoPostBack="true" Width="110" runat="server" />
                                                </td>
                                                <td class="aptfont2" style="padding-top: 12px; width:250px">
                                                    <asp:Label ID="gamedateLabel" runat="server" /><br />
                                                    <asp:TextBox ID="picker2" runat="server" Width="150" />&nbsp;&nbsp;<asp:Image ID="Image1" runat="server"
                                                        ImageUrl="~/images/calendar1.gif" ImageAlign="Middle" />
                                                    <ajaxToolkit:CalendarExtender runat="server" TargetControlID="picker2" 
                                                        PopupButtonID="Image1" />
                                                    <asp:RequiredFieldValidator ID="CompareValidator4" runat="server" ControlToValidate="picker2"
                                                        Display="None" ErrorMessage="You must enter a date" />
                                                    <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender4"  runat="server" TargetControlID="CompareValidator4" /> 
                                                </td>
                                                <td class="aptfont2" style="padding-top: 12px; width:250px">
                                                    <asp:Label ID="gametimeLabel" runat="server" Text="Game time"/><br />
                                                        <asp:TextBox ID="Gametime" runat="server" Width="150" />
                                                        <ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender3" runat="server" TargetControlID="Gametime"
                                                            Mask="99:99" MessageValidatorTip="true" OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError"
                                                            MaskType="Time" AcceptAMPM="true" ErrorTooltipEnabled="False" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="aptfont2">
                                                    <asp:Label ID="locationLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Rinklocation" Width="150" runat="server" DataTextField="Shortname"
                                                        DataValueField="Locnum">
                                                    </asp:DropDownList>
                                                </td>
                                                <td class="aptfont2">
                                                    <asp:Label ID="hometeamLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Hometeam" Width="200" runat="server" DataTextField="Hometeamname"
                                                        DataValueField="Hometeamnum" OnSelectedIndexChanged="DisplaySnacks" AutoPostBack="true">
                                                    </asp:DropDownList> 
                                                </td>
                                                <td class="aptfont2">
                                                    <asp:Label ID="awayteamLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Awayteam" Width="200" runat="server" DataTextField="Awayteamname"
                                                        DataValueField="Awayteamnum" OnSelectedIndexChanged="DisplaySnacks" AutoPostBack="true">
                                                    </asp:DropDownList> 
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="3" class="InsideboxSubTitle">
                                                    <asp:Label ID="refTitle" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="aptfont2" style="padding-top: 12px"> 
                                                    <asp:Label ID="refreqdLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="needrefs" Width="110" runat="server" OnSelectedIndexChanged="DisplayRef" AutoPostBack="true" >
                                                    </asp:DropDownList>
                                                </td>
                                                <td class="aptfont2" style="padding-top: 12px"> 
                                                    <asp:Label ID="gamerefLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Gameref" Width="200" runat="server" DataTextField="Fullname"
                                                        DataValueField="Usernum">
                                                    </asp:DropDownList>
                                                </td>
                                                <td>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="3" class="InsideboxSubTitle">
                                                    <asp:Label ID="snacksTitle" runat="server" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="3" style="text-align:left; font-size: 10pt">
                                                    <asp:Label ID="informLabel" runat="server" />
                                                </td>        
                                            </tr>
                                            <tr>
                                                <td class="aptfont2" style="padding-top: 12px" >                                                                                           
                                                    <asp:Label ID="snacksreqdLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="needSnacks" Width="110" runat="server"  OnSelectedIndexChanged="DisplaySnacks" AutoPostBack="true" />
                                                </td>
                                                <td class="aptfont2" style="padding-top: 12px">                                                                                           
                                                    <asp:Label ID="snackLabel" runat="server" /><br />
                                                    <asp:DropDownList ID="Snacks" Width="200" runat="server" DataTextField="Players"
                                                        DataValueField="Usernum">
                                                    </asp:DropDownList>
                                                </td>
                                                <td>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="4" align="right">
                                                    <asp:Button ID="insertButton" OnClick="AddItem" runat="server" />
                                                    &nbsp;
                                                    <asp:Button ID="updateButton" OnClick="EditItem" runat="server" />
                                                    &nbsp;
                                                    <asp:Button ID="canceladdButton" CausesValidation="False" OnClick="CancelAdd" runat="server" />
                                                    &nbsp;
                                                    <asp:Button ID="canceleditButton" OnClick="CancelEdit" runat="server" />
                                                </td>
                                            </tr>
                                        </table>
                                        </asp:Panel>
                                        <table>
                                        <tr>
                                            <td class="AdminTitle">
                                                <asp:Label ID="schedulegridLabel" runat="server" /></td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <div style="width: 725; height: 300; overflow: auto" align="center">
                                                    <asp:GridView ID="EditGrid" runat="server" Width="700" CellPadding="3" 
                                                        DataKeyNames="gamenum" RowStyle-Font-Size="10pt" HeaderStyle-Font-Size="10pt"
                                                        HeaderStyle-ForeColor="white" HeaderStyle-BackColor="SteelBlue" AutoGenerateColumns="false"
                                                        OnSelectedIndexChanged="LoadItem" allowPaging="true" PageSize="30" OnRowDeleting="DeleteItem">
                                                        <Columns>
                                                            <asp:CommandField ItemStyle-Width="70" ShowSelectButton="true" ButtonType="Button"
                                                                ItemStyle-HorizontalAlign="Center" ControlStyle-Font-Size="9pt" ControlStyle-BackColor="steelblue"
                                                                ControlStyle-ForeColor="white" />
                                                            <asp:TemplateField ShowHeader="False" ItemStyle-Width="60"  ItemStyle-HorizontalAlign="Center">
                                                                <ItemTemplate>
                                                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                                                                        OnClientClick='return confirm("Are you sure you want to delete this entry?");'
                                                                        Text="Delete" Commandname="Delete" CommandArgument='<%# Eval("gamenum") %>'  />
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                            <asp:BoundField DataField="Gametype" ItemStyle-Font-Size="11px" ItemStyle-Width="75" ItemStyle-HorizontalAlign="Center" />
                                                            <asp:TemplateField ItemStyle-Font-Size="11px" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" >
                                                                <ItemTemplate>
                                                                    <asp:Label ID="myDates" runat="server" Text='<%# MyTools.Makedate(Eval("Gamedates"),"shorttime") %>' />
                                                                </ItemTemplate>
                                                            </asp:TemplateField>
                                                            <asp:BoundField DataField="Gamelocation" ItemStyle-Font-Size="11px" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" />
                                                            <asp:BoundField DataField="Teams" ItemStyle-Font-Size="11px" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" />
                                                            <asp:BoundField DataField="Snackresp" ItemStyle-Font-Size="11px" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" />                                                      
                                                            <asp:BoundField DataField="Refresp" ItemStyle-Font-Size="11px" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" /> 
                                                        </Columns>
                                                    </asp:GridView>
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                </asp:View> 
                            </asp:MultiView>
                        </td>
                    </tr>
                </table>
                </div>

Open in new window

0
Comment
Question by:pbissegger
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 167 total points
ID: 38729086
Hi,

You can use PageStartupScript
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script type="text/javascript">        // initialize dialog box 
        function CallCondition() { 
            alert("Hi"); 
        } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
    </div> 
    </form> 
</body> 
</html>

Open in new window


protected void Button1_Click(object sender, EventArgs e) 
    { 
        if (TextBox1.Text == "") 
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "scriptKey", "CallCondition();",true); 
    }

Open in new window

0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 38729089
and If you are using AJAX then use ScriptManage function instead of Page

ScriptManager.RegisterStartupScript

Thanks
0
 
LVL 25

Assisted Solution

by:Rouchie
Rouchie earned 166 total points
ID: 38729185
An easy way is to push the javascript to generate the alert into the head area dynamically, so it disappears on each subsequent postback when its no longer needed, like this:

    Protected Sub DeleteItem(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
 
        ' Get the gamenum
        Dim selectedRecord As DataKey = EditGrid.DataKeys(EditGrid.SelectedIndex)
        Gamenum = selectedRecord.Value
    
        cmd.Connection = MyConn
        cmd.Connection.Open()
    
        ' Delete game 
        cmd.CommandText = "Update games set gamestatus = 'disabled' WHERE gamenum = " & Gamenum
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

         BindData
	page.header.controls.add(new literalcontrol("<script type=""text/javascript"">window.alert('Your record was deleted!');</script>"))        
    End Sub

Open in new window

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38729742
At the end of your AddItem, EditItem, DeleteItem functions, add following code

    Page.ClientScript.RegisterStartupScript(Page.GetType(), "AlertScript", "Your new event has been added successfully", True)


Change the 3rd argument depending on function your put this line in.
0
 

Author Comment

by:pbissegger
ID: 38730070
Guys,

I tried a couple of the solutions, and here are the results:

1. IJZ's solution did not produce an error, but did not produce the popup after the Datagrid item was deleted.

I am using Masterpages, perhaps this is causing the problem and I will investigate further.

2. Both Rouchie and Codecruiser's solutions resulted in the page not being displayed, and the following error message:

"Server Error in '/' Application.

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Only Content controls are allowed directly in a content page that contains Content controls.

Source Error:


[No relevant source lines]"

It looks like this is also a Masterpage problem.

Any suggestions ?

Thanks, Peter
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 167 total points
ID: 38730117
And does this work?


    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "AlertScript", "Your new event has been added successfully", True)
0
 

Author Comment

by:pbissegger
ID: 38731589
Code cruiser,

Same issue with the new code.

Peter
0
 

Author Comment

by:pbissegger
ID: 38731607
Wait,

Hold on a second - the new code does not cause the parser error.

I had commented out the code from Rouchie ... and the commented out code cause the error because he had the word "</script>" in his code preceeded by a single apostrophe. When I commented it out, the page thought the "</script>" was the end of the code....

Sorry ....

Am testing the solutions again
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question