jmasterx
jmasterx

Reputation: 54183

Store a value for an aspx checkbox?

I have a checkbox list filled by a list of ListItem, each ListItem having both text and a value like "8" or "5".

But I realized that a CheckBox does not have a value, its value is checked or not.

var listType = SettingsManager.Get("CRMCaseTypes");
var listStatus = SettingsManager.Get("CRMStatusReasons");

var listTypeItems = ParseSettingList(listType);
var listStatusItems = ParseSettingList(listStatus);

cblCRMType.DataSource = listTypeItems;
cblCRMType.DataBind();
cblCRMStatus.DataSource = listStatusItems;
cblCRMStatus.DataBind();

foreach (Control c in cblCRMStatus.Controls)
{
   CheckBox cb = c as CheckBox;
   if(cb != null && cb.(value........)
}

Is there some way I could store a value in each checkbox and use it again in code behind after the user clicks submit?

Thanks

Upvotes: 0

Views: 2329

Answers (7)

Dave Clinton
Dave Clinton

Reputation: 50

Although probably no longer needed by OP I'll add my answer since this thread still ranks highly in Google. If this is an ASPxCheckBox you can make use of the JSProperties dictionary to store your value(s) like so:

cb.JSProperties["cpMyValue"] = "MyValue";

I would then usually use a callback from a ASPxGridView or CallbackPanel to get this back to server side (which is slightly out of scope of the original question).

Upvotes: 0

Razvan Trifan
Razvan Trifan

Reputation: 544

You need to look at the Items collection, not the Controls collection:

    foreach (ListItem item in cblCRMStatus.Items)
    {
        string value = item.Value;
    }

Upvotes: 0

StuckOnSimpleThings
StuckOnSimpleThings

Reputation: 165

Is there some way I could store a value in each checkbox and use it again in code behind after the user clicks submit?

You can set it to a Session

Session["CbxList"] = YourCheckBoxList;

Then when you want to reference it just add the following:

if (Session["CbxList"] != null)
{
YourCheckBoxList = Session["CbxList"] as CheckBoxList;
}

and use.

I've included a link to sessions in case you or anyone else is not familiar with them: http://msdn.microsoft.com/en-us/library/ms178581(v=vs.100).aspx

Upvotes: 0

Agustin Meriles
Agustin Meriles

Reputation: 4854

Yes, you can. You can add a custom attribute to the CheckBox. You can use the HTML5 data attributes so your HTML will be HTML5 valid:

Set

foreach (Control c in cblCRMStatus.Controls)
{
   CheckBox cb = c as CheckBox;
   if(cb != null)
   {
       cb.Attributes.Add("data-MyField", myFieldVal);
   }
}

Retrieve

foreach (Control c in cblCRMStatus.Controls)
{
   CheckBox cb = c as CheckBox;
   if(cb != null && cb.Attributes["data-MyField"].ToString())
   {
       // do something
   }
}

Upvotes: 1

99823
99823

Reputation: 2457

You could add it as an attribute -

SET:

myCheckBox.Attributes.Add("myKey","myValue");

GET:

var myKey = myCheckBox.Attributes["myKey"] != null ?  myCheckBox.Attributes["myKey"].ToString() : "";

Upvotes: 0

FastGeek
FastGeek

Reputation: 411

You could pull the value from the list that the checkboxes were bound to, referencing the relevant list item based on the index of the checkbox.

Upvotes: 0

Tianyun Ling
Tianyun Ling

Reputation: 1097

How long do you want to re use it? If you want to store it temporarily, you can use session. If you want to store it longer, save it to a database or a file.

Upvotes: 0

Related Questions