![]() |
asp.net makes me mad!
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. :) |
Re: asp.net makes me mad!
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" <suzy@spam.com> wrote in message news:HJqnc.17594$dD5.6148@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. > > :) > > |
Re: asp.net makes me mad!
Use the ItemDataBound event or ItemCreated of the control to add attributes !!!
|
Re: asp.net makes me mad!
http://support.microsoft.com/default...;en-us;Q309338
"suzy" <suzy@spam.com> wrote in message news:HJqnc.17594$dD5.6148@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. > > :) > > |
Re: asp.net makes me mad!
oh great.... look what i've just found:
http://support.microsoft.com/default...;en-us;Q309338 "Mark Fitzpatrick" <markfitz@fitzme.com> wrote in message news:eMxf7EdNEHA.1032@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" <suzy@spam.com> wrote in message > news:HJqnc.17594$dD5.6148@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. > > > > :) > > > > > > |
Re: asp.net makes me mad!
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" <anonymous@discussions.microsoft.com> wrote in message news:45C83D6A-7680-4B8F-B8EB-37223F27668A@microsoft.com... > Use the ItemDataBound event or ItemCreated of the control to add attributes !!! |
| All times are GMT. The time now is 11:16 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.