views:

934

answers:

4

i'm pretty sure it's not possible, but i'll throw this out there anyways.

I have about 10 asp:checkbox controls on the page, and I need to go and update the database every time any of them gets checked/unchecked.

Right now i have all of them tied to one event handler that fires on CheckedChanged, then i cast the sender to Checkbox and get the ID, and assign a value based on that ID to a parameter that gets passes to the stored procedure.

Is it possible to assign a custom parameter to each checkbox, so i don't have to tie their IDs to sproc parameter values.

Thank you.

p.s. static Dictionary<> seems to be the way to go, however i can't get examples from this post to work in .net 2.0

A: 

You could inherit from it and add the property you need.

Or use a Dictionary to map control IDs to your database IDs.

EDIT: A dictionary holds a list of key/value pairs. The control's ID could be the key, and the database-relevant "custom parameter" you want (I'm not 100% sure what you're talking about but the Dictionary can store it) could be the value. When you want to get the value, you get it with:

myDictionary[keyValue]

Declare Like:

Dictionary<string, string> myDictionary = new Dictionary<string, string>();

EDIT 2: For a static Dictionary:

public static readonly IDictionary<string, string> myDictionary = new Dictionary<string, string>();

static ClassConstructor()
{
    myDictionary.Add("key1", "value1");
    myDictionary.Add("key2", "value2");
    myDictionary.Add("key3", "value3");
}
colithium
can you elaborate on Dictionary?
roman m
i want to use static Dictionary<>, can't get it to work though. see my p.s. in the post
roman m
A: 

You could try setting an attribute on each checkbox via the Attribute collection, which you should then be able to get the value of after you have cast the sender in your response method.

Antony Scott
Not a good solution... Attributes are not automatically persisted in the ViewState, so you still have to map the controls to their proc parameter every page load. So there's no point in adding it to Attributes.
Bryan
true, i've only ever used that technique when I've been using a custom Ajax library.
Antony Scott
+1  A: 

It might be an idea to try using regular HTML checkboxes and submitting them as an array / comma-separated list:

<input type="checkbox" id="item-1" name="items[]" value="1" />
<label for="item1">Item 1</label>
....
<input type="checkbox" id="item-n" name="items[]" value="n" />
<label for="item1">Item n</label>

Then on the server side you could do something like:

string tmp = Request.Form["items[]"];
if (!string.IsNullOrEmpty(tmp)) {
    string [] items = tmp.Split(new char[]{','});
    // rest of processing, etc.
}

Hopefully this will reduce the amount of work you have to do server side.

Ian Oxley
+1  A: 

Why not make your own custom server checkbox control.

namespace CustomControls
{
    public class CustomCheckBox : CheckBox
    {
       string _myValue;
       public string MyValue
       {
           get { return _myValue; }
           set { _myValue = value; }
       }

       public CustomCheckBox()
       {
       }
    }
}

<%@ Register TagPrefix="MyControls" Namespace="CustomControls"%>
<MyControls:CustomCheckBox id="chkBox" runat="server" MyValue="value"></MyControls:CustomTextBox>
Phaedrus