Brian
asked on
Retrieve CheckBoxList value to CheckBoxList Control
Hello Experts,
I have a form that uses the CheckBoxList Control. The data that gets displayed for the CheckBoxList control is populated from a table in my database.
Then a user will click on each value they see fit on the CheckBoxList Control and those values they select get stored in another table in my database.
The problem that I have is that I don't know how to retrieve the values that the user selected which are stored in my database back to my CheckBoxList Control for me to view.
Below is what I'm using now to retrieve all my data from multiple tables back to the form based on the employee's unique ID in the database. Everything works fine except retrieving the values back to the CheckBoxList control to visually see what they selected. I checked my stored procedure out and it works fine.
You will see at the bottom of my CodeBehind that I try to retrieve the values to the CheckBoxList control which is labeled as "cblCurrentlyReceiveValues ".
I have a form that uses the CheckBoxList Control. The data that gets displayed for the CheckBoxList control is populated from a table in my database.
Then a user will click on each value they see fit on the CheckBoxList Control and those values they select get stored in another table in my database.
The problem that I have is that I don't know how to retrieve the values that the user selected which are stored in my database back to my CheckBoxList Control for me to view.
Below is what I'm using now to retrieve all my data from multiple tables back to the form based on the employee's unique ID in the database. Everything works fine except retrieving the values back to the CheckBoxList control to visually see what they selected. I checked my stored procedure out and it works fine.
protected void Page_Load(object sender, EventArgs e)
{
RetrieveCandidateByID();
}
protected void RetrieveCandidateByID()
{
int cid = Convert.ToInt32(Request.QueryString["c_id"].ToString());
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TMF"].ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "RetrieveCandidateByID";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.Parameters.Add("@c_id", SqlDbType.Int).Value = cid;
DataTable dtExtenuating = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter();
try
{
conn.Open();
adp.SelectCommand = cmd;
adp.Fill(dtExtenuating);
if ((dtExtenuating != null))
{
DataRow data = dtExtenuating.Rows[0];
txtFirstName.Text = data["c_fname"].ToString();
txtLastName.Text = data["c_lname"].ToString();
txtAddress.Text = data["c_address"].ToString();
ddlState.SelectedValue = data["state_id"].ToString();
ddlCity.SelectedValue = data["city_id"].ToString();
ddlZipCode.SelectedValue = data["zip_id"].ToString();
txtPhone.Text = data["c_phone"].ToString();
txtEmail.Text = data["c_email"].ToString();
ddlSchool.SelectedValue = data["sch_id"].ToString();
ddlGrade.SelectedValue = data["grade_id"].ToString();
ddlAge.SelectedValue = data["age_id"].ToString();
txtPurposeApp.Text = data["c_purpose_desc"].ToString();
txtAmountRequested.Text = data["c_amount_requested"].ToString();
ddlHearAboutUs.SelectedValue = data["hau_id"].ToString();
txtCurrentlyReceiveOtherInfo.Text = data["c_crname"].ToString();
txtExtenuatingDesc.Text = data["c_extenuating_desc"].ToString();
txtParentGuardianFirstLastName.Text = data["c_parentguardian_name"].ToString();
foreach (ListItem item in cblCurrentlyReceiveValues.Items)
{
cblCurrentlyReceiveValues.SelectedItem.Value = data["cr_id"].ToString();
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
You will see at the bottom of my CodeBehind that I try to retrieve the values to the CheckBoxList control which is labeled as "cblCurrentlyReceiveValues
You need to have CheckboxList EnableviewState Property set to true. This should preserve the values.
ASKER
even during a Page_Load call based on a users ID from another page?
ASKER
@unknown_routine,
Also, do you know what I need to include in the CodeBehind that I provided with the EnableViewState property set to true which I just now added and it did nothing different. The problem obviously lies in my CodeBehind.
Also, do you know what I need to include in the CodeBehind that I provided with the EnableViewState property set to true which I just now added and it did nothing different. The problem obviously lies in my CodeBehind.
There is probelm in your code behind:
In above you are not using the iterator object which is item(cblCurrentlyReceiveVa lues.Selec tedItem.Va lue won't change in iterations)
You need to change it to:
foreach (ListItem item in cblCurrentlyReceiveValues.Items)
{
cblCurrentlyReceiveValues.SelectedItem.Value = data["cr_id"].ToString();
}
In above you are not using the iterator object which is item(cblCurrentlyReceiveVa
You need to change it to:
foreach (ListItem item in cblCurrentlyReceiveValues.Items)
{
item.Value=data["cr_id"].ToString();
}
ASKER
@unknown_routine,
That did not work. I did not receive any errors, nothing is getting selected on the CheckBoxList Control from the values in the database.
That did not work. I did not receive any errors, nothing is getting selected on the CheckBoxList Control from the values in the database.
what do you expect to see. The foreach above only sets the values.
If you put a breakpoint what do you see as value of
data["cr_id"] ? is it a bool?
If so change the code to :
foreach (ListItem item in cblCurrentlyReceiveValues. Items)
{
item.selected=(bool)data[" cr_id"]
}
This should work. if not, let me know what is value of
data["cr_id"].ToString();
in the above code.
If you put a breakpoint what do you see as value of
data["cr_id"] ? is it a bool?
If so change the code to :
foreach (ListItem item in cblCurrentlyReceiveValues.
{
item.selected=(bool)data["
}
This should work. if not, let me know what is value of
data["cr_id"].ToString();
in the above code.
ASKER
@unknown_routine,
I need to retrieve values from a table in my database to a CheckBoxList Control based on the value "cr_id" which is an "int" value.
So for example, I have and employee in which his primary ID in the table is called "cr_id" and the "cr_id" value for example is 10034. So I need to retrieve all the entries that he initially submitted using the CheckBoxList control to be shown on the CheckBoxList Control during Page_Load based on the value of "cr_id" getting passed in.
So I should be able to retrieve all of the CheckBoxList values that cr_id 10034 has in the database and I would like to have the CheckBoxList Control display those by means of displaying the check on the CheckBoxList control.
I need to retrieve values from a table in my database to a CheckBoxList Control based on the value "cr_id" which is an "int" value.
So for example, I have and employee in which his primary ID in the table is called "cr_id" and the "cr_id" value for example is 10034. So I need to retrieve all the entries that he initially submitted using the CheckBoxList control to be shown on the CheckBoxList Control during Page_Load based on the value of "cr_id" getting passed in.
So I should be able to retrieve all of the CheckBoxList values that cr_id 10034 has in the database and I would like to have the CheckBoxList Control display those by means of displaying the check on the CheckBoxList control.
ASKER
@unknown_routine,
I found the following code below that will display one of my values in the database to the CheckBoxList Control but it does not display all of them. It only displays the first value in the CheckBoxList Control and not the others.
I found the following code below that will display one of my values in the database to the CheckBoxList Control but it does not display all of them. It only displays the first value in the CheckBoxList Control and not the others.
CheckBoxList chkbx = (CheckBoxList)form1.FindControl("cblCurrentlyReceiveValues");
ListItem currentCheckBox = chkbx.Items.FindByValue(data["cr_id"].ToString());
if (currentCheckBox != null)
{
currentCheckBox.Selected = true;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I figured out the issue myself. I'm very disappointed in the lack of support that I received from EE.