ASP.NET confirm delete in a grid - asp.net

I need to add a confirm delete action to a grid. the problem is the way the "Delete" link is rendered.
my grid is built in code behind in vb.net.
i have this
colDelete.AllowDelete = True
colDelete.Width = "100"
AddHandler CType(gridSavedForLater, Grid).DeleteCommand, AddressOf dgDeleteSelectedIncident
and the sub is the following
Sub dgDeleteSelectedIncident(ByVal sender As Object, ByVal e As GridRecordEventArgs)
Dim message As String = "Are you sure you want to delete this incident?"
Dim caption As String = "Confirm Delete"
Dim result = System.Windows.Forms.MessageBox.Show(message, caption, Windows.Forms.MessageBoxButtons.OKCancel, Windows.Forms.MessageBoxIcon.Warning)
'If (result = Windows.Forms.DialogResult.Cancel) Then
'Else
' MessageBox("Are you sure you want to delete this incident?")
'Get the VehicleId of the row whose Delete button was clicked
'Dim SelectedIncidentId As String = e.Record("IncidentId")
''Delete the record from the database
'objIncident = New Incidents(SelectedIncidentId, Session("userFullName"))
'objIncident.DeleteIncident()
''Rebind the DataGrid
LoadSavedForLater()
'' End If
End Sub
i need to add a javascript confirm dialog when this sub is called. i can do it with a windows form messagebox but that does not work on the server.
pleae help
joe

You cannot show a MessageBox in ASP.NET since it would be shown on the server. So you need a javascript confirm onclick of the delete button. Therefor you don't need to postback to the server first. You can attach the script on the initial load of the GridView.
A good place would be in RowDataBound of the GridView:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
' if it's a custom button in a TemplateField '
Dim BtnDelete = DirectCast(e.Row.FindControl("BtnDelete"), Button)
BtnDelete.OnClientClick = "return confirm('Are you certain you want to delete?');"
' if it's an autogenerated delete-LinkButton: '
Dim LnkBtnDelete As LinkButton = DirectCast(e.Row.Cells(0).Controls(0), LinkButton)
LnkBtnDelete.OnClientClick = "return confirm('Are you certain you want to delete?');"
End Select
End Sub

As an alternative, a common method of notifying users in a web page of a status change (save, delete, update, etc.) is having an update element in your HTML. Basically, a label (or whatever) that you'll set with updates to the user.
"Your changes have been successfully saved." or "An error was encountered when trying to save your update.", for example. You can set the color red for an error or whatever your programming heart desires, stylistically.
I kind of like this approach as a pop-up always feels more like a WinForm thing to me. Either works, so I just thought I'd suggest another approach.

Related

Persisting Dynamic ReadOnly Field Past Initialisation

I create a readonly textbox dynamically on page load/init and on the first load (not IsPostBack) I set the text. I also have a button on the page with a click event that changes the content of the textbox. Using another button, I read the text of the textbox. The problem is as follows:
If I click the button that changes the textbox and then click the button that reads the text of the textbox, it gets it fine.
If I just load the page and click the button that reads the text of the textbox, it brings back an empty string
I need both scenarios to bring back a result - whether it's the original text or the new programmatically changed text.
Example code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Textbox As New TextBox()
Textbox.ID = "Bob"
Textbox.ReadOnly = True
If Not IsPostBack Then
Textbox.Text = "Initial Text"
End If
Content.Controls.Add(Textbox)
Dim Button As New Button()
Button.Text = "Change text"
AddHandler Button.Click, AddressOf Button_Click
Content.Controls.Add(Button)
End Sub
Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
'FindControlRecursive is locally defined - just finds a control based on root control and ID
CType(FindControlRecursive(Content, "Bob"), TextBox).Text = "Hmmmmm"
End Sub
Private Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
Dim Textbox As TextBox = CType(FindControlRecursive(Content, "Bob"), TextBox)
MsgBox(Textbox.Text)
End Sub
I could just reinitialise the textbox each time but this is just an example and in practice I'll be pulling it from SQL and if I have several read only controls on the page I'd rather get the control to persist its text rather than going back to SQL each time.
I found two ways to solve this, the second being the more favoured approach (depending on situation):
Specifically add the initial text to the viewstate and then set the text on each postback using the value from viewstate:
If Not IsPostBack Then
Textbox.Text = "Initial Text"
ViewState("Bob") = Textbox.Text
Else
Textbox.Text = ViewState("Bob")
End If
Add a PreRender event to the textbox that just sets it text to itself. This takes place after the ViewState starts being tracked so it's added to the viewstate via the control and is persisted across postbacks:
If Not IsPostBack Then
Textbox.Text = "Initial Text"
AddHandler Textbox.PreRender, AddressOf PersistPastInitialLoad
End If
And add new sub:
Protected Sub PersistPastInitialLoad(ByVal sender As Object, ByVal e As EventArgs)
'This is just for example - would need refining for different types of controls
CType(sender, TextBox).Text = CType(sender, TextBox).Text
End Sub
Essentially they both do the same thing - hold the text in viewstate - but it depends on implementation which is the best to use. The first implementation might be a bit easier to read/work with but it would result in redundant viewstate bloat if you click the button to change the textbox text (i.e. you'll have the initial text in viewstate against the viewstate key you created as well as the current text added to viewstate by the control onchange).

Gridview data not refreshing

I have a gridview which has a List (Of MyObject) as its datasource.
gv.DataSource = MyListObject
gv.Databind()
Under a templatefield i have a button configured to delete a record from MyListObject and then rebind it. To add a record i have the below code
Protected Sub btnAddRecord_Click(sender As Object, e As EventArgs) Handles btnAddRecord.Click
Dim Customer As New Customer
With Customer
.Name = txtName.Text
.Surname = txtSurname.Text
.....
.ID += MyListObject.Count
End With
MyListObject.Add(Customer)
gv.DataSource = MyListObject
gv.DataBind()
End Sub
This works fine, but then i need to allow the user to delete a record if need be:
Private Sub gv_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
If e.CommandName = "Delete" Then
MyListObject.RemoveAt(e.CommandArgument)
gv.DataSource = Nothing
gv.DataBind()
gv.DataSource = MyObjectList
gv.DataBind()
upnl.UpdateMode = UpdatePanelUpdateMode.Conditional
upnl.Update()
End If
End Sub
When i click the button it deletes the record but doesnt refresh the data. By that i mean when the record is added i am assigning the ID as a row ID and then use that ID to remove the record. Since the List and Gridview values are now out of sync i set the datasource to nothing and rebind it in order that i was going to have the values reset and the ID would be the correct one - but this doesnt works as i expected.
Could anyone advise where im going wrong and how to correct this problem?
Is gridview in the updatepanel? If yes, that panel should also be refreshed.
I added CommandArgument='<%# Container.DataItemIndex %>' which resolved the issue as i was then deleting the row the user clicked against.
Just as a side note, I'd probably handle what get's updated when on the client side using the ajax script manager rather than doing it in the code behind. Saves headaches. The above might be updating the update panel correctly, but the ajax plumbing may not be there on the client side.

Filling the dropdown from sql bug

Good afternoon people have been trying to fill a dropdown using a sql command, until so good, when I click on the dropdown it shows all the items, but when I try to click on an item in the dropdown it always returns the first item in the dropdown .... follows the codes, what i want to do is get the selected value and item from the dropdown and save it on a label for future use.
I appreciate all the support possible,
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
utilizador.Text = Me.Context.User.Identity.Name
If (Not Page.IsPostBack) Then
escolhePerfil()
End If
'DropDownPerfil.DataBind()
lbperfil2.Text = DropDownPerfil.SelectedItem.Text
lbnome.Text = DropDownPerfil.SelectedValue
End Sub
Function escolhePerfil() As Boolean
Dim connstring As String = "Data Source=10.2.24.17;Persist Security Info=True;User ID=sa;Password=Pr0dUn1C0$qL;database=ePrimavera"
Dim SQLData As New System.Data.SqlClient.SqlConnection(connstring)
Dim cmdSelect As New System.Data.SqlClient.SqlCommand("SELECT u.WindowsUser,u.Email ,g.Description, u.Login FROM [ePrimavera].[dbo].[PLT_Users] as u,[ePrimavera].[dbo].[PLT_UserGroups] as ug, [ePrimavera].[dbo].[PLT_Groups] as g where u.ID = ug.UserID And ug.GroupID = g.ID and u.WindowsUser like 'bancounico\" & utilizador.Text & "'", SQLData)
SQLData.Open()
Dim dtrReader As System.Data.SqlClient.SqlDataReader = cmdSelect.ExecuteReader()
If dtrReader.HasRows Then
DropDownPerfil.DataValueField = "Login"
DropDownPerfil.DataTextField = "Description"
DropDownPerfil.DataSource = dtrReader
DropDownPerfil.DataBind()
End If
SQLData.Close()
Return True
End Function
.aspx
<asp:DropDownList ID="DropDownPerfil" runat="server"
Height="16px" Width="202px" CssClass="DropBorderColor">
</asp:DropDownList>
try the following code:
Protected Sub DropDownPerfil_SelectedIndexChanged(sender As Object, e As EventArgs)
lbperfil2.Text = DropDownPerfil.SelectedItem.Text
lbnome.Text = DropDownPerfil.SelectedValue
End Sub
The problem is that you are trying to get the value "too early". The value is not valid in the Page_Load, since the control's OnLoad event fired after the Page's OnLoad (Page_Load) event.
The way, what the others wrote the correct, since the Event handlig section (inclueding control's onchanged event) will process after the Load section.
For more details check the offical ASP.NET life cycle site in the MSDN
The likely reason is that some (if not all) the values you are assigning to the DropDownList for Login are occur more than once.
When you then select an item in the DDL, if the value of that item occurs more than once, the selected index will highlight the first instance. To test this, comment out DropDownPerfil.DataValueField = "Login"
I am sure upon selection, it will highlight the correct item you intended on selecting.

Deleting Dynamically Populated images from a Directory (NOT GridView) ASP.NET (VB)

The code below displays a thumbnail of each image in a specific server directory and when I click on the image it pops up a nice full sized picture. It works perfectly.
I would however, like to be able to delete an image. I first thought I could have a button at the bottom of the page with a checkbox next to each image, giving it a uniqueID as per the filename but as they are dynamically created I couldn’t figure how to handle the Click Event on the button for a randomly named Checkbox ID. Then I tried adding a button next to each item and then tried an OnClick & OnServerClick to call a Sub but this didn’t work either.
Any/All suggestions welcomed :)
Private Sub ImageList()
If Directory.Exists(Server.MapPath("JobImages\" & DBC_JOB_JobID.Text)) Then
Dim MySB As New StringBuilder
Dim dirInfo As New DirectoryInfo(Server.MapPath("JobImages\" & DBC_JOB_JobID.Text))
MySB.Append("<ul class=""clearfix"">")
MySB.AppendLine()
For Each File In dirInfo.GetFiles()
MySB.Append("<li><a rel=""jobpic"" href=""JobImages\" & DBC_JOB_JobID.Text & "\" & File.Name & """><img src=""JobImages\" & DBC_JOB_JobID.Text & "\Thumbs\" & File.Name & """ width=""150"" height=""100"" /> <span class=""size"">" & File.Name & " </span></a></li>")
MySB.AppendLine()
Next
MySB.Append("</ul>")
MySB.AppendLine()
lblMyPictures.Text = MySB.ToString
End If
End Sub
OK what Kendrick is talking about (basically) is using server side controls to keep track of which file to delete. What you are doing right now is dumping markup into a Label control, which on postback won't fire an event on the server side. However you can accomplish this easily with server side controls.
The basic idea is you use a container control such as a Panel and add each child control to it. Then you hook events to each row with data identifying that row (such as filename).
Markup:
<asp:Panel ID="pnlList" runat="server">
</asp:Panel>
Code-Behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Directory.Exists(Server.MapPath("Files")) Then
Dim objDirInfo As New DirectoryInfo(Server.MapPath("Files"))
For Each objFile As FileInfo In objDirInfo.GetFiles()
Dim objLabel As New Label
objLabel.Text = objFile.Name
Dim objLinkButton As New LinkButton
objLinkButton.Text = "Delete"
objLinkButton.CommandName = "Delete"
objLinkButton.CommandArgument = objFile.Name
AddHandler objLinkButton.Command, AddressOf DeleteFile
Dim objLiteral As New LiteralControl
objLiteral.Text = "<br/>"
pnlList.Controls.Add(objLabel)
pnlList.Controls.Add(objLinkButton)
pnlList.Controls.Add(objLiteral)
Next
End If
End Sub
Public Sub DeleteFile(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
If e.CommandName = "Delete" Then
Dim strFileName As String = Server.MapPath("Files\" & e.CommandArgument)
If File.Exists(strFileName) Then
Dim objFile As New FileInfo(strFileName)
objFile.Delete()
End If
End If
End Sub
This would be an excellent example of where using a data aware would make your life a lot easier.
That said, if you didn't want to use a server-side control, you could assign an ID to each checkbox (i.e. DeleteImage_1) and then store the ID and associated image name in the viewstate on the page. Go through the checked checkboxes and refer back to the viewstate for the name that goes with each ID when they click the delete button.

Accessing TextBox Column in RadGrid By Telerik

Do you know how to access textboxes added to a radgrid that are not bound but are used to trap any row related input a user typed in to the textbox for that column.
I need to access this data server side when a postback occurs.
Your thoughts are greatly appreciated
Thanking you
Tony
That depends on how those textboxes are being added/created. If by 'not bound' you mean they are in Template columns you should be able to use .FindControl in one of the grid's events to grab that textbox.
And again which event will depend on what is causing the postback to happen.
For the purpose of this code example I'll assume you are dealing with a Command item on the grid
Private Sub radGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
Select Case e.CommandName
Case "Update"
Dim txt as Textbox
txt = e.Item.FindControl("textboxID")
If Not txt is Nothing Then someObject.someString = txt.Text
Case Else
'do something else
End Sub
Hope that helps.
Private Sub radGrid_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand
Select Case e.CommandName
Case "Update"
Dim txt as Textbox
txt = e.Item.FindControl("textboxID")
If Not txt is Nothing Then someObject.someString = txt.Text
Case Else
'do something else
End Sub

Resources