asp.net makes me mad!

Discussion in 'ASP .Net' started by suzy, May 9, 2004.

  1. suzy

    suzy Guest

    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.

    :)
    suzy, May 9, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:HJqnc.17594$...
    > 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.
    >
    > :)
    >
    >
    Mark Fitzpatrick, May 9, 2004
    #2
    1. Advertising

  3. Use the ItemDataBound event or ItemCreated of the control to add attributes !!!
    =?Utf-8?B?RGFuIEI=?=, May 9, 2004
    #3
  4. suzy

    Andy Gaskell Guest

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

    "suzy" <> wrote in message
    news:HJqnc.17594$...
    > 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.
    >
    > :)
    >
    >
    Andy Gaskell, May 9, 2004
    #4
  5. suzy

    suzy Guest

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

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


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

    >
    >
    suzy, May 9, 2004
    #5
  6. 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" <> wrote in message
    news:...
    > Use the ItemDataBound event or ItemCreated of the control to add

    attributes !!!
    Mark Fitzpatrick, May 9, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Haider Abbas Y. Narsinh

    Re: I m gonna mad

    Haider Abbas Y. Narsinh, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,004
    Haider Abbas Y. Narsinh
    Aug 22, 2003
  2. SEWilson

    Re: I m gonna mad

    SEWilson, Aug 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,327
    SEWilson
    Aug 23, 2003
  3. Toby Considine

    Re: I m gonna mad Plz someone help me

    Toby Considine, Aug 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,248
    Toby Considine
    Aug 31, 2003
  4. Paul King

    ASP_WP driving me mad...

    Paul King, Sep 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    477
    Paul King
    Sep 24, 2004
  5. Mats

    Has Windows, ASP or Access gone mad?

    Mats, Aug 5, 2003, in forum: ASP General
    Replies:
    4
    Views:
    142
    Shailesh Humbad
    Aug 5, 2003
Loading...

Share This Page