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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CheckBoxList and FormView Insert

Status
Not open for further replies.

craigbtg

MIS
Nov 16, 2006
8
US
Hello,

I am using a formview to insert data into two tables. When I click insert the first part of my form is bound so the records are inserted automatically and I have an autonumber field generating a primary key in MS Access. I can grab the new primary key field in the formview iteminserted event, but I can't seem to figure out how to loop through my checkboxlist and insert those items into my second table.

The basic idea is table1 has PrimaryKeyField which gets generated on the iteminsert. Table2 then needs to insert PrimaryKeyField and my CheckBoxValueFields each as a separate record.

I think my problem has something to do with the checkboxlist being part of the insertitemtemplate and I am not calling it properly to retrieve the values.

Thanks in advance for your assistance.
 
I was able to loop through my checkboxlist in the iteminserted event using the following code:

Code:
        Dim FormView As FormView = CType(FormView1, FormView)
        Dim cblist As CheckBoxList = CType(FormView.Row.FindControl("checkboxlist1"), CheckBoxList)
        Dim i
        For i = 0 To cblist.Items.Count - 1
            If cblist.Items(i).Selected Then
                Response.Write(cblist.Items(i).Value.ToString + "<br />")
            End If
        Next

From here I can use the selected values and add them to my second table. This will work, but it doesn't seem like the real solution. I wonder if anyone has a better suggestion. Thanks.
 
It seems that there must be an easier way to add the selected values from the checkboxlist.

I thought it was strange that I needed to define an instance of a checkboxlist only to use the same one that is already on the formview. I also thought there may be an easier way to insert the selected values at the same time the bound form fields are inserted. So as soon as the form fields are added the checkboxlist items are also added at that time rather than using the iteminserted event.
 
If it is in a template column you have to access thorough an event of the fromview. I would use the itemdatabound event of the formview, check if the row is in insert mode and then get a referenct to the cbl like you did above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top