Array of Checkboxes or Checkboxes in a PlaceHolder
Hi Folks,
This is my first post here. If this is not the correct forum to post, do accept my apologies.
I am working on creating checkboxes dynamically. I have a stored procedure that reads a list of interests from a table and these have to be displayed as checkboxes.
I do this using a place holder and what I get is a series of checkboxes with column name next to them, but I want the values in that column and not column name.
for(int i = 0; i < myDS.Tables["Interests"].Rows.Count; i++)
{
CheckBox objCheckBox = new CheckBox();
objCheckBox.ID = myDS.Tables["Interests"].Columns["InterestTypeID"].ToString();
objCheckBox.Text = myDS.Tables["Interests"].Columns["InterestType"].ToString();
phlInterests.Controls.Add(objCheckBox);
}
If I create an array of checkboxes, I keep running into an error Object reference not set to an instance of an object.
Could someone point me in the right direction.
Thanks,
Priya
pria525Posted Oct 6, 2004, 2:58 PM
jsheplerPosted Oct 6, 2004, 2:42 PM
cblInterests.DataValueField = "InterestTypeID";jsheplerPosted Oct 6, 2004, 2:41 PM
private void createInterests() { DataSet myDS = new DataSet(); myDS = mytFinder.getTrailInterests(); cblInterests.DataSource = myDS.Tables["Interests"].DefaultView; cblInterests.DataTextField = "InterestType"; cblInterests.DataValueField = "Interests"; cblInterests.DataBind(); }pria525Posted Oct 6, 2004, 2:33 PM
jsheplerPosted Oct 6, 2004, 1:57 PM
pria525Posted Oct 6, 2004, 1:46 PM
jsheplerPosted Oct 5, 2004, 5:10 PM
void Page_Load (object sender, EventArgs e) { if (!IsPostBack) { cblInterests.DataSource = msDS.Tables["Interests"].DefaultView; cblInterests.DataTextField = "InterestType"; cblInterests.DataValueField = "InterestTypeID"; cblInterests.DataBind(); } }This will generate a checkbox for each row in your dataset. The values are pulled from InterestTypeID and the text (label) is pulled from InterestType. The biggest reason for doing it this way instead of manually adding the items is getting the selected value(s) later. Dynamically adding the controls the way you were doing does not keep them in the VIEWSTATE and hence will not be available on the form's postback. In other words, when your user selects an item and clicks the submit button, your code will no longer know anything about the checkboxes you put inside phlInterests - in fact, they won't even exist anymore. If you absolutely must add the checkboxes manually like you posted, there are pretty ugly ways of dealing with it that I could show you. Using the CheckBoxList control avoids all that mess. It will be added to the VIEWSTATE, so it will be available on postbacks. You can access the selected values by looping through the contol's Items property and checking the Selected property of each item:foreach(ListItem item in cblInterests.Items) { if(item.Selected == true) { // code that does whatever you need with the selected item(s) // item.Value would be the InterestTypeID as a string // item.Text would be the InterestType as a string } }shimtannyPosted Oct 5, 2004, 1:18 PM