Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I have a listview. I have added placeholder inside <itemtemplate> of listview. I am dynamically creating checkboxed inside placeholder.

there is a button, on click of which i want to display values of all checked checkboxes. How to do this?

C#
protected void lvwFields_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            PlaceHolder plhldFields = (PlaceHolder)e.Item.FindControl("plhldFields");

            ListViewDataItem item = (ListViewDataItem)e.Item;
            DataRowView dvw = (DataRowView)item.DataItem;

            CheckBox cb = new CheckBox();

            cb.ID = dvw["Val"].ToString();
            cb.Text = dvw["Txt"].ToString();
            cb.ToolTip = dvw[ViewState["FieldName"].ToString()].ToString();

            plhldFields.Controls.Add(cb);

           showSubFields(plhldFields, dvw["fieldname"].ToString(), dvw["t"].ToString());
         }

public void showSubFields(PlaceHolder ph, string strSubFldName, string strTableName)
        {
//after executing dataset
HtmlTableRow tr = new HtmlTableRow();
                            HtmlTableCell tc = new HtmlTableCell();
                            CheckBox chk = new CheckBox();
                            HtmlGenericControl hgen = new HtmlGenericControl();
                            hgen.InnerHtml = "    ";
                            chk.ID = dsSub.Tables[0].Rows[i]["SubVal"].ToString().Trim();
                            chk.Text = dsSub.Tables[0].Rows[i]["SubTxt"].ToString().Trim();
                            chk.ToolTip = dsSub.Tables[0].Rows[i]["disp"].ToString().Trim();
                            tc.Controls.Add(hgen);
                            tc.Controls.Add(chk);
                            tr.Controls.Add(tc);
                            tbl.Controls.Add(tr);
                            ph.Controls.Add(tbl);
}

there is a button on click of that i want to display checked values

protected void btnAdd_Click(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lvwFields.Items)
            {
                PlaceHolder ph = (PlaceHolder)item.FindControl("plhldFields");
                foreach (Control c in ph.Controls) //here i am getting countrols count 0
                {
                    if (c is CheckBox)
                    {

                    }
                }
            }
}


Thanks
Posted
Updated 27-May-13 23:56pm
v2
Comments
IpsitaMishra 28-May-13 5:31am    
can you please provide the code how you are adding the checkbox dynamically,so that it will be easier to go ahead.
Maciej Los 28-May-13 5:35am    
WinForms, WebControls?
lukeer 28-May-13 5:50am    
Remove the comment with the code and move the code to your question. Use the "Improve question" link for that.
Wrap the code in such tags:<pre lang="c#">YourCodeHere();</pre> to make it readable.

1 solution

All dynamic controls get lost on every post back. So you need to regenerate them on every postback by rebinding your listview (best is in pageload). After that you can fetch dynamic control value from Request object like below

C#
if (c is CheckBox)
{
    var chkval = "";
    if (Request[c.UniqueID] != null)
    {
      chkval = Request[c.UniqueID];
    }
}
 
Share this answer
 
v2
Comments
PleaseHelpCP 28-May-13 8:26am    
thanks Mahesh Bailwal. its working fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900