Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > asp.net makes me mad!

Reply
Thread Tools

asp.net makes me mad!

 
 
suzy
Guest
Posts: n/a
 
      05-09-2004
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.




 
Reply With Quote
 
 
 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      05-09-2004
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" <(E-Mail Removed)> wrote in message
news:HJqnc.17594$(E-Mail Removed) 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.
>
>
>
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGFuIEI=?=
Guest
Posts: n/a
 
      05-09-2004
Use the ItemDataBound event or ItemCreated of the control to add attributes !!!
 
Reply With Quote
 
Andy Gaskell
Guest
Posts: n/a
 
      05-09-2004
http://support.microsoft.com/default...;en-us;Q309338

"suzy" <(E-Mail Removed)> wrote in message
news:HJqnc.17594$(E-Mail Removed) 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.
>
>
>
>



 
Reply With Quote
 
suzy
Guest
Posts: n/a
 
      05-09-2004
oh great.... look what i've just found:

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


"Mark Fitzpatrick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:HJqnc.17594$(E-Mail Removed) 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.
> >
> >
> >
> >

>
>



 
Reply With Quote
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      05-09-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Use the ItemDataBound event or ItemCreated of the control to add

attributes !!!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Press Release - Visual CertExam Suite Makes It Easy to Create and Take Professional Practice Tests David Johnson MCSD 0 10-19-2004 04:02 PM
OT: IE w/ SP2 -- makes me laugh Brian Firefox 2 08-27-2004 02:41 AM
Firebird: Clicking mouse wheel on xml page makes window go blank. Quivis Firefox 4 12-30-2003 09:14 PM
OT Makes me sick Kline Sphere MCSE 4 12-19-2003 07:39 PM
OT: What makes Tron2003 tick =?iso-8859-1?Q?Frisbee=AE_MCNGP?= MCSE 21 07-23-2003 05:20 PM



Advertisments