radio button inside datagrid


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: radio button inside datagrid

  1. #1
    Balaji N.S Guest

    radio button inside datagrid

    Hi



    I have used radio button inside datagrid. It is allowing me to select all
    the radio buttons. Actually only one radio button should be selected at a
    time, on selecting other radio button, previous radio button should be
    unchecked. I am not able to call onclick event of that radio button.



    Hope some one would already solved this kind of problem.



    Thanks



    Balaji




  2. #2
    Join Date
    Sep 2005
    Posts
    1

    Thumbs up Radio button as template column in datagird

    -----This is Datagrid with a button just copy this into u r aspx page----

    <asp:datagrid id="grdReport" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 200px"
    runat="server" Width="144px" BackColor="White" AutoGenerateColumns="False" CellPadding="4"
    Height="22px" BorderWidth="1px" BorderStyle="None" BorderColor="#CC9966">
    <Columns>
    <asp:BoundColumn DataField="type" SortExpression="type" HeaderText="Type"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Select">
    <ItemTemplate>
    <asp:RadioButton id="rdbBatchTime" runat="server" AutoPostBack="True" OnCheckedChanged="SelectOnlyOne"></asp:RadioButton>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
    <asp:button id="Button1" style="Z-INDEX: 102; LEFT: 336px; POSITION: absolute; TOP: 40px" runat="server"
    Text="Display Selected Value"></asp:button>

    -----This is Code Bloack just copy this into u r code page , then see the result and chage it to ur needs----

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim m_ClientID As String = ""
    Dim rb As RadioButton

    Dim j As Integer = 1
    For Each i As DataGridItem In grdReport.Items
    rb = CType(i.FindControl("rdbBatchTime"), RadioButton)
    If rb.Checked = True Then
    Response.Write(j)
    End If
    j += 1
    Next

    End Sub
    Public Sub SelectOnlyOne(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim m_ClientID As String = ""
    Dim rb As New RadioButton

    rb = CType(sender, RadioButton)
    m_ClientID = rb.ClientID

    For Each i As DataGridItem In grdReport.Items
    rb = CType(i.FindControl("rdbBatchTime"), RadioButton)
    rb.Checked = False
    If (m_ClientID = rb.ClientID) Then
    rb.Checked = True
    End If
    Next

    End Sub

  3. #3
    Join Date
    Dec 2004
    Posts
    717
    Hi Balaji,
    You would say it's better if you use JavaScript to check & unchecked option button of datagrid...

    Here is very good samples for datagrid with Radio Buttons.....
    Handling RadioButtons on DataGrid : By Hernan Molina


    P.S :
    Using the datagrid with checked boxes [CheckAll] or option boxes, I think it make to redure the performance if many records over 50,000 are displayed in the datagrid..
    I also posted one thread : DataGrid with Checkedbox/Radio button
    But I haven't recieved any reply yet. I'm still finding the better way to reduce the loading time..
    Last edited by Sync; 02-18-2007 at 12:03 AM.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  4. #4
    Join Date
    Feb 2007
    Posts
    2
    Using Radio buttons to select each row in datagrid
    =====================================

    Using ASP.net to handle radio button would be slow or very slow if datagrid has much data to display because ASP .net triggers "evey" event of any server side control by reloaing the page while posting some data back to it, which definitely reduce performance.

    So I suggest you to choose javascript for this in collaboration with ASP .net, infact ASP .net translates most of its commands into javascript.

    To have radio buttons to select each row inside the datagrid follow the following easy steps (100% tested, if found any problem post here):

    .ASPX File:
    ========

    In aspx page add the following javascript function

    <script language=javascript>
    function SelectMeOnly(objRadioButton, grdName) {
    var i, obj;
    //example of radio button id inside the grid (grdAddress): grdAddress__ctl2_radioSelect
    for (i=0; i<document.all.length; i++) {
    obj = document.all(i);
    if (obj.type == "radio") {
    if (objRadioButton.id.substr(0, grdName.length) == grdName)
    if (objRadioButton.id == obj.id)
    obj.checked = true;
    else
    obj.checked = false;
    }
    }
    }
    </script>

    Your Radio Button ASP .net Control inside the data grid would look like this:

    <asp:RadioButton ID="radioSelect" GroupName='<%#DataBinder.Eval(Container.DataItem,"RecID")%>' Runat=server></asp:RadioButton>

    Here I used GroupName property to just Identify the row no in Database, where datagrid is bound to any datasource in page load event. RecID in this case is the Primary Key in the table holding unique RecID for each row in the table. You can replace this with your table's field name, or remove it at all if you donot want to record the row number for each radio button. You can also use the Text property to set text for the radio button.

    Thats all concerned in aspx page.

    .CS File
    ======

    Here Make the following function and call it in Page Load event, this function would set the OnClick event of each RadioButton inside the data grid, let say your data grid name is "grdDataGrid":

    public void SetGrdRadiosOnClick()
    {
    int i;
    RadioButton B;

    for (i = 0; i<grdDataGrid.Items.Count; i++)
    {
    B = (RadioButton) grdDataGrid.Items[i].FindControl("radioSelect");
    B.Attributes.Add("OnClick", "SelectMeOnly(" + B.ClientID + ", "+ "'grdDataGrid'" + ")");
    }
    }

    You got it, Thats all it need to have radio button functionality to select rows in Datagrid and in fastest way.


    Further, if you want to get which radio is selected and at which row then you cn use the following function, let say you have a button name "btnSelect" outside the datagrid to process the selected row in datagrid. (Remember we set the Unique Row ID of database in GroupName property of each radio button):
    GridName is "grdDataGrid"
    RadioButton inside the grid was "radioSelect" which is repeated for each row

    public void btnSelect_Click(object sender, System.EventArgs e)
    {
    RadioButton B;
    int i, RecID=0;
    for (i = 0; i<grdDataGrid.Items.Count; i++)
    {
    B = (RadioButton) grdDataGrid.Items[i].FindControl("radioSelect");
    if (B.Checked)
    RecID = int.Parse(B.GroupName);
    }

    Response.Redirect("./ProcessRecord.aspx?RecID=" + RecID);
    }

    God Bless You.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center