ASP.NET Confirm Message without OnClientClick/Button - asp.net

is it possible to raise a confirmation message with yes/no from code behind, without any use of client click?
My problem setup:
A row is going to be deleted from a datagrid
delete function checks if the selected row has dependencies
if the row has children (dependencies) raise a message if the user wants to keep going with the delete process, if not cancel the function.
I can't check the dependencies beforehand to set the javascript confirm on the delete button (performance would die [there are other database issues...]).
I need to raise the message from the function with an return value which the code behind can use. Is there any native way to do this?
Small sample:
Private Sub Delete(ByVal sender As Object, Optional ByVal e As System.EventArgs = Nothing)
//Do check for dependencies...
If intDependencyCounter > 0 Then
//This is where i need my message to be shown
Dim blUserInput As Boolean = True/False //depends on user input
If blUserInput = False Then
//Cancel function...
Else
//Keep going with function...
EndIf
EndIf
End Sub
Hope someone can give me an idea how to solve this.
Regards,
Megajin

Related

Why If statement is repeat for twice?

I have a "if" or "case" statement in the a method that I can called. I have "Flag" varriable as public status. Its Crazy its loop/running for twice, so cause have duplicate command, in these case I have duplicate data on the database.
Public Sub compartment1(ByVal exec As Boolean)
Try
If exec = True Then
Select Case FlagMark
Case 1
Insert database execute command
Case 2
another command
End Select
ElseIf exec = False Then
End If
Catch ex As Exception
End Try
End Sub
And this is my button command
Private Sub cmd_confirm_Click(sender As Object, e As EventArgs) Handles cmd_confirm.Click
If pCheck2.Checked = True Then
FlagMark = 1
compartment1(True)
End If
End Sub
The Method "Compartment1" is running twice, look like looping, so I have a duplicate data or duplicate for single command. Can it running only once?
Solved! after I replace the html button that given runat=server attributes with ASP.Net button.
Why is so different, If with ASP.NET button the event is running normaly step by step the if or case statement, While using Html button with runnat=server is look like looping process. Even though has a same properties configuration as bellow :
CausesValidation =True
EnableViewState=True, etc

ASP.NET sqldatasource.select(arg) gives stackoverflow.exception error

I have a code to read the total number of rows in a SQLDatasource:
Protected Sub DSArticles_Selected(sender As Object, e As SqlDataSourceStatusEventArgs) Handles DSArticles.Selected
Dim args As DataSourceSelectArguments = New DataSourceSelectArguments
Dim dv As DataView = DSArticles.Select(args)
dv.RowFilter = DSArticles.FilterExpression
LblCikkekSzama.Text = dv.Count & " cikk"
End Sub
The browser says the page cannot be viewed. When in debugging mode I get the error:
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll.
Any ideas?
Thanks
Sudi
I have a feeling you're causing an infinite loop because in the selected event you're doing Select() which triggers the selected event again, which calls Select again, etc... So this is probably not a good place to do this. Maybe move it to page_load or somewhere else that wouldn't trigger the selected event continuously. Somewhere that makes sense for what you're trying to accomplish.

VB.Net exception: Object reference not set to an instance of an object

I'm currently working on coding a web page for a school project. This site is supposed to be a simple online store where people can order prints of artwork. The specific page I'm working on has a Drop Down List (ddlArt) that is bound to my database and displays a list of the different art pieces available. When the user selects one of the items, all the information on that item is pulled from the database and displayed on the page in a variety of labels and such. The only thing is that I'm getting a null reference exception error saying "Object reference not set to an instance of an object" when I go to try to run the page. I got the same error on a homework assignment earlier in the year and managed to get it fixed, but I can't remember what I did and I can't get help from school until next week, so I thought I'd try my luck on here. Here's my code:
Private selectedArt As Art
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlArt.DataBind()
End If
selectedArt = Me.GetSelectedArt
lblArtID.Text = selectedArt.ArtID()
lblArtName.Text = selectedArt.ArtName()
lblCaption.Text = selectedArt.Caption()
lblDescription.Text = selectedArt.Description()
imgArt.ImageUrl = "~/images/" & selectedArt.FileName()
End Sub
Private Function GetSelectedArt() As Art
Dim artTable As DataView = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'"
Dim artRow As DataRowView = artTable(0)
Me.imgArt.ImageUrl = "~/images/" & artRow("FileName")
Dim art As New Art
art.ArtID = artRow("ArtID").ToString
art.ArtName = artRow("ArtName").ToString
art.Caption = artRow("Caption").ToString
art.Description = artRow("LongDescription").ToString
art.FileName = artRow("FileName").ToString
Return art
End Function
And here's the code for the Art class, in case anybody is interested:
Public Class Art
Public Property ArtID As Integer
Public Property ArtName As String
Public Property ArtType As String
Public Property Caption As String
Public Property FileName As String
Public Property Description As String
End Class
When I get the error, it highlights the artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'" line in the GetSelectedArt function. I've tried comparing it to my corrected homework assignment that I mentioned, but I can't seem to find the problem. My VB is a little fuzzy because it's been awhile since I actually took the class. Any suggestions? Thanks a bunch!
If I understand your comment above correctly, on the initial page load there is nothing in the ddlArt, because the user must first choose an art type.
If that is correct, then your answer to my question is your answer.
For whatever reason (and without seeing at least the Select statement), artTbl is not getting instantiated, which is why you're seeing the Object reference not set to an instance of an object error.
One way to fix this (without knowledge of your SqlDataSource it's hard to give a precise answer) is to modify your Page Load method so that GetSelectedArt is only called when the user has selected an item from the drop down list. Right now GetSelectedArt is called every time the page loads.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlArt.DataBind()
Else
selectedArt = Me.GetSelectedArt
lblArtID.Text = selectedArt.ArtID()
lblArtName.Text = selectedArt.ArtName()
lblCaption.Text = selectedArt.Caption()
lblDescription.Text = selectedArt.Description()
imgArt.ImageUrl = "~/images/" & selectedArt.FileName()
End If
End Sub
However, the above modification will only prevent GetSelectedArt from being called on the initial page load. If your SqlDataSource.Select command is still returning nothing, then you're still going to have this problem.
A better solution would be to call the GetSelectedArt on the ddlArt.SelectedIndexChanged event handler. This way you'll know that you have (or should have) a valid SelectedValue from ddlArt.
Also, if you don't populate the drop down until the user selects an art type from the radio button list, why are you binding the drop down list on the initial page load (and what are you binding it to)? Or is the drop down list and detail information on a different page from the radio button list?
May be .. with ArtID as integer
artTable.RowFilter = "ArtID = " & format(ddlArt.SelectedValue)

How do I Prevent FormView from clearing user's entered Values after Insert Method has fired?

I have been struggling with getting FormViews to work the way Microsoft expects me to for about a day and have figure a bunch of great stuff out.
I can catch e.Exception and e.ReturnValue in the ObjectDataSource.Inserting Event Handler and I can even cheat and check other properties of the Object in the ObjectDataSource.ObjectDisposing by checking the e.ObjectInstance ... and I even learned that FormView's Inserting Handler Runs AFTER the ObjectDisposing Handler so If there is a problem found I still have time to react to it and st the e.KeepInInsertMode to true on the FormView.
My problem is, it seems that the values entered by the user into the Insert form are cleared regardless.
So, How do I Prevent a FormView from clearing after it's Insert Method has fired?
(Using ASP.NET + VB)
I don't think posting my code here will really do much good and i would have to modify it to trim out confidential business logic stuff... so I'll skip it for now.
edit:
I have found a temporary and admittedly terribly cludgy solution (in case no one ever finds a REAL solution to the problem).
I have a page variable defined as:
Dim eInsertArgs As FormViewInsertedEventArgs
And then I do the following in my ItemInserted handler
If boolInsertErrorOccurred = False Then
e.KeepInInsertMode = True
eInsertArgs = e
Else
eInsertArgs = Nothing
End If
Then on each of the controls I have something like this in that controls databinding event:
If IsNothing(eInsertArgs) = False Then
Dim _sender As TextBox = sender
_sender.Text = eInsertArgs.Values("_FieldName")
End If
The effect of this is that I am setting the values BACK to the submitted values AFTER ASP.NET binds the FormView to the default (blank) Template.
Please help me find a less terrible solution. :)
You need to create your own server control which inherits from the FormView control.
Public Class MyFormView
Inherits FormView
Protected Overrides Sub OnDataSourceViewChanged(ByVal sender As Object,
ByVal e As EventArgs)
If (MyBase.CurrentMode = FormViewMode.Insert) Then
MyBase.RequiresDataBinding = False
Else
MyBase.OnDataSourceViewChanged(sender, e)
End If
End Sub
End Class
Please take a look at this page: http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net/76885/FormView-Insert

ASP.NET: Very simple event handling not working

I have an object Order with a simple event,
Public Event ErrorOccurred(ByVal msg As String)
that I raise in the constructor like so when an order cannot be found (along w/setting a boolean error flag:
RaiseEvent ErrorOccurred("This order does not exist in the database.")
[Error] = True
I have a webform subscribed to the order's ErrorOccurred event:
Public WithEvents o As New Order
and I have an error handler method on the form:
Private Sub OnErrorOccurred(ByVal msg As String) Handles o.ErrorOccurred
litMsg.Text = "<p class=""error-confirm"">" & msg & "</p>"
End Sub
When a textbox is changed, it autoposts back to the page and employs the following logic:
Private Sub txtOrderID_TextChanged(ByVal sender As Object,_
ByVal e As System.EventArgs) Handles txtOrderID.TextChanged
If IsNumeric(txtOrderID.Text) Then
If o.OrderID = 0 Then o = New Order(txtOrderID.Text)
If Not o.Error Then
'do stuff'
Else
'error, run error handling'
End If
....
When there is an error (when the Else logic is run), everything performs as expected except the event does not fire. However, since the Error flag is set to true, it means the event MUST have fired, since that line executes AFTER the RaiseEvent line.
I've tried everything I can think of, but I cannot figure out what could be wrong. I have events strewn everywhere in my project and they all work well using virtually the same structure. What could I be doing wrong here?
I would say that since you are raising the event in the constructor, before you even have a reference to the object in your parent class, that you are unable to handle the event. In this case, especially with errors in the constructor, you would probably be much better off throwing an exception than to raise an event. I would be better to throw an exception, because some other code calling your class might not even handle the event, and you would most likely want to know that an error occured. Throwing exceptions is the standard way of letting the calling code know that an error occured. Events are more for optional things that the calling class may want to handle, but that it may also want to ignore.

Resources