Sunday, December 11, 2011

radiobutton inside datalist itemTemplate

aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="radioButtonInListView.aspx.cs" Inherits="WebApplication1.radioButtonInListView" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function resetname(senderid) {
            var radio = document.getElementById(senderid);
            radio.name = 'myoption';
         }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" DataKeyField="NationalityID" 
            DataSourceID="SqlDataSource1" onitemdatabound="DataList1_ItemDataBound">
            <ItemTemplate>
                Nationality:
                <asp:Label ID="NationalityLabel" runat="server" 
                    Text='<%# Eval("Nationality") %>' />
                <br />
                <br />
                <asp:RadioButton ID="RadioButton1" runat="server"/>
            </ItemTemplate>
        </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TrustDBDevConnectionString %>" 
            SelectCommand="SELECT * FROM [Nationality]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>
 
C# :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
   
public partial class radioButtonInListView : System.Web.UI.Page
   
{
       
protected void Page_Load(object sender, EventArgs e)
       
{

       
}

       
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
       
{
           
RadioButton r = (RadioButton)e.Item.FindControl("RadioButton1");
            r
.Attributes.Add("onclick", "resetname('" + r.ClientID + "')");
       
}
   
}
}