Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binding a checkbox item? 1

Status
Not open for further replies.

CGreenMTU

Programmer
May 27, 2004
61
US
i'm binding mostly textboxes, and dropdown lists & checkboxes to web forms. I have all of the textboxes binded, however, I'm having trouble binding the checkboxes. It will not let me bind them the same way i am binding my textboxes. On my web forms I am displaying records from a Microsoft Access database, and I want to have the checkboxes unchecked or checked according to the records in the database.

Here is how I have my textboxes binded, and if anyone can help me bind my checkboxes, i would greatly appreciate it...


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
daContacts.Fill(DsContacts1)
txtCompany.DataBind()
txtID.DataBind()
txtSal.DataBind()
txtUpdated.DataBind()
txtFirst.DataBind()
txtMiddle.DataBind()
txtLast.DataBind()
txtSuffix.DataBind()
txtTitle.DataBind()
txtAdd1.DataBind()
txtAdd2.DataBind()
txtCity.DataBind()
txtState.DataBind()
txtZip.DataBind()
txtPhone.DataBind()
txtExt.DataBind()
txt800.DataBind()
txtFax.DataBind()
txtEmail.DataBind()
txtWeb.DataBind()
txtComments.DataBind()
End If
End Sub
 
you could bind this way:
Dim Row as DataRow

Row = DsContacts1.Tables("[TableName]").Rows(0)
chkBox.checked = Row.Item("[FieldName]")


Jason Meckley
Database Analyst
WITF
 
that code seems to give me the same result as:

chkbox.Databind()

however, as i navigate thru records, the check boxes won't check, but the words "True" and "False" show up next to each checkbox on the running web form. I'm assuming TRUE refers to it being checked in the database, and FALSE, it not being checked. How can I fix this?
 
it sounds like it's binding the text 'True'/'False' to the check box instead of assigning the value True/False to the check status.

Jason Meckley
Database Analyst
WITF
 
i check the box using the code I posted above.

Dim Row as DataRow

Row = DsContacts1.Tables("MyTable").Rows(0)
MyCheckBox.Checked = Row.Item("MyField")


Jason Meckley
Database Analyst
WITF
 
any other suggestions anyone ???

however, as i navigate thru records, the check boxes won't check, but the words "True" and "False" show up next to each checkbox on the running web form. I'm assuming TRUE refers to it being checked in the database, and FALSE, it not being checked. How can I fix this?

i guess i should mention i'm using navigation buttons to go thru records on the web forms from the access database
 
in the properties of your checkbox look for "databindings..." click the button to the rigth and a dialog box will appear. There select the "Text" property and in the binding box select "Not Specified". Finally, select the "Checked" property and in the binding box select the field you want to bind it to. Note that if you have your checkbox inside a container (i.e.: DataGrid, DataRepeater, ...) you should select container->DataItem->YourField in this last box.

Hope this help
 
maybe you can help me some more...
i'm also trying to bind a few listboxes and drop-down boxes also and am having no luck figuring that out.

thanks.....
 
Ok, with the drop down lists we are talking about a fairly different matter. In this case there are a number of issues we must manage.

First, we need two tables in our dataset. The first one is the table displayed in the datagrid. The second one (to which the first one is related) to fill the drop down list.

Second, I assume you want the drop down list to display the "translated foreign key". To do that you most add the foreign key as a normal column or columns to the datagrid and make them invisible. Then add a new template column and add the drop down list to it (in the edit template column option). Here you can add a disabled DropDownList to the itemTemplate and another enabled one to the editItemTemplate. After that, set the DataSource property to your dataset, the DataMember property to the second table in the dataset, the DataTextField property to the field you want to display (i.e.:name, description, etc) and the DataValueField to the row's key value.

Third, you have to handle the DataGrid.ItemDataBound event to select the appropriate option in the drop down list. your code should be something like this:

If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
Dim foreignKeyValue As String
foreignKeyValue = e.Item.Cells(3).Text
Dim dropDownList As DropDownList
dropDownList = CType(e.Item.FindControl("THE NAME OF YOUR itemTemplate DROP DOWN LIST"), DropDownList)
dropDownList.SelectedIndex = _
dropDownList.Items.IndexOf( _
dropDownList.Items.FindByValue(foreignKeyValue))
End If

This will make sure the correct value is shown by the drop down list

Finally, you have to handle the DataGrid.EditCommand event like this:

DataGrid1.EditItemIndex = e.Item.ItemIndex
FirstTableDataAdapter.Fill(MyDataSet)
SecondTableDataAdapter.Fill(MyDataSet)
DataGrid1.DataBind()
Dim dropDownList As DropDownList
dropDownList = CType(DataGrid1.Items
(DataGrid1.EditItemIndex). _
FindControl("THE NAME OF YOUR editItemTemplate DROP DOWN LIST"), DropDownList)
dropDownList.SelectedIndex = _
ddl.Items.IndexOf( _
ddl.Items.FindByValue(e.Item.Cells(THE INDEX OF YOUR FOREIGN KEY COLUMN'S CELL IN THE DATA GRID).Text))

I know there must be a way to do all this without all the coding but I have been unable to overcome some things like keeping the right selection in the drop down list when entering the edit mode.

Let me know if I didn´t explain clear enough. I'm not that good as an english speaker.
 
that helps a little bit, but is a little confusing. i'm kind of a asp.net newbie. i want to bind the drop-down box. The other fields from my database are displayed in textboxes and checkboxes on the web form. would the code be differently to perform this function?
 
you could go to this site, sign up as a bronze member (free) and download "3420 - Creating a Master / Detail Application". I'm sure it will help you a lot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top