Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Controls / March 2008

Tip: Looking for answers? Try searching our database.

CheckBocList values problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gan Forr - 14 Mar 2008 11:32 GMT
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
Stan - 16 Mar 2008 13:28 GMT
> Hi....I have a problem.
>
[quoted text clipped - 30 lines]
> 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
Gan Forr - 19 Mar 2008 09:04 GMT
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 :(
Gan Forr - 19 Mar 2008 09:13 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.