asp.net makes me mad!

S

suzy

i thought .net was meant to make things easier, so why is this simple thing
taking me over 2 days.

i have a asp.net page with a checkboxlist control on it. This displays a
bunch of checkboxes. All i want to do is add an onclick event to each
checkbox that calls a javascript function passing in the id of the checkbox.
i could do this easily in classic asp so why is it such a bloody nightmare
in .net?!

to populate the checkboxlist control i call the .DataSource and .DataBind
methods. So I am not doing any explicit looping. However, I have tried
looping through the checkboxlist elements after .DataBind() and manually
call .Attributes.Add it still doesn't work. (see below)

foreach (ListItem item in ((CheckBoxList)
chkList.FindControl("chkList")).Items)
{
item.Attributes.Add ("onclick",
string.Format("javascript:check_Click({0});", item.Value));
}

I don't know if this is the right way to go about things, but the code runs
without errors (and the lines of code are being called when i step through
the code). However, there is no sign of any onclick attriibute on the
client (even if i view source of the html page). it is nowhere in the page.

Someone please help! I am wasting so much time with this.

thank you so much.

:)
 
M

Mark Fitzpatrick

Have you also tried the following way also?

for(int i = 0; i < CheckBoxList1.Items.Count;i++)
{

CheckBoxList1.Items.Attributes.Add("onclick",string.Format("javascript:ch
eck_Click({0});", item.Value)));
}

Another way might be:
for(int i = 0; i < CheckBoxList1.Items.Count;i++)
{
ListItem newItem = new ListItem();
newItem.Text = CheckBoxList1.Items.Text;
newItem.Value = CheckBoxList1.Items.Value;
newItem.Attributes.Add("onclick",string.Format("javascript:check_Click({0});
", newItem.Value));
newItem.Selected = CheckBoxList1.Items.Selected;
CheckBoxList1.Items.RemoveAt(i);
CheckBoxList1.Items.Insert(i, newItem);
}

Though in the above you'd probably have to play with the Insert function and
do a quick check to make sure that you're not at the last position otherwise
it might through an array out of bounds error and you can just use the add
method in that case.

One of the problems with databinding in certain controls is, once the child
control is added, it's not alterable. A bit of a pain to say the least but
that's often how it is. In some cases it's actually easier to loop through
your data, create a listitem just how you need it and add it to the
collection. Sometimes using the automatic way of doing things is more
trouble than it's worth as it would take you no more than 5 minutes to write
the code to add the items individually.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP- FrontPage
 
S

suzy

oh great.... look what i've just found:

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q309338


Mark Fitzpatrick said:
Have you also tried the following way also?

for(int i = 0; i < CheckBoxList1.Items.Count;i++)
{

CheckBoxList1.Items.Attributes.Add("onclick",string.Format("javascript:ch
eck_Click({0});", item.Value)));
}

Another way might be:
for(int i = 0; i < CheckBoxList1.Items.Count;i++)
{
ListItem newItem = new ListItem();
newItem.Text = CheckBoxList1.Items.Text;
newItem.Value = CheckBoxList1.Items.Value;
newItem.Attributes.Add("onclick",string.Format("javascript:check_Click({0});
", newItem.Value));
newItem.Selected = CheckBoxList1.Items.Selected;
CheckBoxList1.Items.RemoveAt(i);
CheckBoxList1.Items.Insert(i, newItem);
}

Though in the above you'd probably have to play with the Insert function and
do a quick check to make sure that you're not at the last position otherwise
it might through an array out of bounds error and you can just use the add
method in that case.

One of the problems with databinding in certain controls is, once the child
control is added, it's not alterable. A bit of a pain to say the least but
that's often how it is. In some cases it's actually easier to loop through
your data, create a listitem just how you need it and add it to the
collection. Sometimes using the automatic way of doing things is more
trouble than it's worth as it would take you no more than 5 minutes to write
the code to add the items individually.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP- FrontPage

suzy said:
i thought .net was meant to make things easier, so why is this simple thing
taking me over 2 days.

i have a asp.net page with a checkboxlist control on it. This displays a
bunch of checkboxes. All i want to do is add an onclick event to each
checkbox that calls a javascript function passing in the id of the checkbox.
i could do this easily in classic asp so why is it such a bloody nightmare
in .net?!

to populate the checkboxlist control i call the .DataSource and ..DataBind
methods. So I am not doing any explicit looping. However, I have tried
looping through the checkboxlist elements after .DataBind() and manually
call .Attributes.Add it still doesn't work. (see below)

foreach (ListItem item in ((CheckBoxList)
chkList.FindControl("chkList")).Items)
{
item.Attributes.Add ("onclick",
string.Format("javascript:check_Click({0});", item.Value));
}

I don't know if this is the right way to go about things, but the code runs
without errors (and the lines of code are being called when i step through
the code). However, there is no sign of any onclick attriibute on the
client (even if i view source of the html page). it is nowhere in the page.

Someone please help! I am wasting so much time with this.

thank you so much.

:)
 
M

Mark Fitzpatrick

Can't. Only the DataGrid, DataList, and Repeater Controls have those methods
publically exposed. The CheckBoxList doesn't have these events available.

Mark Fitzpatrick
Microsoft MVP - FrontPage

Dan B said:
Use the ItemDataBound event or ItemCreated of the control to add
attributes !!!
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top