CheckBocList values problem

G

Gan Forr

Hi....I have a problem.

I created a CheckBoxList on a web form, but...when I populate it....in
the resulting Html page there is no value in every checkboxlist item.

I did something like this (suppose that I pass the CheckBoxList as
parameter):

private void
PopulateCheckBoxList(System.Web.UI.WebControls.CheckBoxList control)
{
control.Items.Clear();
for (int i=0; i<10; i++)
{
string Cod = "Value" + i.ToString();
string Text = "Text" + i.ToString();
ListItem item = new ListItem();
item.Text = Text;
item.Value = Cod;
item.Attributes.Add("value",Cod);
control.Items.Add(item);
//control.Items.Add(new System.Web.UI.WebControls.ListItem(Text,
Cod));
}
}

I noticed that the checked values are actually present when I get them
from the c# code, but I need to have them in the html page too. I
tried to add manually the "value" attribute as you can see but I
couldn't get the value in the web page.

I hope somebody can help me
Thanks
Federico
 
S

Stan

Hi....I have a problem.

I created a CheckBoxList on a web form, but...when I populate it....in
the resulting Html page there is no value in every checkboxlist item.

I did something like this (suppose that I pass the CheckBoxList as
parameter):

                private void
PopulateCheckBoxList(System.Web.UI.WebControls.CheckBoxList control)
                {
                        control.Items.Clear();
                        for (int i=0; i<10; i++)
                        {
                                string Cod= "Value" + i.ToString();
                                string Text = "Text" + i.ToString();
                                ListItem item = new ListItem();
                                item.Text = Text;
                                item.Value= Cod;
                                item.Attributes.Add("value",Cod);
                                control.Items.Add(item);
                                //control.Items.Add(new System.Web.UI.WebControls.ListItem(Text,
Cod));
                        }
                }

I noticed that the checked values are actually present when I get them
from the c# code, but I need to have them in the html page too. I
tried to add manually the "value" attribute as you can see but I
couldn't get the value in the web page.

I hope somebody can help me
Thanks
Federico

Hi

One problem I can see with the sample code is that the 'control'
parameter is passed as a value instead of a reference. The code in the
body of the procedure will work on a copy of the object rather than
the object itself, hence the original CheckBoxList control will be
unmodified leaving the ListItems collection empty.

Try replacing the header with this:

private void PopulateCheckBoxList(ref
System.Web.UI.WebControls.CheckBoxList control)

Then call it like this:

PopulateCheckBoxList(ref MyCheckBoxList);

Also delete the line:
item.Attributes.Add("value",Cod);

I'm not sure what effect it will have (potentially it will corrupt it
with duplicate attribute names) but the two lines of your code prior
to this one are the proper way to set the item properties
(alternatively do it with the constructor).

On a point of coding style I would avoid using type names or common
property names as instance identifiers (I refer to your use of
'control' and 'Text' as variable names).

HTH
 
G

Gan Forr

First of all, thans for your reply.
One problem I can see with the sample code is that the 'control'
parameter is passed as a value instead of a reference.

Yes, I know, but it works with any other type of control...try drop
down lists or radiolists, I never had the need to pass the argument by
rerefence....
By the way...I tried your suggestion and there is no difference.
On the other way....if I try to get the values of the CheckBoxList on
the server side, it works, I can get the selected values but, in the
resulting html page, there is no property named value, so I assume it
is stored in the viewstate of the asp page.

The reason of this:
item.Attributes.Add("value",Cod);

is because there is no value property for the checkboxes in the list,
so I tried to manually add it.
You said that potentially I could obtain a duplicate value property
but, unfortunately, none of them is displayed in the page.

I reduced the code to this:

private void CaricaCheckBoxList(ref
System.Web.UI.WebControls.CheckBoxList ChkLstControl)
{
ChkLstControl.Items.Clear();
for (int i=0; i<10; i++)
{
string Val = "Value" + i.ToString();
string Txt = "Text" + i.ToString();
ChkLstControl.Items.Add(new
System.Web.UI.WebControls.ListItem(Txt, Val));
}
}

but the result is the same :(
 
G

Gan Forr

Just wanted to say....it's very simple to verify the problem:

add a checkboxlist to a webform.
Even if you add items to the collection with text and values directly
from the design view, without filling the control at runtime, you
still get no value property in the resulting html page.

Try it yourself
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top