Reputation: 282
I have an issue I wonder if this fine community can help me out.
I have a webform asp.net (using vb.net code behind) web application, on one of my forms has a datagrid. Each record can have a different status and each record can be selected via a checkbox. At the bottom of the grid I have a dropdown of possible actions that users can take, these actions will be against the records selected and a button to invoke the action selected.
Now the issue I have and I am getting rather annoyed at it is that due to the difference in status before I complete the action selected i need to check whether the records selected can do such action.
Example
I select records 1,2,3,4,5. The action I select is to download in a CSV format, however only records which are unlocked can be downloaded, so in my example only records 1,3,5 are unlocked. This means I can only download 1,3,5 and not records 2,4. Once downloaded I want to mark the record that it has been downloaded and refresh the grid.
I have this completed this by finding the records to be downloaded (aka 1,3,5), create a CSV (or XML as that is also one of the actions) as a string, mark the records 1,3,5 as downloaded, refresh the grid and then I invoke a hidden button on the form which will actually download the data.
If Not ViewState("Data") Is Nothing Then
Response.Clear()
Response.ClearHeaders()
Response.Buffer = True
Response.ContentEncoding = Encoding.UTF8
Response.ContentType = IIf(ViewState("ExportType").ToString() = SharedApplication.ExportedType.XML, "application/xml", "text/csv")
Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", "records-" & "." & ViewState("ExportType").ToString()))
Response.Write(ViewState("Data"))
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.SuppressContent = True
HttpContext.Current.ApplicationInstance.CompleteRequest()
ViewState("Data") = Nothing
End If
The above downloads the data, to invoke the download button I use javascript to do a postback on the click event of the hidden button.
However the issue I have is before I download the data I want to add a confirmation box, something to say "Only 3 of the 5 records will be downloaded, do you want to continue". With yes continuing download and No to stop action. How do I go about doing this. I have tried all sorts of techniques but cannot figure it out.
I have tried adding a return confirmation on the hidden button in its onlcientclik event, but this never gets fired. It does work if you actually click on the button but not when I do a postback to its click via JS.
The closet i can find is using the registeronsubmit in the code behind.
ClientScript.RegisterOnSubmitStatement(btnHidden.GetType, "confirm", "return confirm('Are you sure?');")
But this gets fired constantly, even when we I click on another button. I have tried adding another hidden button with the hope I can add the clientclick to show a confirmation box but that does not work as well.
Does anyone have an idea, I have herd of possibly creating a popup to display the messagebox but then how do I capture if my code needs to continue.
Upvotes: 0
Views: 387
Reputation: 282
Hi I thought I better share what I eventually did to complete my issue incase someone else moves into this territory.
So I went away and thought about this more and the solution I came up and actually works pretty well although some may say this is rather an ugly way of doing this.
I use a dropdown list to mark what action is needed, so I thought why don't we use the postback event on the dropdownlist to build up the message of the confirmation box and then add the JS confirmation box on the OnClientClick event of the button.
btnRun.OnClientClick = "return confirm('" & sMessage & "');"
The message variable is a string which builds up the message depending on which process action is selected.
This means as soon as the dropdownlist is changed to an action, the codes will work out how many selected items will be changed. This is a sql query which picks up which codes/ids will be changed, then the count of data against the total count of selected items will be changed/exported whatever the action selected. This builds the process button confirmation, so when the user clicks on the process button it displays X out of X will be changed, do you want to continue. If cancel is selected nothing happens if ok is selected it carries on and completes it action as per usual before I wanted to show some message box.
Upvotes: 1