How to check checkboxes according to textbox text ! - asp.net

If in textbox the default textbox value id 1,2,3,4,5,6 ...... upto 55
then the following checkboxes would be checked according to the text display in text box...
if textbox1.text =1,2,3 then in my webform checkbox1, checkbox2, checkbox3 would be checked ... on page load event...
how to do this ?

Dim splitted as String() = MyTextBox.Text.Split(",")
For Each id As String in splitted
Dim ctrl as Control = Page.FindControl("checkbox" & id)
If Not control Is Nothing Then
Dim chkbox As CheckBox = DirectCast(ctrl, CheckBox)
chkbox.Checked = True
End If
Next
I'm actually a C# programmer, so not 100% if the VB.NET syntax is correct. Another NB! is that this sample only works if the checkboxes are directly in your ASP.NET page. If they're ie. inside an ASP:Panel, then you'll have to use "MyPanel.FindControl" istead of Page.FindControl

55 checkboxes? You could have lots of if...else to check each number, but I would create a collection of checkboxes. Then parse the number in the text box check it is in range and then simply look up correct checkbox to check according using the value as an index.
Another thought: It sounds like one and only one checkbox should be set at a time? If so, you should replace them with a group of radio buttons. As well as being easier to code, it avoids duplicate checkbox checks, and signals to the user that only one can be set.

Related

VB.Net Looping over text boxes to check if they contain only numbers

Im working on a simple university project, which has to registers a new member. Right now im busy with form validation.
Some of my textboxes can only contain text and not letters, the code I use to check if the textboxes which must only accept text is as follows:
'check textboxes contains only characters'
'initial'
If Not System.Text.RegularExpressions.Regex.Match(vinital, "^[a-z]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Success Then
MsgBox("Initial can only contain text .")
End If
My question
Instead of doing one if statment at a time for every textbox, how can I loop over them using similar code as above to validate for text only.
You could try to put all your controls in a Panel and then iterate through it like this (untested, but you get the idea):
Dim ctrl As Control
For Each ctrl In Panel1.Controls 'Me.Controls should also work, I think.
If (ctrl.GetType() Is GetType(TextBox)) Then
Dim txt As TextBox = CType(ctrl, TextBox)
'check content of txt here
End If
Next

DetailsView Insert default value for checkboxes

I have a big DetailsView form for an insert to a database and I have a lot (10+) checkbox fields. Many of them aren't visible because we don't want those fields activated yet to have data submitted (but may be activated in the future).
How I can set the default value of all checkboxes at first to "false" (unchecked)? As of right now, they are being inserted as nulls and that doesn't play too well with everything else.
So far I have this code behind running as an onDataBound event for the DetailsView:
Dim row As DetailsViewRow
For Each row In DetailsView1A.Rows
If row.Cells.GetType() = GetType(CheckBox) Then
Dim tempCheckbox As CheckBox = CType(row.Cells(1).Controls(0), CheckBox)
If tempCheckbox Is DBNull.Value Then
tempCheckbox.Checked = False
End If
End If
Next
However, all the checkboxes are still being submitted as nulls. What am I doing wrong?
Try handling the ItemInserting event, and updating the "Values" collection of the DetailsViewInsertEventArgs object. These are the values that will be passed to your datasource's insert statement.
Go through and update the "Values" entry for each of these hidden CheckBox columns to False. Here's is an example:
Sub CustomerDetail_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
e.Values("yourFieldName") = False
End Sub
Note that "yourFieldName" will be the name of the column in the datasource to which the DetailsView is connected.
Also, False might need to be a string, I'm not sure how this actually gets passed:
e.Values("yourFieldName") = "False"
One other thought: if the above doesn't work (for some reason those hidden CheckBoxes don't have entries in the Values collection), I would just not hide them on the server side - hide them using CSS or JavaScript. In other words, instead of having Visible="False" on your CheckBox, use the CSS property display:none.

Casting a control to another page

I have tried everything to no avail. I need to cast the value from a user selected dropdownlist to another dropdownlist on another page. The data in the textboxes are a series of numbers.
Also is there a way to display the value in the second dropdown list as well as other values? For example, my dropdown has the values 1-6, user selects 4, which displays first in the dropdown on page 2 but the other values also display in case they want to change there selection??
If Not Page.PreviousPage Is Nothing Then
Dim table As Control = PreviousPage.Controls(0).FindControl("table1")
Dim ddl As DropDownList = CType(table.FindControl("ddlB_Codes"),c DropDownList)
If Not ddl Is Nothing Then
ddl_OC.DataSource = ddl.SelectedValue.Substring(0, 6)
End If
End If
Quick update I FINALLY got my casting to work through a session, only now I need to know how to display the session values and the other additional values for my drop box in case the user wants to change something? Thanks for the help
ddl_OC.DataSource = CType(Session.Item("valCodes"), String)
ddl_OC.DataBind()
ddl_SO.DataSource = CType(Session.Item("valAccts"), String)
ddl_SO.DataBind()
Use event selectedvaluechanged to set datasource for another DropDownList, when first one is changed.
Don't forget to populate it vin DataBind function usage.

Setting value of checkbox programmatically in VB.NET

I can check the value of a checkbox in a GridViewRow:
isChecked = CType(row.FindControl("chkSelect"), CheckBox).Checked
But what's baking my noodle is trying to figure out how to programmatically set a checkbox to checked.
The scenario is I have some rows in a GridView that are associated to another value in a dropdown. So, when I select the value in the dropdown, I'd like the checkboxes in the GridViewRows that are associated with that value to be already checked.
Problem: The check value is not persisted in the database. There's no field for it. The checkbox on the GridViewRows is an ASP TemplateField.
So I iterate through the rows and would like to check whichever checkboxes I need to based on whatever condition.
Hope I was sufficiently clear!
You should be able to do it like this
CType(row.FindControl("chkSelect"), CheckBox).Checked = True
or
CType(row.Cells(index).Controls(controlIndex), CheckBox).Checked = True
Also, see the following article for more information.

Insert record with EmptyDataTemplate in asp:ListView

I have an EmptyDataTemplate in my asp:ListView which I want to use to insert a new record.
I have Inserting working in the InsertItemTemplate... I thought I could copy the InsertItemTemplate into the EmptyDataTemplate, on clicking Insert this gives the error
Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert.
How can I use the EmptyDataTemplate to Insert a row? Do I need to use the OnClick of the button to access the values within the EmptyDataTemplate and do an Insert by myself?
I'm using LinqDataSource
You might have figured it by now
but if you set the InsertItemPosition to anything other than None the EmptyData Template will not be rendered i.e it will always show the insert template
you can read more here
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.emptydatatemplate.aspx
No way if want to insert data in empty data template.
It is possible to do an insert from the EmptyDataTemplate by handcrafting the insert. I am using a listview to display a static number of rows based on a unique filtered item. I am basically listing all the static attributes of an object. In the case where a new object is filtered on that does not have any attributes associated with it, i use the EmptyDataTemplate of the listview to display a HTMLTable that contains asp.net controls to capture data. I have a command button within the table that i evaluate using the ListView_ItemCommand. If the CommandName matches that of the "Insert" button within the EmptyDataItem, I use the ListView.Controls(0).FindControl method to locate my table. I then loop through my table and do inserts on the data found within each row. I included the how to find a control within the htmltable. In my code I am actually grabbing a bunch of controls then crafting the sql and using a SQLConnection to perform the insert.
Protected Sub ListView_ItemCommand(sender As Object, e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView.ItemCommand
Select Case e.CommandName
Case "Submit"
Dim edt As HtmlTable = ListView.Controls(0).FindControl("myhtmltable")
Dim ddl As DropDownList = CType(edt.FindControl("mydropdownlist"), DropDownList)
'Perform Insert
Case "Some other commandname"
End Select
End Sub
You will need to still do error checking and databind() and refresh your listview.
Is this the best way. Maybe not... But it is possible.
~Ian

Resources