Setting value of checkbox programmatically in VB.NET - asp.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.

Related

How to have 2 or more DropDownlist in 1 row in Gridview

I have a List Product. The product list is just like in the picture.
If I display directly from the list to the Gridview.
It will be like in the picture: The rows will hv other Product rows if product type varies.
Here's a link!.
How can I achieve this? I tried looking up on Rowspan, but unsure how I can retrieve back the Dropdownlist record.
Here's a link!.
I tried searching but none found so far. The nearest I found was just labels.
Here is a link
for the implementation.
Hope it helps!!
I am not sure if you are trying to access the dropdown value on button click. If this is the scenario try using the below code.
var tmpValue = ((DropDownList)gvRowItem.Cells[/*cellId*/].FindControl("/*dropDownListId*/")).SelectedValue;
If you need to populate the value on selection of dropdown within the grid, you can add the postback event and handle the same as below
Control ctrl = gridview.Rows[/*rowId*/].FindControl("/*dropDownListId*/") as DropDownList;
DropDownList ddl = (DropDownList)ctrl;
var selectedValue = ddl.SelectedValue;
Let me know in case you are expecting something else

Set default value of dropdownlist based on datasource

I have a webform that uses a dropdownlist to populate a gridview. The dropdown list is bound to a datasource. I would like to have the default value of the dropdown list be a value in the listing. I have a series of 5 values, one of which is actually a blank. The default value I am looking for would be index 2 or the value "Agency Error"
Here is the query I have for the datasource:
SELECT DISTINCT [AnnoType] FROM [AnnoType]
Is there a way to do this?
I am just trying to eliminate one of maybe 50 clicks the user needs to do in the process.
Use SelectedIndex property
dropdownlist.SelectedIndex = 2;
or SelectedValue property
dropdownlist.SelectedValue = dropdownlist.Items.FindByText("Agency Error").Value;

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.

How to check checkboxes according to textbox text !

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.

Resources