Display Popup / MessageBox / Alert AFTER GridView Add / Edit / Delete in VB.Net
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" /> <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" /> <asp:Button ID="updateButton" OnClick="EditItem" runat="server" /> <asp:Button ID="canceladdButton" CausesValidation="False" OnClick="CancelAdd" runat="server" /> <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>
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.
pbissegger
ASKER
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.
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....
ScriptManager.RegisterStar
Thanks