adding attributes to radio button list <asp:radiobuttonlist .../> don't work?

Discussion in 'ASP .Net' started by psb, Sep 21, 2004.

  1. psb

    psb Guest

    hello, I have a radio button list inside of a datalist... after finding the
    radio button list, I do another lookup to populate the radio button list. I
    have just made it to where the radio button list could affect price
    depending on the option selected... I want to add a JavaScript:OnClick event
    when the radio button that affects the price is clicked to pop an alert()
    stating, "this will affect the price".

    ----------------------------------------
    radList = CType(dlstProducts.Items(i).FindControl("radList"),
    RadioButtonList)

    radList.DataSource = objDataTable
    radList.DataTextField = "OptionText"
    radList.DataValueField = "ID"
    radList.DataBind()

    For j = 0 To objDataTable.Rows.Count - 1
    If CLng(objDataTable.Rows(j)("PriceIncreasePercent")) <> 0 Then
    lngPriceIncrease = CLng(objDataTable.Rows(j)("PriceIncreasePercent"))
    radList.Items(j).Attributes.Add("onclick", "alert('increase price " &
    lngPriceIncrease & "%');")
    End If
    Next
    ----------------------------------

    The radio buttons get output perfectly, but the attribute to add the onclick
    event is not there. it isn't even in the "View Source" of the document.
    can attributes not be added to radio button list items?? I have also tried
    building the radio button list manually with [[ radList.Items.Add(new
    listitem("","")) ]] inside the for loop and same thing happens. I can't
    belive this if it is true.
    psb, Sep 21, 2004
    #1
    1. Advertising

  2. Hi,

    The problem is that the RadioButton's themselves get created at the Render
    time and get rendered to the output directly - you have no control over
    them. This is by design. The System.Web.UI.WebControls.RadioButtonList
    implements the interface System.Web.UI.WebControls.IRepeatInfoUser which in
    fact controls the creation of the list. The method that adds the
    RadioButtons to the list is called RenderItem (see
    http://msdn.microsoft.com/library/e...ntrolsirepeatinfouserclassrenderitemtopic.asp )
    .. In the System.Web.UI.WebControls.RadioButtonList implementation it looks
    more or less like this:

    Sub RenderItem( _
    itemType As System.Web.UI.WebControls.ListItemType, _
    repeatIndex As Integer,
    repeatInfo As System.Web.UI.WebControls.RepeatInfo,
    writer As System.Web.UI.HtmlTextWriter)

    Dim dummy as New RadioButton()

    'Set the text
    'Set the value, id, etc.
    'Set if checked
    'Set if it should AutoPostBack
    'Set Enabled, TabIndex etc.

    dummy.RenderControl(writer)

    End Sub

    In this situation your only option is to write your own RadioButtonList
    implementation in which you can rewrite this method or even render the list
    by yourself - thus you'll control everything.

    Greetings
    Martin
    "psb" <> wrote in message
    news:...
    > hello, I have a radio button list inside of a datalist... after finding

    the
    > radio button list, I do another lookup to populate the radio button list.

    I
    > have just made it to where the radio button list could affect price
    > depending on the option selected... I want to add a JavaScript:OnClick

    event
    > when the radio button that affects the price is clicked to pop an alert()
    > stating, "this will affect the price".
    >
    > ----------------------------------------
    > radList = CType(dlstProducts.Items(i).FindControl("radList"),
    > RadioButtonList)
    >
    > radList.DataSource = objDataTable
    > radList.DataTextField = "OptionText"
    > radList.DataValueField = "ID"
    > radList.DataBind()
    >
    > For j = 0 To objDataTable.Rows.Count - 1
    > If CLng(objDataTable.Rows(j)("PriceIncreasePercent")) <> 0 Then
    > lngPriceIncrease = CLng(objDataTable.Rows(j)("PriceIncreasePercent"))
    > radList.Items(j).Attributes.Add("onclick", "alert('increase price " &
    > lngPriceIncrease & "%');")
    > End If
    > Next
    > ----------------------------------
    >
    > The radio buttons get output perfectly, but the attribute to add the

    onclick
    > event is not there. it isn't even in the "View Source" of the document.
    > can attributes not be added to radio button list items?? I have also

    tried
    > building the radio button list manually with [[ radList.Items.Add(new
    > listitem("","")) ]] inside the for loop and same thing happens. I can't
    > belive this if it is true.
    >
    >
    Martin Dechev, Sep 22, 2004
    #2
    1. Advertising

  3. psb

    Achintya Jha Guest

    Hi,
    There is a bug in .NET.
    Adding ListItems don't work with CheckboxList,RadiobuttonList etc.

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


    try using the old ASP method.....

    "psb" <> wrote in message news:<>...
    > hello, I have a radio button list inside of a datalist... after finding the
    > radio button list, I do another lookup to populate the radio button list. I
    > have just made it to where the radio button list could affect price
    > depending on the option selected... I want to add a JavaScript:OnClick event
    > when the radio button that affects the price is clicked to pop an alert()
    > stating, "this will affect the price".
    >
    > ----------------------------------------
    > radList = CType(dlstProducts.Items(i).FindControl("radList"),
    > RadioButtonList)
    >
    > radList.DataSource = objDataTable
    > radList.DataTextField = "OptionText"
    > radList.DataValueField = "ID"
    > radList.DataBind()
    >
    > For j = 0 To objDataTable.Rows.Count - 1
    > If CLng(objDataTable.Rows(j)("PriceIncreasePercent")) <> 0 Then
    > lngPriceIncrease = CLng(objDataTable.Rows(j)("PriceIncreasePercent"))
    > radList.Items(j).Attributes.Add("onclick", "alert('increase price " &
    > lngPriceIncrease & "%');")
    > End If
    > Next
    > ----------------------------------
    >
    > The radio buttons get output perfectly, but the attribute to add the onclick
    > event is not there. it isn't even in the "View Source" of the document.
    > can attributes not be added to radio button list items?? I have also tried
    > building the radio button list manually with [[ radList.Items.Add(new
    > listitem("","")) ]] inside the for loop and same thing happens. I can't
    > belive this if it is true.
    Achintya Jha, Sep 22, 2004
    #3
    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. khawar
    Replies:
    0
    Views:
    815
    khawar
    Aug 7, 2003
  2. headware
    Replies:
    3
    Views:
    4,501
  3. Guest
    Replies:
    1
    Views:
    5,296
    alex bowers
    Nov 18, 2003
  4. darrel
    Replies:
    2
    Views:
    689
    Bruce Barker
    Mar 24, 2005
  5. Hiten
    Replies:
    1
    Views:
    599
    Vidar Petursson
    May 26, 2004
Loading...

Share This Page