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

P

psb

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.
 
M

Martin Dechev

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top