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

Update a record based on checked/unchecked value in a checkboxlist 1

Status
Not open for further replies.

prover

Programmer
Sep 12, 2001
54
US
I have a form with preselected items in a checkboxlist control. What i'd like to do is either add a record if a checkbox was checked. Delete a record if the checkbox was unchecked.

A user checked the following items and recorded them to a DB.

Checkboxlist Items:

car
truck (checked)
boat
motorcycle (checked)
bike (checked)

now if the use checked "boat" i want add it to the DB and if they unchecked "bike" i want to delete it. Should i turn on autopostback and try to get the value of the checked item and make a call to the DB or is this just the wrong appoach.

TIA


 
it can be done the way you selected. however this leads to many postbacks which is interperted as a poor user exprience. a good rule of thumb with web applications.
the fewer postbacks the better.

instead of posting back on every change have the user check/uncheck the values. then preform the save action when the user clicks a "save" or "next" button.

if users are selecting simple lookup values which are insterted into an M:N joining table the easiest solution is to delete all existing records and insert the selected ones.

if your data looks like this
Code:
Table1
-------
Id pk
name
address
phone
...

Table2
-------
Id pk
Description

Table2
---------
Table1Id pk, fk to Table1.Id
Table2Id pk, fk to Table2.Id
then your code would look like this
Code:
protected void Button_Click(object sender, EventArgs e)
{
   if (Page.IsValid)
   {
       int table1Id = //get value;
       //compile other data to save

       List<int> table2Ids = new list<int>();
       foreach(ListItem item in MyCheckBoxList.Items)
       {
           if(item.Selected)
           {
              table2Ids .Add(int.Parse(item.Value.ToString();
           }
       }
       //begin transaction
       try
       {
           //save other data
           //delete from Table3 where Table1Id = @table1Id

           foreach(int i in table2Ids)
           {
              //insert into Table3 values (@table1Id, @i)
           }
           //commit transaction
       }
       catch
       {
           //rollback transaction
       }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
After my post i realized (imediately) that my first idea was (lets face it) DUMB! I left the post anyway figuring someone could help with other ideas. I thought of deleting all records then adding the new values but that seemed to simple and heck simple things escape me :)

Thank you!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top