Adding an OnClick Event to a RadioButtonList ListItem
The ASP.NET RadioButtonList does not provide a declarative way to attach the onclick attribute to a ListItem. However, it can be done using the OnDataBound event handler. This is useful in a scenario where you have a TextBox that you would like to populate with the user’s RadioButtonList SelectedValue. In this example, I’m using the ASP.NET AJAX ModalPopupExtender which contains the RadioButtonList.
.aspx
I’m populating RadioButtonList with a list of currently visible pages in the CMS application this code resides in. The ModalPopup is triggered by a LinkButton (lnkBrowseUrls).
<asp:Panel ID="pnlUrlFinder" runat="server" CssClass="popupBox">
<div>
<asp:SqlDataSource
ID="sqlPages"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM pages WHERE page_visible=1">
</asp:SqlDataSource>
<asp:RadioButtonList
runat="server"
ID="radioUrls"
DataSourceID="sqlPages"
DataTextField="page_title"
DataValueField="page_id"
OnDataBound="radioUrls_OnDataBound">
</asp:RadioButtonList>
</div>
<asp:LinkButton ID="lnkCloseUrlFinder" runat="server" Text="Close" />
</asp:Panel>
<cc1:ModalPopupExtender
ID="ModalPopupExtender1"
runat="server"
TargetControlID="lnkBrowseUrls"
PopupControlID="pnlUrlFinder"
OkControlID="radioUrls"
CancelControlID="lnkCloseUrlFinder">
</cc1:ModalPopupExtender>
.aspx.cs
The OnDataBound event handler creates the onclick event on each ListItem which populates a TextBox with the path to the given page. (I’m using Ionic’s ISAPI Rewrite Filter to rewrite url’s so they are “Search Engine Friendly”.)
protected void radioUrls_OnDataBound(object sender, EventArgs e)
{
RadioButtonList rbl = (RadioButtonList)sender;
foreach (ListItem li in rbl.Items)
{
string s = "document.getElementById('" + txtUrl.ClientID + "').value = '" + Request.ApplicationPath + "/page/view/" + li.Value + "/'";
li.Attributes.Add("onclick", s);
}
}
Adding an Onclick Event to a RadioButtonList ListItem…
You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…
Trackback on November 22, 2006 @ 4:30 pm
OnClick für RadioButtonList…
Wer eine RadioButtonList in ASP.NET per JavaScript ansteuern möchte, benötigt für die einzelnen Radiobuttons und die zugehörigen Labels ein onclick-Event. Einen guten Tipp wie das zu bewerkstelligen geht, gibt Shane Shepherd. Allerdings wird hier s…
Trackback on January 11, 2007 @ 5:54 am
woriking with radiobtn list, and i want to check the radiobtn values in modalpopup.
Comment on May 22, 2008 @ 6:59 am
hi guys
i have 2 radio buttons. and one button(select). and if i click the button.A modalpopupextendor should open. and radio button values should appear in the spgridview.
thanks
vardhan
Comment on May 22, 2008 @ 7:26 am
What is “txtUrl.ClientID” ??
Comment on May 12, 2010 @ 5:53 pm