Solved

Unable to bind RadioButtonList within repeater.

Posted on 2004-07-30
4
750 Views
Last Modified: 2008-01-09
I have a repeater with a RadioButtonList.  I can bind the repeater, but I can't seem to bind the RadioButtonList.

Any suggestion???


Below is my code:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="VB" runat="server">
Dim SQL as String = ""
Dim connection = "..."
Dim dbConnection as SqlConnection
Dim dbCommand As SQLCommand
Dim dbReader as sqldatareader
Dim skillCount as Long
Dim employee as String
      
<%--
The following variable 'message' is used for code rended blocks or inline script.
This is used for example only.
 --%>
 Dim message as String
          
    Sub Page_Load(sender As Object, e As EventArgs)

      If not Page.IsPostBack Then
            '-- Do the following on initial page load.                   
            '-- Initial value for employeeId until it can be obtained by user logon
            employee = "441"
            BindGrid()
      End If
                                    
    End Sub
      
    Sub BindGrid()    
                  
      '-------------------------------------------------------------------------------
      ' Get the skills and proficiencies.
      '-------------------------------------------------------------------------------
                  
      '-- Open a database connection
      dbConnection.Open()
      '-- Return the count of recordset
      SQL = "SELECT COUNT(*)" _
            & " FROM dbo.tbl_rl_skill a, dbo.tbl_rl_proficiency b" _
            & " WHERE b.emp_id = " & employee _
            & " AND b.skill_id = a.skill_id"
      dbCommand = New SQLCommand(SQL, dbConnection)                        
      '-- Return SQL function value from the table
      skillCount = dbCommand.ExecuteScalar()            
      '-- Bind the control only if records are returned
      If dbCommand.ExecuteScalar() <> 0 Then            
            '-- Create and issue an SQL command through the database connection
            SQL = "SELECT a.skill, b.proficiency" _
                  & " FROM dbo.tbl_rl_skill a, dbo.tbl_rl_proficiency b" _
                  & " WHERE b.emp_id = " & employee _
                  & " AND b.skill_id = a.skill_id"
            dbCommand = New SQLCommand(SQL, dbConnection)                                    
            '-- Create a recordset of selected records from the database            
             dbReader = dbCommand.ExecuteReader()                                          
            '-- Bind the recordset to a control
            repeater1.DataSource = dbReader                  
            repeater1.DataBind()
            radioList1.DataSource = dbReader
            radioList1.DataValueField = "proficiency"
            radioList1.DataBind()      
      End If            
      '-- Close the reader and database connections
      dbReader.Close()
      dbConnection.Close()
                  
    End Sub
                   
</script>
<html>
<head>
</head>
<body>
<form runat="server">
 </p>      
<asp:repeater id="repeater1" runat="server">
      <HeaderTemplate>
      <table width="100%" style="font: 7pt verdana">
            <tr style="background-color:DFA894">
                  <th align="left">
                        Skill
                  </th>
                  <th>
                        Proficiency
                  </th>
            </tr>
      </HeaderTemplate>
      <ItemTemplate>
            <tr style="background-color:FFECD8">
                  <td align="left">
                        <%# DataBinder.Eval(Container.DataItem, "skill") %>
                  </td>
                  <td align="center">
                        <asp:RadioButtonList id="radioList1" runat="server" RepeatColumns="4">
                              <asp:ListItem Value="0" Text="" />
                              <asp:ListItem Value="1" Text="" />
                              <asp:ListItem Value="2" Text="" />
                              <asp:ListItem Value="3" Text="" />
                        </asp:RadioButtonList>                        
                  </td>                  
            </tr>
      </ItemTemplate>
      <FooterTemplate>
            <tr style="font-size:7pt">
                  <td align ="left" >
                        <%= skillCount & " skills" %>
                  </td>
                  <td align="right" colspan="4">
                        xxxxxl
                  </td>
            </tr>
      </table>
      </FooterTemplate>
</asp:repeater>
      
</form>
</body>
</html>
0
Comment
Question by:seckel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 11676717
I am not sure but after you have assigned a dbreader to repeater, it might loose its values. But I am not sure abt it. Declare a second dbReader get the data and bind it to the radio button list and see if it will work.

Best, Nauman
0
 

Author Comment

by:seckel
ID: 11676804
Ok, my binding now looks like this:

'-- Bind the recordset to a control
repeater1.DataSource = dbReader                  
repeater1.DataBind()
dbReader = dbCommand.ExecuteReader()      
radioList1.DataSource = dbReader
radioList1.DataValueField = "proficiency"
radioList1.DataBind()      

But I now get this message:
Compiler Error Message: BC30451: Name 'radioList1' is not declared.

My asp:RadioButtonList id="radioList1" is correct.  

Any suggestions??
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 11676965
Radio button create problem in the DataList or DataGrid as it cant be grouped even if you assign the group name. See the following post:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21003207.html

I will recommend you use GlobalRadioButton which is free and can be downloaded from the following URL:

http://metabuilders.com/Tools/GlobalRadioButton.aspx


-Nauman
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 11677205
It has to be done like so:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="RadioButtonInDataGrid.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
            <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" AutoGenerateColumns="False" Width="144px" Height="22px">
                        <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
                        <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
                        <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
                        <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
                        <Columns>
                              <asp:TemplateColumn HeaderText="Select">
                                    <ItemTemplate>
                                          <asp:Label id="Label2" runat="server"></asp:Label>
                                    </ItemTemplate>
                              </asp:TemplateColumn>
                              <asp:BoundColumn DataField="lastname" HeaderText="Last Name"></asp:BoundColumn>
                        </Columns>
                        <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
                  </asp:DataGrid>
                  <asp:Label id="Label3" style="Z-INDEX: 103; LEFT: 222px; POSITION: absolute; TOP: 35px" runat="server" Width="184px"></asp:Label>
                  <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 218px; POSITION: absolute; TOP: 70px" runat="server" Text="Display Selected Value"></asp:Button>
            </form>
      </body>
</HTML>

----------------------------------------------------------------------------------------------------------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            BindGrid()
        End If
    End Sub

    Sub BindGrid()
        Dim connstr As String = "Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=.\NetSDK"
        Dim cnn As SqlConnection
        Dim da As SqlDataAdapter
        Dim ds As New DataSet()
        cnn = New SqlConnection(connstr)
        da = New SqlDataAdapter("select * from employees", cnn)
        da.Fill(ds, "employees")
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
    End Sub

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            Dim r As Label
            r = e.Item.FindControl("Label2")
            r.Text = "<input type=radio name='myradiogroup' value=" & e.Item.Cells(1).Text & ">"
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label3.Text = Request.Form("myradiogroup")
        Dim i As DataGridItem
        For Each i In DataGrid1.Items
            If i.ItemType = ListItemType.AlternatingItem Or i.ItemType = ListItemType.Item Then
                Dim r As Label
                r = i.FindControl("Label2")
                If r.Text.IndexOf(Label3.Text) > 0 Then
                    r.Text = "<input type=radio name='myradiogroup' value=" & i.Cells(1).Text & " checked>"
                Else
                    r.Text = "<input type=radio name='myradiogroup' value=" & i.Cells(1).Text & ">"
                End If
            End If
        Next
    End Sub

Regards,

Aeros
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question