I have a HTML multiple select being used which caused no issue until a selection with a comma was entered. When saving / loading the values are split by ',' into a list. Therefore causing an issue
I have tried to find a way of possibly changing the character that is being used to split the values when the form is posted but came to a dead end.
Would be very grateful if someone has any insight into this.
Thanks in advance.
---Update with Code---
The control is created dynamically
Dim SelectName1 As New HtmlSelect
SelectName1.ID = "SelectName" & id
SelectName1.Name = "SelectName"
SelectName1.Multiple = True
and filled by looping though the values.
For Each value As String In Request.Form(idToFind).Split(",")
If Not IsDBNull(SelectName.Items.FindByValue(value)) Then....
I cannot add any more code than that, Apologies
After updating the question by add code, I think the available options are limited:
Create an extra Javascript method, that stores the selected values in a json object and store it into a hidden input field. I'm not an expert in asp.net, though I'm sure there exists a method to parse json objects from a string.
Create an extra javascript method, that concatinating all values to a String and store it into a hidden input field. With creation of a single string, you can define your own delimiter.
After that, add this extra Javascript mehod to the event onSubmit. Then submit the form and read the value from hidden input field.
P.S. In my eyes the second idea is more simple to create, than the first one. And maybe there are better ways.
Related
I have a HTML table where I am allowing dynamic adding/deleting rows (that contain text-input's in the cells) using some JavaScript. I am not using ASP.NET TextBox controls, just traditional HTML, as I don't think rows containing ASP.NET controls can be added/deleted without a postback.
When the user clicks an ASP:Button, I am using VB to loop through the table rows in server-code, and ultimately use LINQ to write to a database. I have "Imports System.Web.UI.HtmlControls" in the file.
The code where I am getting problems is:
Dim name As HtmlInputText
name = row.Cells(0).Controls(0)
The error is: "Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.HtmlControls.HtmlInputText'." On the 2nd line above.
There is only one input control in each cell, so I am assuming I can use "Controls(0)" to access it. I have read solutions that use "FindControl", but I don't think this works with standard HTML "inputs", but also, as the rows are dynamically added/deleted, it's near-impossible to know the "ID" to search for.
Any ideas? Cheers.
Figured out the problem. Just needed to add runat="server" to the input tags. Thanks every one for the replies.
I am creating a program in VB.NET which uses an online MySQL database to retrieve certain data. I have now succeeded in connecting and getting some basic stuff out of it. Now, what I want to do is that when an user presses a button it has to update the list. What happens is that the stuff that is already in the database also gets resent and so the list just doubles himself, although it adds the new database value.
How can I make sure that it only adds new values to the list, instead of adding all values from the list again? I have read that you can use the Preserve keyword with arrays, though this isn't an array and neither have I figured out how to convert my data into an array.
Private Sub generateList()
DB.writeQuery("SELECT snacks.ID, snacks.naam, snacks.baktijd FROM snacks")
While DB.DR.Read
ListBox1.Items.Add(DB.DR.Item("naam"))
End While
DB.closeConnection()
End Sub
This is the piece of code I use to generate the list, I reuse this code to refresh the list. As you can see, it uses my own written MySQL class. I know it makes an connection so there is nothing wrong with that.
Any help would be appreciated.
What you will want to do is to clear the ListBox before you reload the list. You can do this like so:
ListBox1.Items.Clear()
Then when you reload the list, the items will not be duplicated.
Looks like you need to clear your listbox before adding the items.
ListBox1.Items.Clear()
Try this my friend:
If Not listBox1.Items.Contains(DB.DR.Item("naam")) Then
ListBox1.Items.Add(DB.DR.Item("naam"))
else
'this item already exists.
end if
I have created Wordpress custom fields through functions.php. My code works fine, and I've few input fields and some checkboxes.
Problem is when I save post, even if I don't put content inside my form, these rows are created in DB. I'd like to do some kind of php check and avoid creation of row in DB if field content is not saved.
I tried several ways, but in most cases it would result in incorrect behaviors of checkboxes for example.
Full code is here: http://pastebin.com/embed_js.php?i=Vvnseiep
I'd appreciate your help in this matter. I'm not very experienced.
Thanks!
Here Validation part comes into play. Why cant you use Javascript to validate your input in the client environment itself and then allowing it to hit the db.
Name all of the checkboxes the same(name="meta_box_check[]"). They must still have different ID's. When the form is posted you will be given an array of values that were checked in $_POST['meta_box_check']. You can then check if the array is empty. You can also save the checkbox data as JSON data. This isn't always good practice, but will only use one data row to save any and all checkbox values.
<?php
if(!empty($_POST['meta_box_check']))
{
//process your data and save it
update_post_meta($post_id, 'meta_features_checklist', json_encode($_POST['meta_box_check']));
}
?>
This is a basic example and make sure you do some data validating before saving.
Ok so I hope the title of the question matches what I about to ask, but here is what I am trying to get at:
So I have an access database that uses a number of unbound forms, and the purpose of the forms are to collect data and save to various tables with VBA click events using SQL statements (INSERT or UPDATE based on whether the ID of the record is present on the form in a hidden text box). When entering a new record (via INSERT), I get the row number with
MyRow = db.openrecordset("SELECT ##Identity")(0) 'thanks David
So you maybe getting the picture. If I have another form that relates to the first form in terms of the record, I just open a recordset and pass that value to another hidden text box.
So my question is, is there a better way to do this regarding passing that value (or just using that value) using a variable instead of this awkward method. So I realize a lot of folks are going to go with the obvious answer of, "Why not just make your forms bound instead of all this code"...and I am sure that is a valid answer, however I inherited this database which was already put together like this, and re-structuring it would be a daunting task.
Any and all advice, or learning resources are greatly appreciated, as they always are!
I use unbound controls on forms for all these kinds of values. The current solution of using an unbound form is sounder than using global or form level variables. If I recall the details correctly while debugging code and you hit the stop button you lose all global or form level variables. Or if the user hits an unhandled error.
Have you looked at OpenArgs?
DoCmd.OpenForm "Form1", , , , , , "Hello"
So how can I pass a value from one form to another? For example: The user select's an organization from a list and this opens up a trip form that allows a user to enter various information regarding the trip. At one place I would like to add another little pop up form where they can enter contact information (just a name and phone for POC) of the organization they are visiting.
So when that initial form opened from the selection screen it has two IDs that are simply hidden in text boxes (one being the tripID, and the other being the OrgID), so how do I pass these to the second little pop up form so that the contact information has the relative IDs with it.
Thanks.
The best approach in these cases is not to attempted to pass a bunch of variables. It is too much code, and is inflexible. For example, if you need to pass two values, what happens over the years when that requirement grows to 5 values? Trying to maintain and pass a whole whack of values is too much coding work.
Keep in mind that each form in ms-access is really a class object that you can manipulate in code. So, use a object approach here and you find you not only write less code, but your code will be more clean, more modular, no need for global vars, and code you write can often be re-used between different forms.
Here is how:
In general when one form launches another form in the 2nd form in the forms on-open event (in fact, you can even use as late as the on-load event) you can pick up a reference to the PREVIOUS form object. In other words, you can use a object approach here.
At the forms module level, for form I declare a form object as:
Option Compare Database
Option Explicit
dim frmPrevious as form
Then, in the forms on-load event, we go:
Set frmPrevious = Screen.ActiveForm
Now, any code in our form can FREELY use code, events, even varibles declared as public from that previous form in code.
So, if you want to force a disk write of the previous form, and re-load of data.
frmPrevious.Refresh
If you want to set the ID value, then go:
frmPrevious!ID = some value
And, note that you can even declare form previous as a PUBLIC variable for that form, and thus if you two forms deep, you could go:
frmPrevious.frmPrevious!ID = some value
So, simply declare a forms object in EACH forms code module (or at lest the ones where you need to use values in code). The above means any code has a ready made reference to the previous form object. Functions declared as public in a form will become a METHOD of the form, and can be run like:
frmPrevious.MyCustomRefresh
or even things like some option to force the previous form to generate and setup a invoice number:
frmPrevous.SetInvoice
or
frmPrevious.SetProjectStatusOn
So not only can you shuffle values and data back and forth, but you can easily execute features and functions that you build in code for the prevous form.
In fact as a coding standard, MOST of my forms have a public function called MyRefresh.
Note that the beauty of this approach is that you can thus read + use + set values from that previous form. This allows your code to not only receive values, but also set values in that previous form. So this approach is bi-directional. You can shuffle data and values back and forth between the forms. The other advantage here is you NOT restricted to just variables, but can use fields, control values (events, properties) etc.
This approach means that much of the previous form is now at your fingertips.
So don’t try to pass a whole whack of variables. Pass a reference to the form and you have a nice ready made object at your fingertips and it makes this type of coding problem a breeze.
The usual way would be to reference the textboxes in the initial form from the popup form, like this:
Forms!frmInitialForm!tripID
Forms!frmInitialForm!OrgID
However, this tightly binds the popup form to the initial form, so that it cannot be used anywhere else in the application.
A better approach is to use OpenArgs:
DoCmd.OpenForm "frmPopup", OpenArgs:=Me.tripID & ", " & me.OrgID
This places your two values into a string, which is passed to the popup form. You can then parse the two values out of the OpenArgs using the Split function.
For more info about passing parameters using OpenArgs, see:
http://www.fmsinc.com/free/NewTips/Access/accesstip13.asp
This one could help
MS Access: passing parameters from one access form to another