2

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

Community
  • 1
  • 1
roman m
  • 26,012
  • 31
  • 101
  • 133

3 Answers3

3

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
  • 8,351
  • 26
  • 28
1

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
  • 10,916
  • 6
  • 42
  • 49
0

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
  • 10,269
  • 5
  • 42
  • 57