By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

asp.net makes me mad!

P: n/a
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.

:)
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Have you also tried the following way also?

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

CheckBoxList1.Items[i].Attributes.Add("onclick",string.Format("javascrip t: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[i].Text;
newItem.Value = CheckBoxList1.Items[i].Value;
newItem.Attributes.Add("onclick",string.Format("ja vascript:check_Click({0});
", newItem.Value));
newItem.Selected = CheckBoxList1.Items[i].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" <su**@spam.com> wrote in message
news:HJ******************@pathologist.blueyonder.n et...
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.

:)

Nov 18 '05 #2

P: n/a
Use the ItemDataBound event or ItemCreated of the control to add attributes !!!
Nov 18 '05 #3

P: n/a
http://support.microsoft.com/default...;en-us;Q309338

"suzy" <su**@spam.com> wrote in message
news:HJ******************@pathologist.blueyonder.n et...
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.

:)

Nov 18 '05 #4

P: n/a
oh great.... look what i've just found:

http://support.microsoft.com/default...;en-us;Q309338
"Mark Fitzpatrick" <ma******@fitzme.com> wrote in message
news:eM**************@tk2msftngp13.phx.gbl...
Have you also tried the following way also?

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

CheckBoxList1.Items[i].Attributes.Add("onclick",string.Format("javascrip t: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[i].Text;
newItem.Value = CheckBoxList1.Items[i].Value;
newItem.Attributes.Add("onclick",string.Format("ja vascript:check_Click({0}); ", newItem.Value));
newItem.Selected = CheckBoxList1.Items[i].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" <su**@spam.com> wrote in message
news:HJ******************@pathologist.blueyonder.n et...
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.

:)


Nov 18 '05 #5

P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
Use the ItemDataBound event or ItemCreated of the control to add

attributes !!!
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.