RadioButtonList and Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dighazuse@msn.com

    RadioButtonList and Javascript

    trying to get a javascript attached to a DataGrid RadioButtonList . I
    have a similar thing working for a DDL so i copied that for the RBL.
    It "errors" when i am trying to get the current selected value for the
    RBL.


    code in ItemDataBound:

    rblDC.Attribute s.Add("onClick" , "rblDC_onClick( '" + lblDC.ClientID +
    "', '" + rblDCodes.Clien tID + "', '" + e.Item.ItemType + "')");

    Javascript:

    function rblDC_onClick(l blDCId, rblDCId, eiT)
    {
    var lblDC = document.getEle mentById(lblDCI d);
    var rblDC = document.getEle mentById(rblDCI d);
    var rblDCSelection = rblDC.options[rblDC.selectedI ndex].value); <--
    JS error
    if (lblDC.innerTex t == rblDCSelection)
    "do stuff"
    }

    the error i get says:
    "options" is null or is not an object.
    which, to noob me, says that it doesn't have a valid rblDC. not sure
    though. could be a syntax error of sorts.

    thank you
    troy

  • Bruce Barker

    #2
    Re: RadioButtonList and Javascript

    a radio button list renders as a table of ID=ClientID, with an <input
    type=radio> for each radio button. the buttons are named ClientID, and given
    id of ClientID + "_" + index.

    so adding a onclick is to the table and tables do not have options, thus
    your error.



    you need onclick event for each radio button, and check checked property.


    try

    function rblDC_onClick(l blDCId, rblDCId, eiT)
    {
    var lblDC = document.getEle mentById(lblDCI d);
    var rblDCs = document.getEle mentsByName(rbl DCId);
    var rblDCSelection = "";
    for (var i in rblDCs)
    {
    if (rblDCs[i].checked)
    {
    rblDCSelection = rblDCs[i].value;
    break;
    }
    }
    if (lblDC.innerTex t == rblDCSelection)
    "do stuff"
    }

    -- bruce (sqlwork.com)


    <dighazuse@msn. com> wrote in message
    news:1132001274 .902320.83020@o 13g2000cwo.goog legroups.com...[color=blue]
    > trying to get a javascript attached to a DataGrid RadioButtonList . I
    > have a similar thing working for a DDL so i copied that for the RBL.
    > It "errors" when i am trying to get the current selected value for the
    > RBL.
    >
    >
    > code in ItemDataBound:
    >
    > rblDC.Attribute s.Add("onClick" , "rblDC_onClick( '" + lblDC.ClientID +
    > "', '" + rblDCodes.Clien tID + "', '" + e.Item.ItemType + "')");
    >
    > Javascript:
    >
    > function rblDC_onClick(l blDCId, rblDCId, eiT)
    > {
    > var lblDC = document.getEle mentById(lblDCI d);
    > var rblDC = document.getEle mentById(rblDCI d);
    > var rblDCSelection = rblDC.options[rblDC.selectedI ndex].value); <--
    > JS error
    > if (lblDC.innerTex t == rblDCSelection)
    > "do stuff"
    > }
    >
    > the error i get says:
    > "options" is null or is not an object.
    > which, to noob me, says that it doesn't have a valid rblDC. not sure
    > though. could be a syntax error of sorts.
    >
    > thank you
    > troy
    >[/color]


    Comment

    • dighazuse@msn.com

      #3
      Re: RadioButtonList and Javascript

      thank you for replying Bruce:
      perhaps i am misunderstandin g but I am adding the Attribute for each
      item/row on the DataGrid at ItemDataBound time.

      If I do need to check the index, it then confuses me why the exact same
      code works perfectly for my DDL, where I don't have to check the index.

      Troy

      Comment

      • Bruce Barker

        #4
        Re: RadioButtonList and Javascript

        the drop drown renders as a <select>. in the browser dom a select has an
        options array, one for each option caontained in the select. a option has a
        value and text propery (also a selected which indicates if this is the
        seledted item)

        there is no radio list in the browser, asp.net just renders a table with
        radio buttons. in the browser, if the radio buttons have the same name, they
        are treated as exclusive. their is id need to be unique. here is a simple
        radio list:

        <asp:RadioButto nList id="myList" runat="server">
        <asp:ListItem Value="1">optio n 1</asp:ListItem>
        <asp:ListItem Value="2">optio n 2</asp:ListItem>
        </asp:RadioButton List>

        will render:

        <table id="myList" border="0">
        <tr>
        <td>
        <input id="myList_0" type="radio" name="myList" value="1" />
        <label for="myList_0"> option 1</label>
        </td>
        </tr>
        <tr>
        <td>
        <input id="myList_1" type="radio" name="myList" value="2" />
        <label for="myList_1"> option 2</label>
        </td>
        </tr>
        </table>

        to get a collection of the radio buttons, you get all form inputs with the
        name myList.

        document.getEle mentsByName("my List");

        to figure out the selected one, you look looking for a checked one.

        note: if you want to write client script, you should study the html dom.

        -- bruce (sqlwork.com)




        <dighazuse@msn. com> wrote in message
        news:1132003638 .653313.282720@ g43g2000cwa.goo glegroups.com.. .[color=blue]
        > thank you for replying Bruce:
        > perhaps i am misunderstandin g but I am adding the Attribute for each
        > item/row on the DataGrid at ItemDataBound time.
        >
        > If I do need to check the index, it then confuses me why the exact same
        > code works perfectly for my DDL, where I don't have to check the index.
        >
        > Troy
        >[/color]


        Comment

        • dighazuse@msn.com

          #5
          Re: RadioButtonList and Javascript

          thank you again bruce.

          being new at this I was not aware that a RBL isn't really 1 element but
          a group of individual elements. good to know. I see why the DDL is
          different.

          So, your code suggestion now, i believe, makes sense.
          To go over it again then:
          - the code:
          var rblDCs = document.getEle mentsByName(rbl DCId);

          is returning the names of the elements of the table (rblDC_0, rblDC_1
          .... )

          - the code:
          for (i in rblDCs)

          is spinning through those names trying to figure out which one has
          been checked.
          correct?

          however, unfortunately there has to be a however, the code to get the
          elementsbyname doesn't seem to be returning the names .. but something
          else like attributes of the table and such (like "length").

          is there a step that I am missing?

          thank you
          troy

          Comment

          • dighazuse@msn.com

            #6
            Re: RadioButtonList and Javascript

            also, I noticed that if the names of the elements are not the same.
            The table name is
            itg__ctl3_rblDC
            and the names of the individual RBL elements are
            itg:_ctl3:rbldc

            Comment

            Working...