Category Archives: CheckBoxList

[RESOLVED] Populating a dynamically generated checklist control in code-behind not working

<div style="margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); " mce_style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; ">

(The following is a complete re-edit of the original post which was rambling, confusing etc...)

I have a form with a dynamically generated checklist:

<asp:CheckBoxList ID="cblGames" runat="server" DataSourceID="sqlGames" DataTextField="Game" DataValueField="Id" </asp:CheckBoxList>

 <asp:SqlDataSource ID="sqlGames" runat="server" ConnectionString="<%$ ConnectionStrings:Games %>" 

SelectCommand="SELECT [Id], [Game], [ShowWaitingList] FROM [Aanbod] WHERE ([ShowWaitingList] = @ShowWaitingList)">

 <SelectParameters><asp:Parameter DefaultValue="true" Name="ShowWaitingList" Type="Boolean" /></SelectParameters>

</asp:SqlDataSource>

The following code-behind is supposed to check those boxes that correspond to values stored in a database. It fails and I don't know why. Any ideas?

regards, Guy (p.s. [AanbodId] and [Id] are equivalent.)


if    (!Page.IsPostBack && Request.QueryString["id"] != null)
        {
               // Code that gets the data goes here. Removed for brevity's sake //
              ListItem currentCheckBox = cblGames.Items.FindByValue(reader["AanbodId"].ToString());
              if (currentCheckBox != null)
              {
                  currentCheckBox.Selected = true;
              }
             

            }
        }


</div>


First of all, here's a tip using the Insert Code tool:  Once you've inserted a code block, don't edit it.  That's probably how you ended up with all those spurious <SPAN> tags in your C#.  (No disrespect intended - just something I had to learn the hard way.)

Secondly, .aspx pages by default post back to themselves, so you shouln't need to redirect a page back to itself.  If you use Response.Redirect to do that, you'll loose all the information saved in viewstate, which may or may not explain why you're having this problem.

HI, (this post can be ignored, see re-edit above)


I tried to edit the code down because it's a little convoluted (I'm a newbie) and I hoped just presenting the key  would make it easier to understand the problem. I'm sure those span tags didn't show in the preview!


Anyway, the page only redirects after a new record is inserted. This insures that the user is presented with a blank form to enter the next record. I see the usefulness of viewstate for many tasks, but think it less than useful for database inserts which most of the time involve moving onto the next record. I considered resetting all entry fields to blanks in the code behind, but a redirect is simpler (if not  exactly elegant).

So I am still stuck with the original problem. Is it clear what I want to be done and what the problem is?

Both scenarios/solutions I've tried take place with viewstate (not that it is used). Neither give me the result I want.


regards, Guy

Are you running the above code in the CheckBoxList.DataBound handler or where?  (It won't work before that event.)

(And you're right about preview mode; it's useless.  You just have to learn the hard way, like all of us, that the code block starts and ends with the two blank lines above and below the code, and that once you click Insert, the only safe way to edit it is in HTML mode.  And even in HTML mode, you can't put HTML tags between the <PRE> and </PRE> tags.)

[RESOLVED] How to determine if all checkboxes are checked in code behind

I have a page that im using javascript to do a select all / unselect all for my checkboxes on the page.
Now i need to determine in the code behind if all of them are checked, im wondering if it would be easier to just set a session or something when
the select all is checked, but that wouldnt be accurate, because after you select all, you still can uncheck some..

So i need it to check at the time the button is clicked.. there are currectly 30 checkboxes, NOT a checkboxlist.

I tried using the site search but getting an error has occured when i click to search...

Can anyone help?

Assuming you're using <asp:CheckBox> controls, and that you aren't doing this inside something like a DataGrid, the following VB should work.

Dim MyCheckBoxIsChecked as Boolean = MyCheckBoxId.Checked

Of course, you would have to do that for each checkbox.

Or you could create a gigantic If claus that AND'ed them all together:

If MyCheckBoxId01.Checked And MyCheckBoxId02.Checked And MyCheckBoxId03.Checked And MyCheckBoxId04.Checked ... Then

i was hoping for something with less code.. the javascript i have is 3 lines and handles all the checkboxes.. could there be something to convert the javascript i have to C#?

cubangt

the javascript i have is 3 lines and handles all the checkboxes
[Hmm, that couldn't be jQuery, could it?]

JavaScript and C# function in different environments and work with different object hierarchies.  AFAIK, there is no translation.

In my experience, my time is better spend using copy/paste to generate 30 lines of dumb, repetitive code than it would be debugging a clever routine that attempts to scan all the controls on a page and determine programmatically whether or not it's one that I'm looking for.  (YMMV)

I dont mind doing 30 lines.. figured i'd check if there was something better / different to do the same thing in C#

went ahead and did it with all 30 checkboxes.

 

thanks

you just need a function to walk the control hierarchy which you can pass the test to.

public void ControlWalker(Control ctl, Action<Control> action)  {
        action(ctl);
        foreach (Control child in ctl.Controls)
            ControlWalker(child, action);
 }
....

bool allChecked = true;
ControlWalker(Page, ctl => {
       if (ctl is ICheckBoxControl)
              allChecked = allChecked & ((ICheckBoxControl)ctl).Checked;
});

//or if you don't want to predefine a function:

bool allChecked = true;
Action<Control> walk = null; //hack to allow recursion
walk = (ctl => {
      if (ctl is ICheckBoxControl)
            allChecked = allChecked & ((ICheckBoxControl)ctl).Checked;
      foreach (Control child in ctl.Controls)
             walk(child);
});

 

[RESOLVED] Issue with displaying check box list values in a label or some other web control

I have trouble displaying the values of what options the user checked in a checkboxlist on a aspx page..... I am trying to do this programmatically through VB coding....This is what I have so far below in my code, but it just replaces the last value checked from the checkbox list.... I want it to display all the values checked onto one label....

 

If its not possible to display all the values of the checked values onto a label, what other web controls can I use to accomplish this...

Here is my code.....

 lblProLanguageInput.Text = " "

        If ProLanguages.Items(0).Selected Then

            lblProLanguageInput.Text = ProLanguages.Items(0).Text & "</br>"

        ElseIf ProLanguages.Items(1).Selected Then

            lblProLanguageInput.Text = ProLanguages.Items(1).Text & "</br>"

        ElseIf ProLanguages.Items(2).Selected Then

            lblProLanguageInput.Text = ProLanguages.Items(2).Text & "</br>"

        ElseIf ProLanguages.Items(3).Selected Then

            lblProLanguageInput.Text = ProLanguages.Items(3).Text & "</br>"

        End If

<div></div>

For every item that's checked, you're overwriting the value in the label.  instead you want to append to it.

So replace this: lblProLanguageInput.Text = ProLanguages.Items(1).Text & "</br>"

with this: lblProLanguageInput.Text = lblProLanguageInput.Text & ProLanguages.Items(1).Text & "</br>"

and do the same for item 2 & 3 as well.  Actually, it's better if you use something like a StringBuilder to build the string, and then at the end you set the .Text property of the label to the value in the stringbuilder.

I just tried to replace the code with what you recommended, but it didnt work....As far as stringbuilder, I am not familiar with using that before and I did some research, but I cannot get it to work.... Frown

Try using a multiline textbox to display the results instead as a test. Also, if you're familiar with debugging, put a breakpoint at the end of the method to check the .Text value and see if you're capturing everything you expect.

I figured it out using a For Each loop to display the checked items....

 

 

lblProLanguageInput.Text =

String

.Empty

 

For Each ListItem In

ProLanguages.Items

 

If (ListItem.selected)

Then

 

 

lblProLanguageInput.Text += ListItem.Text +

 

"<br />"

 

 

End

If

 

Next

Hello, this is a very simple solution to your question:

This is the code:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim i As Byte
        ListBox1.Items.Clear()
        For i = 0 To CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then ListBox1.Items.Add(CheckBoxList1.Items(i).Text)
        Next
    End Sub

And here the html:

 

<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Option1</asp:ListItem>
            <asp:ListItem>Option2</asp:ListItem>
            <asp:ListItem>Option3</asp:ListItem>
            <asp:ListItem>Option4</asp:ListItem>
        </asp:CheckBoxList>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
    
    </div>
    </form>
</body>

Hope it helps ;)

 

[RESOLVED] Real-time control effect via code behind

Hi

I have two checkboxes on web form chkYes and chkNo. When I click either I want the other one to be un-ticked. I have implemented the following in code behind;

protected void chkNo_CheckedChanged(object sender, EventArgs e)
{
  this.chkYes.Checked = false;
}
 
protected void chkYes_CheckedChanged(object sender, EventArgs e)
{
  this.chkNo.Checked = false;
}

Problem is ticking one tick has no effect on the other. What is the reason and how can I make it work?

Thanks

Regards

you'd need to set the checkbox's autopostback property to true to see the code run as soon as you click.

note - I think that you might want to check to see if the checkbox that is firing its changed event is actually checked before you uncheck the other one..??

keep in mind that doing postbacks can be heavy, so you might also want to consider a client side jQuery solution for mutually exclusive checkboxes.

http://dotnetspidor.blogspot.com/2009/10/jquery-mutually-exclusive-checkboxes-in.html 

Hi, You can use AJAX MutuallyExclusiveCheckBoxExtender without post back of page

see: http://www.asp.net/ajax/tutorials/creating-mutually-exclusive-checkboxes-vb

but best is use radio button list it ment for single select only

also see:

http://stackoverflow.com/questions/1818102/how-to-make-user-select-only-one-check-box-in-a-checkboxlist

 

you can make this one happen on client side itself....there is no need for you to make a round trip to server for this requirement...

make use of javascript....try this...

http://blog.schuager.com/2008/09/mutually-exclusive-checkboxes-with.html

Thanks.

[RESOLVED] Html Checkbox Get Value of checked item

Hello I am creating Dynamic Control.

I want to retrive value of selected Checkbox. below i written source of checbox.

<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" type="checkbox">
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$1" type="checkbox">
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_2" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$2" type="checkbox">

My Code Is below.

if (Session["CurrentControlObjType"] == "Checkbox")
                {
                    StringBuilder items = new StringBuilder();
                    string[] item = null;
                    for (int i = 7; i < Request.Form.Count - 1; i++)
                    {
                        string tmp = Request.Form[i];
                        if (!string.IsNullOrEmpty(tmp))
                        {
                            item =(tmp.Split(new char[] { ',' }));


                        }
                    }
                    strbuild.Append("<<" + Session["CurrentControlCaption"] + "$$" + Session["CurrentControlId"] + "$$" + item + ">>");


<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" />Other, Please specify
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$1" />ddd</
input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_2" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$2" />ccde<
                }

what is solution for retrive my Value.


Hello prajapatiamit,

I cud'nt figure out the actual source code for your design.

But still.

If your checkboxes are web server controls then you can easily access the controls by their ids and check their checked property to determine whether they are checked or not.

If you have used checkboxlist then you can loop throught each items in the checkboxlist.items collection and check the Selected property of each item.

If you have used the html input controls then remember that the request.forms collection that you are using consists of only those items that are checked.

 

Hope been helpful

 

 

If you have used the html input controls then remember that the request.forms collection that you are using consists of only those items that are checked. My answer is yes I m getting only check item but strange issue is i m getting string 'on'. what is that meaning. u have any idea.

prajapatiamit2003

but strange issue is i m getting string 'on'. what is that meaning

It's meaning is that your checkbox controls have "on" values in their value property.

 

Can you please show the design code once again coz in your previous post their was some problem. I can't see it.

 

 

<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" />
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$1" />
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_2" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$2" />
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" />
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$1" />
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_2" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$2" />
<input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" /> <input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$1" /><
Hello I am not able to paste HTML content here. if try paste HTML control it convert in design <input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" /> <input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$1" /><
<table id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010" border="0" style="width:500px;">
					<tr>
						<td><input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" /><label for="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0">Other, Please specify</label></td>
					</tr><tr>
						<td><input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$1" /><label for="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1">ddd</label></td>
					</tr><tr>
						<td><input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_2" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$2" /><label for="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_2">ccde</label></td>
					</tr>
				</table>


 

Hi prajapati

From your code it seems like you are using checkboxlist control. I'm preety sure of that

Here is a sample code you can check for it.

StringBuilder strbuild = new StringBuilder();
               
                strbuild.Append("<<" + Session["CurrentControlCaption"] + "$$" + Session["CurrentControlId"] + "$$");

                foreach (ListItem chkItem in chklist.Items)
                {
                    
                    if (chkItem.Selected)
                    {
                        strbuild.Append(chkItem.Text + "," ); // or chkItem.Value for values.

                    }
                }
                if (strbuild.ToString().Substring(strbuild.Length, 1) == ",")
                    strbuild.Remove(strbuild.Length, 1);

                strbuild.Append(">>");

Here i'm assuming that the name of your checkbox list is chklist

 

Hope been helpful

 

Hi prajapati

You can use this javascript code for find checkbox item value.


<script type="text/javascript" language="javascript">
    function GetValue() {
        var divCollection = document.getElementsByTagName('input');
        for (var i = 0; i < divCollection.length; i++) {
            if (divCollection[i].id.indexOf('chkSelect|') != '-1') {
                if (document.getElementById(divCollection[i].id).checked) {
                    var checkId = divCollection[i].id.split('|');
                    if (document.getElementById('<%=hdnSelect.ClientID%>').value == '') {
                        document.getElementById('<%=hdnSelect.ClientID%>').value = checkId[1];
                    }
                    else {
                        document.getElementById('<%=hdnSelect.ClientID%>').value = document.getElementById('<%=hdnSelect.ClientID%>').value + '|' + checkId[1];

                    }
                }
            }
        }

        return true;
    }
  
    
</script>


If this post  help you then Mark as Answer.


Hello I am generating this checkbox dynamically so i am not able to find this checkbox.

If you are generating your control dynamically at runtime have given their IDs then you may need to recreate them on Page_Init event, so that you can access them. I hope you have given them some Ids.

But if you haven't then you might come across some problem, as request.form is the collection of key value pair elements where key is the name of your control and value is the value field of the element.

This is all I can say, unless you show us the code you used to generate these controls.

 

Hope been helpful

 

I am creating Each Control on Button Click. each only one control like wizard type.

Hi prajapatiamit,

Creating controls dynamically at runtime comes with a cost. Asp.net is stateless. So it doesn't have any way to know it's state. Controls are on their own to save their state, either using viewstate or session or input fields.

On way to restore the state of a dynamically created control is to recreate them on postback in Page_Init event. Asp.net automatically sets their corresponding values so that you can access them later on. However your request.form consists of the collection of key value pairs where key is the name of your control and value is the corresponding value of the control. And remember as i told you before, in case of check box and radio buttons only the 1 that was selected in the browser are posted in the request.form collection. All others are discarded on postback from the browser.

So If you want to use the request.form collection then you need to know the id or name of your input element. Remember the server controls id and name when rendered are appended with the contentplaceholder's client id . If you control is inside contentplace holder with id "MainContent" then you can get the name of your dynamic control as this.

string chkid=MainContent.ClientID + "_" + "your_checkboxidorname";

then check the value in the form collection

if(Request.Form[chkid]==null) then

//this means your checkbox was not selected

else

//this means your checkbox was selected.

Same thing goes for the rest of your checkboxes.

It's difficult, isn't it?

 

You can also create the page variables for your checkboxes. Recreate them once again in your page_Init event and access them wherever you require. But this may not work for you as your controls are dynamically added depending upon current wizard step.

You can also place the controls statically in the page and set them to invisible and make them visible only when required. This way you won't have any problem accessing them wherever you want.

If you are developing wizard like interface then use the wizard control of asp.net

Check out these links for that

http://www.4guysfromrolla.com/articles/061406-1.aspx

http://weblogs.asp.net/scottgu/archive/2006/02/21/438732.aspx

 

Hope been helpful

 

hi

Thank you for your prompt reply.

I am able to get checked value of checkbox , but it return value 'ON'.

I need a Label Value.

My Same code is working fine with Radio button , and other control, just issue with checkbox  control only.


You need to make sure that the values of your checkboxes are set.

 

hi

1. <table id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010" border="0" style="width:500px;">
   2.                     <tr>
   3.                         <td><input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" /><label for="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_0">Other, Please specify</label></td>
   4.                     </tr><tr>
   5.                         <td><input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$1" /><label for="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_1">ddd</label></td>
   6.                     </tr><tr>
   7.                         <td><input id="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_2" type="checkbox" name="ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$2" /><label for="ctl00_ContentPlaceHolder1_Survey_Preview1_chk2107159010_2">ccde</label></td>
   8.                     </tr>
   9.                 </table>

when i click on first checkbox then i return "ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010$0" this is true, but it value id 'on'

i need a value 'Other, Please specify'.



Hi prajapatiamit,

Try this first.

Yourcheckbox.Attributes.Add("value", "yourdesiredvalue");

for each checkbox control

If this doesn't work then

Create htmlgeneric control instead of web server controls as:

CheckBox chk = new CheckBox();
            System.Web.UI.HtmlControls.HtmlInputCheckBox chk2 = new System.Web.UI.HtmlControls.HtmlInputCheckBox();
            Label lbl = new Label();
            chk2.ID = "mychkcontrol";
            chk2.Value = "1";
            lbl.Text = "Other Please specify";
            
            Page.Controls.Add(chk2);
            Page.Controls.Add(lbl);

Hope been helpful

 

HTML checkboxes only return the value "on" when the form is submitted and
they are checked, or nothing when they are not checked. 


Same issue i have i found from below link but not found my solution.

http://bytes.com/topic/asp-net/answers/324856-accessing-html-checkbox


I have already told you that if the checkbox controls have their value attribute set then it returns the value, but if it doesn't have it's value property set then it return "On".

In your case, if you look at the rendered code, then the control's values(value attribute) are not set. That's why when you try to get the value from the form collection it returns On.

Try my previous solution. Add the attribute "value" for each checkbox control and see the rendered code in your browser as before. It the input element contains the value attribute then that value will be returned to you in your form collection.

And remember only those checkboxes are posted to the server which are checked.

Please try this at least.

 

 

 

Hello I am not able to under stand what r u saying

I m generation Chekcbox using Below code

#region Checkbox
                                    // XmlNodeList dataSource = DataSource.SelectNodes("item");
                                    CheckBoxList rcbList = new CheckBoxList();
                                    rcbList.ID = obj.Id;


                                    if (obj.Mandatory == "true")
                                    {
                                        lblCaption.Text = "Q." + QuestinNO + " of " + countquestion + ".  " + obj.Caption;
                                        tcLabel.InnerHtml = "<em class='Mandatory'>* </em>";


                                    }
                                    else
                                    {
                                        lblCaption.Text = " " + "Q." + QuestinNO + " of " + countquestion + ".  " + obj.Caption;
                                    }






                                    rcbList.Width = Convert.ToInt32(obj.Width);


                                    tcLabel.Controls.Add(lblCaption);




                                    tcControl.Controls.Add(rcbList);




                                    trField.Cells.Add(tcLabel);


                                    trblankrow.Cells.Add(tcBlankcol); //For BlankRow
                                    tcBlankcol.Style.Add("height", "10px");
                                    tcLabel.Style.Add("height", "30px");


                                    tccolumn2.Style.Add("width", "100px");


                                    trControl.Cells.Add(tccolumn2); //for colspan
                                    trControl.Cells.Add(tcControl);






                                    tblAdditionalInfo.Controls.Add(trField);
                                    tblAdditionalInfo.Controls.Add(trblankrow);
                                    tblAdditionalInfo.Controls.Add(trControl);


                                    trField.Attributes.Add("class", "Section");








                                    List<String> Source = new List<string>();
                                    XmlAttribute DefaultAttribute;
                                    int DefaultIndexCnt = -1;
                                    int DefaultIndex = -1;


                                    string b = obj.Item;
                                    string[] words = b.Split('$', '$');


                                    foreach (string word in words)
                                    {


                                        if (word != "")
                                        {
                                            Source.Add(word);
                                        }
                                    }






                                    rcbList.RepeatDirection = RepeatDirection.Vertical;
                                    rcbList.DataSource = Source;
                                    rcbList.DataBind();
                                    if (DefaultIndex >= 0)
                                        rcbList.Items[DefaultIndex].Selected = true;


                                    //this.Controls.Add(tblAdditionalInfo);


                                    epp2SurveyPL.CustomFieldsDataProperties objField = new epp2SurveyPL.CustomFieldsDataProperties();
                                    objField.FieldId = obj.Id;
                                    objField.FieldType = obj.Type;
                                    objField.Caption = obj.Caption;
                                    FieldIds.Add(objField);


                                    if (words.Contains("Other, Please specify"))
                                    {
                                        TextBox txtCustom = new TextBox();
                                        txtCustom.ID = "txtother" + obj.Id;
                                        txtCustom.MaxLength = obj.MaxLength == String.Empty ? 0 : Convert.ToInt32(obj.MaxLength);
                                        txtCustom.TextMode = TextBoxMode.MultiLine;
                                        txtCustom.Rows = 5;
                                        txtCustom.Columns = 40;
                                        tcControl.Controls.Add(txtCustom);
                                        trControl.Cells.Add(tcControl);
                                        tblAdditionalInfo.Controls.Add(trControl);
                                        trField.Attributes.Add("class", "Section");
                                        objField.FieldId = "txtother" + obj.Id;
                                        objField.FieldType = "TextBox";
                                        objField.Caption = "";
                                        FieldIds.Add(objField);
                                    }


                                    this.Controls.Add(tblAdditionalInfo);


                                    Session["PersonalCustomFields"] = FieldIds;
                                    #endregion


So as I told you before you have been using checkbox list control to render those checkboxes.

You still haven't given me your design code. However I can still try.

So based on what you have said earlier

prajapatiamit2003

I am able to get checked value of checkbox , but it return value 'ON

In that case just add the following line of code

 

//instead of these lines
//rcbList.DataSource = Source;
//rcbList.DataBind();

//use these lines


            foreach(string s in source)
            {
                
                rcbList.Items.Add(new ListItem(s,s));

            }

 

 

 

Hope been helpful

I tried with your code same issue.

here i am sending my design code.

HtmlTableRow trButtonRow = new HtmlTableRow();




                trButtonRow.Attributes.Add("class", "buttonrow");


                //trButtonRow.Style["buttonrow"]
                HtmlTableCell tcButtoncell1 = new HtmlTableCell();
                tcButtoncell1.Style["text-align"] = "right";
                tcButtoncell1.ColSpan = 4;




                int rowIndex = Convert.ToInt32(ViewState["QuestionNo"]);
                int j = 0;
                int countquestion = Qlist.Count;
                foreach (epp2SurveyPL.ContractClass.SurveyQuestion obj in Qlist)
                {
                    if (j == rowIndex)
                    {
                        Label lblCaption = new Label();
                        HtmlTableRow trField = new HtmlTableRow();
                        HtmlTableCell tcLabel = new HtmlTableCell();
                        HtmlTableCell tcmandatory = null;
                        HtmlTableCell tcbgcolumn = new HtmlTableCell();
                        HtmlTableCell tccolumn = new HtmlTableCell();
                        HtmlTableCell tccolumn2 = new HtmlTableCell();
                        HtmlTableCell tccolumn1 = new HtmlTableCell();
                        HtmlTableCell tcControl = new HtmlTableCell();






                        HtmlTableRow trControl = new HtmlTableRow();
                        HtmlTableRow trblankrow = new HtmlTableRow();
                        HtmlTableCell tcBlankcol = new HtmlTableCell();


                        HtmlTableCell tcOther = new HtmlTableCell();
                        HtmlTableRow trOther = new HtmlTableRow();






                        //tcControl.ColSpan = 3;
                        tcLabel.ColSpan = 3;
                        Int32 QuestinNO = 1 + j;
                        if (countquestion == QuestinNO)
                        {
                            btnnext.Text = "Submit";
                        }




                        switch (obj.Type)
                        {
   case "Checkbox":
                                {
                                    #region Checkbox
                                    // XmlNodeList dataSource = DataSource.SelectNodes("item");
                                    CheckBoxList rcbList = new CheckBoxList();
                                    rcbList.ID = obj.Id;
                                    



                                    if (obj.Mandatory == "true")
                                    {
                                        lblCaption.Text = "Q." + QuestinNO + " of " + countquestion + ".  " + obj.Caption;
                                        tcLabel.InnerHtml = "<em class='Mandatory'>* </em>";


                                    }
                                    else
                                    {
                                        lblCaption.Text = " " + "Q." + QuestinNO + " of " + countquestion + ".  " + obj.Caption;
                                    }






                                    rcbList.Width = Convert.ToInt32(obj.Width);


                                    tcLabel.Controls.Add(lblCaption);




                                    tcControl.Controls.Add(rcbList);




                                    trField.Cells.Add(tcLabel);


                                    trblankrow.Cells.Add(tcBlankcol); //For BlankRow
                                    tcBlankcol.Style.Add("height", "10px");
                                    tcLabel.Style.Add("height", "30px");


                                    tccolumn2.Style.Add("width", "100px");


                                    trControl.Cells.Add(tccolumn2); //for colspan
                                    trControl.Cells.Add(tcControl);






                                    tblAdditionalInfo.Controls.Add(trField);
                                    tblAdditionalInfo.Controls.Add(trblankrow);
                                    tblAdditionalInfo.Controls.Add(trControl);


                                    trField.Attributes.Add("class", "Section");








                                    List<String> Source = new List<string>();
                                    XmlAttribute DefaultAttribute;
                                    int DefaultIndexCnt = -1;
                                    int DefaultIndex = -1;


                                    string b = obj.Item;


                                    string[] words = b.Split('$', '$');
                                    StringBuilder stg = new StringBuilder();
                                    foreach (string word in words)
                                    {


                                        if (word != "")
                                        {
                                            //rcbList.Items.Add(word);


                                          //  Source.Add(word);
                                            //stg.Append(word + "-");
                                            rcbList.Items.Add(new ListItem(word, word));




                                        }
                                    }




                                    Session["DropDownValue"] = stg;
                                    rcbList.RepeatDirection = RepeatDirection.Vertical;
                                    //rcbList.DataSource = Source;
                                    //rcbList.DataBind();


                                  

                                    if (DefaultIndex >= 0)
                                        rcbList.Items[DefaultIndex].Selected = true;


                                    //this.Controls.Add(tblAdditionalInfo);


                                    epp2SurveyPL.CustomFieldsDataProperties objField = new epp2SurveyPL.CustomFieldsDataProperties();
                                    objField.FieldId = obj.Id;
                                    objField.FieldType = obj.Type;
                                    objField.Caption = obj.Caption;
                                    FieldIds.Add(objField);


                                    if (words.Contains("Other, Please specify"))
                                    {
                                        TextBox txtCustom = new TextBox();
                                        txtCustom.ID = "txtother" + obj.Id;
                                        txtCustom.MaxLength = obj.MaxLength == String.Empty ? 0 : Convert.ToInt32(obj.MaxLength);
                                        txtCustom.TextMode = TextBoxMode.MultiLine;
                                        txtCustom.Rows = 5;
                                        txtCustom.Columns = 40;
                                        tcControl.Controls.Add(txtCustom);
                                        trControl.Cells.Add(tcControl);
                                        tblAdditionalInfo.Controls.Add(trControl);
                                        trField.Attributes.Add("class", "Section");
                                        objField.FieldId = "txtother" + obj.Id;
                                        objField.FieldType = "TextBox";
                                        objField.Caption = "";
                                        FieldIds.Add(objField);
                                    }


                                    this.Controls.Add(tblAdditionalInfo);


                                    Session["PersonalCustomFields"] = FieldIds;
                                    #endregion
}
   trButtonRow.Style.Add("valign", "top");
                        tblAdditionalInfo.Controls.Add(trButtonRow);

My Value Retrive code is

if (Session["CurrentControlObjType"].ToString() == "Checkbox")
{


                    string b = Request.Form[7].ToString();
                    //string n = Request["ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010_0"].ToString();
                    if (b == "on")
                    {
                        //string  n = Request.Form["ctl00$ContentPlaceHolder1$Survey_Preview1$chk2107159010_"[0]];
                    }


               

                    StringBuilder items = new StringBuilder();
                    string[] item = null;
                    for (int i = 7; i < Request.Form.Count - 1; i++)
                    {
                        int j = i - 7;
                        string tmp = Request.Form[i].ToString(); ;
                        //string[] drpvalue = Session["DropDownValue"].ToString().Split('$','$');
                        if (!string.IsNullOrEmpty(tmp))
                        {
                            
                            item = (tmp.Split(new char[] { ',' }));
                            items.Append(item[0]);

                        }
                    }
                     
                    if (Session["SavedData"] != null)
                    {
                        if (Session["SavedData"].ToString().Contains(Session["CurrentControlId"].ToString()))
                        {
                            if (Session["SavedData"].ToString().Contains(Session["CurrentControlId"].ToString()))
                            {
                                string[] CurrentQuestion = Session["SavedData"].ToString().Split(new char[] { '<', '<', '>', '>' });
                                foreach (string s in CurrentQuestion)
                                {
                                    if (s.Contains(Session["CurrentControlId"].ToString()))
                                    {
                                        Session["SavedData"] = Session["SavedData"].ToString().Replace("<<" + s + ">>", "");
                                        break;
                                    }
                                }
                            }
                            Session["SavedData"] = Session["SavedData"].ToString() + strbuild.Append("<<" + Session["CurrentControlCaption"] + "$$" + Session["CurrentControlId"] + "$$" + items + ">>");
                        }
                        else
                        {
                            Session["SavedData"] = Session["SavedData"].ToString() + strbuild.Append("<<" + Session["CurrentControlCaption"] + "$$" + Session["CurrentControlId"] + "$$" + items + ">>");
                        }
                    }
                    else
                        Session["SavedData"] = strbuild.Append("<<" + Session["CurrentControlCaption"] + "$$" + Session["CurrentControlId"] + "$$" + item + ">>");
                }

I have still issue.

Hi prajapatiamit,

First you need to recreate the controls by calling the same function or  by creating another function in Page_init event. But make sure you recreate  them.
After that in the function where you are actually trying to read the values from the checkboxes, get the checkboxlist as follows.

HTMLTable tblAdditionalInfo=this.FindControl("tblAdditionalInfo_id");//here tblAdditionalInfo_id is the id of your dynamically created tbladditionalInfo html table. or if your tblAdditionalInfo isn't added at runtime then you don't need to get it above way. You can reference it directly.

HtmlTableRow trControl=tblAdditionalInfo.Rows[trcontrol_index]// trcontrol_index is  the index of the trControl. It is hard to know the index but it's the same order you added. From the code it seems like the index must be 2.

HTMLTableCell tcControl=trControl.Cells[tcControl_index]//tcControl_index is the index of the the cell. It must be 1;

CheckBoxList rcbList=tcControl.FindControl('rcbList_id') as CheckBoxList;//I guess rcbList_id can be taken from obj.Id as per your code.

//If you are able to come so far then, Now check for rcblist . If it's null then simply try this to get your checkboxlist as follows.

CheckBoxList rcbList=this.FindControl('rcbList_id');

//Now you have your checkboxlist. Now loop through each listitem in the list.

foreach (ListItem item in lst.Items)
            {
                if (item.Selected)
                {
                    string strItem = item.Text; //or item.Value;
					//add it to your item list. or do whatever you want .

                }
            }

Try this once and let us know. Your code seems preety hefty. Instead of creating these controls dynamically at runtime, you must rethink your strategy and go for wizard control. I'm sure you'll feel easy that way.

 

 

Hi prajapatiamit2003,

The Request.Form include key value pair.

And record which control cuase the postback.

When the checkbox cause the postback, the request.form will include the checkbox id and its status.

Thus the checkbox value is the "on", it may be the  system setting.

Hello sory for late reply

if i recreate them then my Value is lost.

I guess i told you that.

Dynamically created controls do not persist their state on postback. To do so you need to recreate the controls again in pre_init event or load event, to get their value.

Please check previous posts again.