tags:

views:

369

answers:

3

I have a list of msgs (it could be 24 or 100+). I would need to remove many of these messages. I was wondering, what is the best way? I dont like the idea of building names for 100+ elements and doing something like doRemove = Request["msgId" + i];

I prefer receive a array such as long[] and do something like

long[] removeThese = Request["remove"];
foreach ....
    //DELETE msgId WHERE userId=@userId

However i dont know if creating an array and POSTing it is possible with HTML (and basic javascript?) or if its the best solution. How should i do this?

+1  A: 

You could create checkbox with same name

<input...    name="mycheckbox" value="hello"></input>
<input...    name="mycheckbox" value="world"></input>

On the server side, you could use Request("mycheckbox") & this should be an array of selected Items.

Alternatively, you could use asp.net checkboxlist.
link - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist(VS.71).aspx

shahkalpesh
A: 

If you're looking for a "lightweight" solution, you could always have a hidden HTML variable and assign a javascript event to each checkbox that adds its index/id to the variable, creating a comma delimited list of Ids to process.

Then when the submit/confirm occurs, you can have your code behind page grab this string, convert it into a List/Array/Whatever that works best for your processing and finish things up.

Dillie-O
+1  A: 

I would slap a <asp:CheckBoxList /> on there and when you need to submit changes, just take the .SelectedValue and pass it through into your SQL parameter (wherever that is done).

If you are on SQL 2008, you can take advantage of "Table-Valued Paramaters" (just google it) which you just pass any IEnumerable (pretty much any collection) in the variable and you can just JOIN to it in your UPDATE/DELETE/etc query.

Previous versions of SQL, the approach I use is very similar except just the string/VARCHAR is passed in to the query and you have to create a table variable or temp table to hold the values inserted from a split procedure. There are many ways to create the split procedure, but I've found the numbers-table approach works in all versions of SQL and has pretty good performance. See http://www.sommarskog.se/arrays-in-sql-2005.html for exhaustive reference of the possible performance implications of each approach.

Just say no to ' IN (' + @MyValuesCSV + ')' :-)

luna