HtmlEncode RadioButtonList

C

Chris Walls

I have a need to HtmlEncode the DataTextField on a databound
RadioButtonList. I have been unable to find any events to subscribe to
(such as OnItemDataBound on the DataGrid) or any type of template control to
specify within the RadioButtonList control.

I have found MSDN security articles that talk about encoding the display for
these types of controls,
but then only provides examples for the DataGrid.

Any help would be appreciated.
 
S

Steven Cheng[MSFT]

Hi Chris,

Thanks for your posting. From your description, I think what're wondering
is how to htmlencode each Item's Text property in a asp.net radiobuttonlist
control, yes?

Yes, as you've found the RadioButtonList hasn't a ItemBoundEvent like the
DataGrid. This is because RadioButtonList(or DropDownlist..) is simple
databound control rather than a TemplateDataBound control( DataGrid ,
DataList or Repeater). And currently , I think one possible means is to do
the HtmlEncoding work in the RadioButtonList's PreRender event, we can
loopthrough the ListItems in the list and encode the eachone' Text
property. For example:

======================================
private void rblMain_PreRender(object sender, System.EventArgs e)
{
foreach(ListItem item in rblMain.Items)
{
item.Text = Server.HtmlEncode(item.Text);
}
}
======================================

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
C

Chris Walls

This worked just find for the RadioButtonList, but I cannot seem to get this
event to fire for the CheckBoxList. I have seen references to this being a
bug in the framework, but I didn't find anything "official" when I searched
for this on support.microsoft.com.

Is there something wrong with this event handler for the CheckBoxList? If
so, is there a work around?

Thanks,
Chris
 
S

Steven Cheng[MSFT]

Hi Chris,

Thanks for your testing and the response. Yes, I've also found the problem(
PreRender event not fire on CheckBoxList control) you mentioned. Currently
I'm consulting the dev guys to see whether this problem is addressed. Also
, we can use the following means to work around it:

#we do the encoding steps just after we do the databinding for the
checkboxlist:

private void Page_Load(object sender, System.EventArgs e)
{
string[] items = {"<font size='5'>aaa</font>","<font
size='15'>bbb</font>","<font size='25'>ccc</font>","<font
size='35'>ddd</font>"};

if(!IsPostBack)
{
lstMain.DataSource = items;
lstMain.DataBind();

foreach(ListItem item in lstMain.Items)
{
item.Text = Server.HtmlEncode(item.Text);
item.Value = Server.HtmlEncode(item.Value);
}

}
}

And I'll update you when I got some info on the checkboxlist's prerender
issue. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Chris,

After some further research, the CheckBoxList control does has the problem
on PreRender event and it is addressed. And currently we can do the
HtmlEncoding right after we call the CheckBoxList's DataBind method to
workaround this. Also, I'm sorry for any inconvenience it brings you.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top