bojeff30
asked on
dynamically generated checkboxes?
Hello there,
Currently I am working with the project which needs dynamically generated checkboxes. I have generated dynamic checkboxes but I am facing problem with getting the value of the selected checkboxes. Could you explain how to get the selected value for a dynamically generated checkboxes?
Below is my current code:
##### dynamically generated checkboxes:
for (int i = 1; i < 4; i++)
{
CheckBoxList _checkbox = new CheckBoxList();
_checkbox.ID = "myCheckbox" + i.ToString();
_checkbox.Items.Add("first " + i.ToString());
_checkbox.Items.Add("secon d" + i.ToString());
if (i == 1)
{
PlaceHolder1.Controls.Add( _checkbox) ;
}
if (i == 2)
{
PlaceHolder2.Controls.Add( _checkbox) ;
}
if (i == 3)
{
PlaceHolder3.Controls.Add( _checkbox) ;
}
}
### ASPX page
<tr>
<td >
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:Place Holder>
</td>
<td>
<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:Place Holder>
</td>
<td >
<asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:Place Holder>
</td>
</tr>
Currently I am working with the project which needs dynamically generated checkboxes. I have generated dynamic checkboxes but I am facing problem with getting the value of the selected checkboxes. Could you explain how to get the selected value for a dynamically generated checkboxes?
Below is my current code:
##### dynamically generated checkboxes:
for (int i = 1; i < 4; i++)
{
CheckBoxList _checkbox = new CheckBoxList();
_checkbox.ID = "myCheckbox" + i.ToString();
_checkbox.Items.Add("first
_checkbox.Items.Add("secon
if (i == 1)
{
PlaceHolder1.Controls.Add(
}
if (i == 2)
{
PlaceHolder2.Controls.Add(
}
if (i == 3)
{
PlaceHolder3.Controls.Add(
}
}
### ASPX page
<tr>
<td >
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:Place
</td>
<td>
<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:Place
</td>
<td >
<asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:Place
</td>
</tr>
And here is how to get it by name
Label1.Text = "";
CheckBoxList checkBoxList = PlaceHolder1.FindControl("myCheckbox2") as CheckBoxList;
if (checkBoxList != null)
{
ListItem item = checkBoxList.Items.FindByText("first2");
if (item != null)
Label1.Text = item.Selected.ToString();
}
Hi bojeff30
I don't know exactly what is the need of Dynamic Checkbox but the way you are doing is not good. As you are saying you are adding Dynamic Checkbox to a fixed placeholder? It dosen't make sense to me could you explain what is the reason to have 3 place holder and then put one dynamic checkbox in each placeholder?
Thanks
Harry
I don't know exactly what is the need of Dynamic Checkbox but the way you are doing is not good. As you are saying you are adding Dynamic Checkbox to a fixed placeholder? It dosen't make sense to me could you explain what is the reason to have 3 place holder and then put one dynamic checkbox in each placeholder?
Thanks
Harry
i agree with harwantgrewal as to why do u need PlaceHolder control to add a single check box in all the place holders at run time
Create your check boxes at design time
Create your check boxes at design time
Hi raqi0017
Yeah thats what I was trying to understand so that we can help bojeff30 in a right way rather just answering to his problem :)
Thanks
Harry
Yeah thats what I was trying to understand so that we can help bojeff30 in a right way rather just answering to his problem :)
Thanks
Harry
ASKER
Sorry for the miscommunication. I tried to simplify the question, but I think I made it more complicated. Let me start over. My initial code is listed below.
I need to display a list of languages supported for(lets say) different movies. I need the languages to display in a checkboxlist to allow user selection. The selected languages needs to be moved to a listbox once the user click a submit button.
I have two issues.
1.) Im unable to get the selected value from the checkboxlist and move it to the list box.
2.) Im unable to get the desire table format unless I use a placeholders or a panel control. The table format must remain in the table format listed below. Let me know if there is a better way to achieve this task.
Table format
-------------------------- ---------- ---------- --
| Movie 1 | Movie 2 | Movie3 |
-------------------------- ---------- ---------- --
|Checkbo1 | checkbox2 | checkbox3 |
| x English | X English | x English |
| x French | X spanish | X gGerman |
| x Spanish | | |
-------------------------- ---------- ---------- --
my Code:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 4; i++)
{
Label _label = new Label();
_label.ID = "lbUSA" + i.ToString();
//------------------------ ---------- ---------
CheckBoxList _checkbox = new CheckBoxList();
_checkbox.ID = "cblDAO" + i.ToString();
_checkbox.DataTextField = "Language_Description";
_checkbox.DataValueField = "Language_Name";
//Connect to DataBase
string connectionString =
WebConfigurationManager.Co nnectionSt rings["MyD BTOOL"].Co nnectionSt ring;
SqlConnection myConnection = new SqlConnection(connectionSt ring);
//SqlDataReader ReadMyData = new SqlDataReader();
//Stored Procedure Command
SqlCommand command = new SqlCommand("sp_Movie_Langu age_Lookup ", myConnection);
command.CommandType = CommandType.StoredProcedur e;
myConnection.Open();
//Add Stored Procedures Parameters
command.Parameters.Add(new SqlParameter("@TypeID", SqlDbType.Int));
command.Parameters.Add(new SqlParameter("@RegionID", SqlDbType.Int));
command.Parameters.Add(new SqlParameter("@MovieID", SqlDbType.Int));
//Set Value for Stored Procedure Variable
command.Parameters["@LobID "].Value = 1;
command.Parameters["@Regio nID"].Valu e = 4;
command.Parameters["@Movie ID"].Value = i;
SqlDataReader myReader = command.ExecuteReader();
//While the data reader iterates through the rows, populate the CheckBoxList.
if (myReader.HasRows)
{
//populate Checkboxist
while (myReader.Read())
{
_label.Text = myReader["MOVIE_Name"].ToS tring();
_checkbox.DataSource = myReader;
_checkbox.DataBind();
}
}
//Housekeeping - Close the connection
myReader.Close();
myConnection.Close();
//######################## ########## ####
if (i == 1)
{
Panel1.Controls.Add(_label );
PlaceHolder1.Controls.Add( _checkbox) ;
}
if (i == 2)
{
Panel2.Controls.Add(_label );
PlaceHolder2.Controls.Add( _checkbox) ;
}
if (i == 3)
{
Panel3.Controls.Add(_label );
PlaceHolder3.Controls.Add( _checkbox) ;
}
}
}
I need to display a list of languages supported for(lets say) different movies. I need the languages to display in a checkboxlist to allow user selection. The selected languages needs to be moved to a listbox once the user click a submit button.
I have two issues.
1.) Im unable to get the selected value from the checkboxlist and move it to the list box.
2.) Im unable to get the desire table format unless I use a placeholders or a panel control. The table format must remain in the table format listed below. Let me know if there is a better way to achieve this task.
Table format
--------------------------
| Movie 1 | Movie 2 | Movie3 |
--------------------------
|Checkbo1 | checkbox2 | checkbox3 |
| x English | X English | x English |
| x French | X spanish | X gGerman |
| x Spanish | | |
--------------------------
my Code:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 4; i++)
{
Label _label = new Label();
_label.ID = "lbUSA" + i.ToString();
//------------------------
CheckBoxList _checkbox = new CheckBoxList();
_checkbox.ID = "cblDAO" + i.ToString();
_checkbox.DataTextField = "Language_Description";
_checkbox.DataValueField = "Language_Name";
//Connect to DataBase
string connectionString =
WebConfigurationManager.Co
SqlConnection myConnection = new SqlConnection(connectionSt
//SqlDataReader ReadMyData = new SqlDataReader();
//Stored Procedure Command
SqlCommand command = new SqlCommand("sp_Movie_Langu
command.CommandType = CommandType.StoredProcedur
myConnection.Open();
//Add Stored Procedures Parameters
command.Parameters.Add(new
command.Parameters.Add(new
command.Parameters.Add(new
//Set Value for Stored Procedure Variable
command.Parameters["@LobID
command.Parameters["@Regio
command.Parameters["@Movie
SqlDataReader myReader = command.ExecuteReader();
//While the data reader iterates through the rows, populate the CheckBoxList.
if (myReader.HasRows)
{
//populate Checkboxist
while (myReader.Read())
{
_label.Text = myReader["MOVIE_Name"].ToS
_checkbox.DataSource = myReader;
_checkbox.DataBind();
}
}
//Housekeeping - Close the connection
myReader.Close();
myConnection.Close();
//########################
if (i == 1)
{
Panel1.Controls.Add(_label
PlaceHolder1.Controls.Add(
}
if (i == 2)
{
Panel2.Controls.Add(_label
PlaceHolder2.Controls.Add(
}
if (i == 3)
{
Panel3.Controls.Add(_label
PlaceHolder3.Controls.Add(
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Priest04: I'm trying to test your code, but when i click the submit button the dynamic checkboxlists are gone. The code to create the dynamic checkboxlist is under the load method(see below). why is the postback removing the created controls?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ASKER
Priest04:
Disregard the last posting. Your code is working, but it is getting all of the values. How do I get only the checked value from the checkbox.
Label1.Text = ""; // Label control is just to show the results
foreach (Control ctr in PlaceHolder1.Controls)
{
if (ctr is CheckBoxList)
{
foreach (ListItem item in ((CheckBoxList)ctr).Items)
{
Label1.Text += ctr.ID + "." + item.Text + "=" + item.Selected.ToString() + "<br />";
}
}
}
Disregard the last posting. Your code is working, but it is getting all of the values. How do I get only the checked value from the checkbox.
Label1.Text = ""; // Label control is just to show the results
foreach (Control ctr in PlaceHolder1.Controls)
{
if (ctr is CheckBoxList)
{
foreach (ListItem item in ((CheckBoxList)ctr).Items)
{
Label1.Text += ctr.ID + "." + item.Text + "=" + item.Selected.ToString() + "<br />";
}
}
}
ASKER
I change the code. I able to get what i was looking for. Thanks for all your help.
You are welcome.
Open in new window