Solved

radio button list in datagrid

Posted on 2009-07-01
6
626 Views
Last Modified: 2012-05-07
I am trying to create a very simple webpage in VS.NET 2003, which has a data grid and a radio button list within this data grid.
I load the page
Then I select either .Yes .No or .N/A in the radio button
Then I am trying to call a procedure to calculate something like a score to be displayed based on what they selected in the radiobuttonlist. this score is displayed on the same page after i hit a submit button
For some reason after I select either .Yes .No or .N/A in the radio button, the rbYN.Items (0).Selected is always False.
Do I need to do some property in HTML like a select index or something? If yes please give an example or syntax
 
These values YES NO or N/A is not data bound, in the sense they dont come from database.
How do I set some property to fetch the value that was selected by user? In other words I want rbYN.Items (0).Selected to be true when that was selected by the user
 
Please help , hope this questions makes sense.
Thanks in advance

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Survey.aspx.vb" Inherits="CustomerService.WebForm1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

            <HEAD>

                        <title>WebForm1</title>

                        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

                        <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">

                        <meta content="JavaScript" name="vs_defaultClientScript">

                        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

            </HEAD>

            <body MS_POSITIONING="GridLayout">

                        </FORM></COLUMNS></ASPATDAGRID>

                        <form id="Form1" method="post" runat="server">

                                    <asp:datagrid id="dgSurvey" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 384px" runat="server"

                                                Width="744px" Height="50px" BorderWidth="1px" CellPadding="2" AutoGenerateColumns="False"

                                                Font-Size="10pt" Font-Names="Arial" PageSize="20">

                                                <FooterStyle Wrap="False"></FooterStyle>

                                                <AlternatingItemStyle BackColor="White"></AlternatingItemStyle>

                                                <ItemStyle BackColor="#F7F7DE"></ItemStyle>

                                                <HeaderStyle ForeColor="White" BackColor="#6B696B"></HeaderStyle>

                                                <Columns>

                                                            <asp:TemplateColumn HeaderText="Question">

                                                                        <HeaderStyle Wrap="False"></HeaderStyle>

                                                                        <ItemStyle Wrap="False"></ItemStyle>

                                                                        <ItemTemplate>

                                                                                    <asp:Label id=lblQName runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.QLongName") %>'>

                                                                                    </asp:Label>

                                                                        </ItemTemplate>

                                                                        <FooterStyle Wrap="False"></FooterStyle>

                                                            </asp:TemplateColumn>

                                                            <asp:TemplateColumn HeaderText="Answer">

                                                                        <ItemTemplate>

                                                                                    <asp:RadioButtonList id="rbYN" runat="server" RepeatDirection="Horizontal">

                                                                                                <asp:ListItem Value="Yes">Yes</asp:ListItem>

                                                                                                <asp:ListItem Value="No">No</asp:ListItem>

                                                                                                <asp:ListItem Value="N/A">N/A</asp:ListItem>

                                                                                    </asp:RadioButtonList>

                                                                        </ItemTemplate>

                                                            </asp:TemplateColumn>

                                                </Columns>

                                    </asp:datagrid><asp:label id="lblAgent" style="Z-INDEX: 113; LEFT: 392px; POSITION: absolute; TOP: 144px"

                                                runat="server" Font-Italic="True">Agent:</asp:label><asp:dropdownlist id="ddlAgentName" style="Z-INDEX: 102; LEFT: 392px; POSITION: absolute; TOP: 168px"

                                                runat="server" Width="312px" Font-Size="12px" AutoPostBack="True"></asp:dropdownlist><asp:dropdownlist id="ddlCostCtr" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 168px"

                                                runat="server" Width="360px" Font-Size="12px" AutoPostBack="True"></asp:dropdownlist><asp:textbox id="tbPossPoints" style="Z-INDEX: 104; LEFT: 584px; POSITION: absolute; TOP: 312px"

                                                runat="server" Width="72px" Enabled="False" Visible="False"></asp:textbox><asp:label id="lblPossiblePoints" style="Z-INDEX: 105; LEFT: 584px; POSITION: absolute; TOP: 280px"

                                                runat="server" Visible="False">Possible Points</asp:label><asp:textbox id="tbScore" style="Z-INDEX: 106; LEFT: 496px; POSITION: absolute; TOP: 312px" runat="server"

                                                Width="64px" Enabled="False" Visible="False"></asp:textbox><asp:label id="lblScore" style="Z-INDEX: 107; LEFT: 496px; POSITION: absolute; TOP: 280px"

                                                runat="server" Visible="False">Score</asp:label><asp:label id="lblPercentage" style="Z-INDEX: 108; LEFT: 680px; POSITION: absolute; TOP: 312px"

                                                runat="server" Visible="False">PercentageScore</asp:label><asp:label id="lblSubmitSurvey" style="Z-INDEX: 109; LEFT: 496px; POSITION: absolute; TOP: 352px"

                                                runat="server" Font-Size="X-Small" Visible="False" ForeColor="Red">Survey Successfully Submitted</asp:label><asp:textbox id="tbComments" style="Z-INDEX: 110; LEFT: 16px; POSITION: absolute; TOP: 240px"

                                                runat="server" Width="440px" Height="128px" Visible="False" MaxLength="250"></asp:textbox><asp:label id="lblComments" style="Z-INDEX: 111; LEFT: 24px; POSITION: absolute; TOP: 208px"

                                                runat="server" Visible="False">Comments:</asp:label><asp:label id="lblCostCtr" style="Z-INDEX: 112; LEFT: 16px; POSITION: absolute; TOP: 144px"

                                                runat="server" Font-Italic="True">Cost Center and Lawcode:</asp:label><asp:button id="btnSubmit" style="Z-INDEX: 114; LEFT: 496px; POSITION: absolute; TOP: 248px"

                                                runat="server" Width="248px" Visible="False" Text="This survey is ready to submit"></asp:button><asp:button id="btnContinue" style="Z-INDEX: 115; LEFT: 496px; POSITION: absolute; TOP: 208px"

                                                runat="server" Width="248px" Text="Calculate and Continue"></asp:button>

                                    <asp:Button id="Button1" style="Z-INDEX: 116; LEFT: 288px; POSITION: absolute; TOP: 88px" runat="server"

                                                Text="Button"></asp:Button></form>

            </body>

</HTML>

Open in new window

0
Comment
Question by:florida73
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 24761593
Can we also see the code behind logic ?  Like how ur accessing the radio button list and try to get the value ?
0
 
LVL 1

Author Comment

by:florida73
ID: 24762116
This is the code for the procedure that gets called after user selects yes/no/NA in the radio button inside the datagrid and then clicks a button Calculate, am I missing something inbetween the radio button click and calling my procedure? since the radio button values wont be stored in the database.


Private Sub CalculateScore()

        Dim QID, QValue, TotalScore, PossibleScore As Integer

        Dim sSQL, QLngNam As String

        Dim cn As New SqlClient.SqlConnection(CustomerService)

        Dim cmd As New SqlClient.SqlCommand(sSQL, cn)

        Dim drSQL As SqlClient.SqlDataReader

        Dim cmdSQL As New SqlClient.SqlCommand

 

        For Each i As DataGridItem In dgSurvey.Items

            Dim QName As Label = i.FindControl("lblQName")

            Dim rbYN As RadioButtonList = i.FindControl("rbYN")

            Dim QScore As Integer

            'Get the value of the question from database

            cn.Open()

            cmdSQL.Connection = cn

            sSQL = " Select QID, Value from Questions where QLongName = '" & QName.Text & "'"

            cmdSQL.CommandText = sSQL

            cmdSQL.CommandTimeout = 7200

            drSQL = cmdSQL.ExecuteReader

 

            Do Until drSQL.Read = False

                QID = drSQL.GetInt32(0)                 'Get QuestionID

                QValue = drSQL.GetInt32(1)              'Get value of question

            Loop

            If rbYN.Items(0).Selected = True Then             'Answer is Yes

                QScore = QValue

                TotalScore = TotalScore + QValue

                PossibleScore = PossibleScore + QValue

            End If

            If rbYN.Items(1).Selected = True Then              'Answer is No

                QScore = 0

                TotalScore = TotalScore + 0

                PossibleScore = PossibleScore + QValue

            End If

            If rbYN.Items(2).Selected = True Then              'Answer is N/A

                QScore = 99

            End If

            cn.Close()

        Next

 

        tbScore.Text = TotalScore

        tbPossPoints.Text = PossibleScore

 

    End Sub

Thanks
0
 
LVL 25

Expert Comment

by:apeter
ID: 24762271
The code looks fine. Can we see the code which is there in the page_load event. Hope ur resetting the datagrid when there is a postback(calculate button click)....
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:florida73
ID: 24762701
The page load has this code

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            FillddlCostCtr()
            FillddlAgentNam()                       'ddlAgentNam relies on ddl.CostCtr
        End If
        FillGrid()
        lblSubmitSurvey.Visible = False
    End Sub

I am also attaching the whole code in the .vb file for that aspx page
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Put user code to initialize the page here

        If Not Page.IsPostBack Then

            FillddlCostCtr()

            FillddlAgentNam()                       'ddlAgentNam relies on ddl.CostCtr

        End If

        FillGrid()

        lblSubmitSurvey.Visible = False

    End Sub

#Region "GlobalConfig Functions"

    Public Shared Function RunSQLWithDataSet(ByVal sSQL As String, _

                ByVal sConnectionString As String) As DataSet

        Dim oDataSet As New DataSet

        Dim oDataAdapter As SqlClient.SqlDataAdapter

        Try

            'Create New DataAdapter 

            oDataAdapter = New SqlClient.SqlDataAdapter(sSQL, sConnectionString)

            oDataAdapter.SelectCommand.CommandTimeout = 1800

            'Fill Data Set from dataadapter

            oDataAdapter.Fill(oDataSet, "Table1")

            'Set return value of function

            Return oDataSet

        Catch e As Exception

            Throw New Exception("An exception occured in RunSQLWithDataSet", e)

        Finally

        End Try

    End Function

#End Region

 
 

#Region "DDLs"

    Private Sub FillddlCostCtr()

        ddlCostCtr.Items.Clear()

        Dim sConnectionString As String = CustomerService()

        Dim sSQL As String

        Dim oDS As DataSet

        Dim dt As DataTable

        Dim dr As DataRow

        sSQL = "select distinct a.Ctr_Descr + '   ' + a.Lawcode as Ctr_Descr, a.Lawcode as Lawcode "

        sSQL &= " from [wc-sqlc-a\a].[CCFWarehouse].[dbo].[A1 Cost Center Details] as a"

        sSQL &= " join [wc-gpsql].[Symposium].[dbo].[AgentLogins] as b "

        sSQL &= " on a.Lawcode = b.Lawcode "

        sSQL &= " order by Ctr_Descr "

        oDS = RunSQLWithDataSet(sSQL, sConnectionString)

        dt = oDS.Tables("Table1")

        ddlCostCtr.DataSource = dt

        ddlCostCtr.DataTextField = "Ctr_Descr"

        ddlCostCtr.DataValueField = "Lawcode"

        ddlCostCtr.DataBind()

        dt.Dispose()

        oDS.Clear()

    End Sub

 
 

    Private Sub FillddlAgentNam()

        ddlAgentName.Items.Clear()

        Dim sConnectionString As String = CustomerService()

        Dim sSQL As String

        Dim oDS As DataSet

        Dim dt As DataTable

        Dim dr As DataRow

        sSQL = "SELECT LName + ', ' + FName as AgentNam, EmplID FROM Symposium.dbo.AgentLogins "

        sSQL &= " WHERE status = 'A' And LAWCODE = '" & ddlCostCtr.SelectedValue & "' "

        sSQL &= " and EmplID is not null and EmplID <> '' "

        sSQL &= " ORDER BY LName "

        oDS = RunSQLWithDataSet(sSQL, sConnectionString)

        dt = oDS.Tables("Table1")

        ddlAgentName.DataSource = dt

        ddlAgentName.DataTextField = "AgentNam"

        ddlAgentName.DataValueField = "EmplID"

        ddlAgentName.DataBind()

        dt.Dispose()

        oDS.Clear()

    End Sub

 

    Private Sub ddlCostCtr_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCostCtr.SelectedIndexChanged

        FillddlAgentNam()

    End Sub

#End Region

#Region "Button Commands"

    Private Sub btnContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click

        If btnContinue.Text = "Calculate and Continue" Then

            ToggleLock()

        Else

            ToggleUnlock()

        End If

    End Sub

 
 

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        Dim sSQL As String

        Dim QID, QValue, SurveyID As Integer

        Dim oDS As DataSet

        Dim sConnectionString As String = CustomerService()

        Dim cn As New SqlClient.SqlConnection(CustomerService)

        Dim cmd As New SqlClient.SqlCommand(sSQL, cn)

        Dim drSQL As SqlClient.SqlDataReader

        Dim cmdSQL As New SqlClient.SqlCommand

 
 

        'Generate SurveyId

        cn.Open()

        cmdSQL.Connection = cn

        sSQL = " SELECT Convert(Int, Case When Max(SurveyId) is null then 0  "

        sSQL &= " else Max(SurveyId) end) as SurveyID FROM SurveyDetail "

        cmdSQL.CommandText = sSQL

        cmdSQL.CommandTimeout = 7200

        drSQL = cmdSQL.ExecuteReader

        Do Until drSQL.Read = False

            SurveyID = drSQL.GetInt32(0) + 1

        Loop

 

       'Insert into Survey Detail

        sSQL = " INSERT INTO SurveyDetail (SurveyId, Login, Lawcode, Agent, Dt, Tm, DeletedFlag) "

        sSQL &= " VALUES( " & SurveyID & ", 'ANAME', '" & ddlCostCtr.SelectedValue & "',"

        sSQL &= " '" & ddlAgentName.SelectedValue & "' , convert(datetime,convert(char(10),getdate),101)), "

        sSQL &= " Getdate(), '0') "

        oDS = RunSQLWithDataSet(sSQL, sConnectionString)

        oDS.Clear()

 
 

        'Insert comments into the comments table

       If tbComments.Text <> "" Then

            sSQL = " INSERT INTO CustomerService.dbo.Comments (SurveyId, Comments) "

            sSQL &= " VALUES ( " & SurveyID & " , '" & tbComments.Text & "')"

            oDS = RunSQLWithDataSet(sSQL, sConnectionString)

            oDS.Clear()

        End If

        cn.Close()

        'For Stmt to insert score individually into the scores table

        For Each i As DataGridItem In dgSurvey.Items

            Dim QName As Label = i.FindControl("lblQName")

            Dim rbYN As RadioButtonList = i.FindControl("rbYN")

            Dim QScore, TotalScore, PossibleScore As Integer

            cn.Open()

            cmdSQL.Connection = cn

            sSQL = " Select QID, Value from Questions where QLongName = '" & QName.Text & "'"

            cmdSQL.CommandText = sSQL

            cmdSQL.CommandTimeout = 7200

            drSQL = cmdSQL.ExecuteReader
 

 
 

            Do Until drSQL.Read = False

                QID = drSQL.GetInt32(0)                 'Get QuestionID

                QValue = drSQL.GetInt32(1)              'Get value of question

            Loop

            If rbYN.Items(0).Selected Then              'Answer is Yes

                QScore = QValue

                TotalScore = TotalScore + QValue

                PossibleScore = PossibleScore + QValue

            End If

            If rbYN.Items(1).Selected Then              'Answer is No

                QScore = 0

                TotalScore = TotalScore + 0

                PossibleScore = PossibleScore + QValue

            End If

            If rbYN.Items(2).Selected Then              'Answer is N/A

                QScore = 99

            End If

 
 

            'Insert score into the scores table

            sSQL = "INSERT INTO CustomerService.dbo.Scores (SurveyId, QuestionId, Score) "

            sSQL &= " VALUES( " & SurveyID & " , " & QID & ", " & QScore & " ) "

            oDS = RunSQLWithDataSet(sSQL, sConnectionString)

            oDS.Clear()

            cn.Close()

        Next

        ClearForm()

        ToggleUnlock()

        lblSubmitSurvey.Text = "Survey Successfully Submitted.  The SurveyID was " & SurveyID

        lblSubmitSurvey.Visible = True

    End Sub
 

#End Region

#Region "Toggle"
 

    Private Sub ToggleLock()

        'Comments

        lblComments.Visible = True

        tbComments.Visible = True

        'DropDowns

        ddlCostCtr.Enabled = False

        ddlAgentName.Enabled = False

        'Datagrid

        dgSurvey.Enabled = False

        'Score

        lblScore.Visible = True

        tbScore.Visible = True

        'Possible Score

        lblPossiblePoints.Visible = True

        tbPossPoints.Visible = True

        'Percentage Score

        CalculateScore()

        lblPercentage.Text = (CInt(tbScore.Text) / CInt(tbPossPoints.Text) * 100) & "%"

       lblPercentage.Visible = True

        'Option to submit button

        btnSubmit.Visible = True

        'Change text in Continue Button

        btnContinue.Text = "This survey still needs editing"

    End Sub
 
 

    Private Sub ToggleUnlock()

       'Comments

        lblComments.Visible = False

        tbComments.Visible = False

        'DropDowns

        ddlCostCtr.Enabled = True

        ddlAgentName.Enabled = True

        'Datagrid

        dgSurvey.Enabled = True

        'Score

        lblScore.Visible = False

        tbScore.Visible = False

        'Possible Score

        lblPossiblePoints.Visible = False

        tbPossPoints.Visible = False

        'Percentage Score

        lblPercentage.Visible = False

        'Option to submit button

        btnSubmit.Visible = False

        'Change text in Continue Button

       btnContinue.Text = "Calculate and Continue"

    End Sub

#End Region
 

    Private Sub CalculateScore()

        Dim QID, QValue, TotalScore, PossibleScore As Integer

        Dim sSQL, QLngNam As String

        Dim cn As New SqlClient.SqlConnection(CustomerService)

        Dim cmd As New SqlClient.SqlCommand(sSQL, cn)

        Dim drSQL As SqlClient.SqlDataReader

        Dim cmdSQL As New SqlClient.SqlCommand

        For Each i As DataGridItem In dgSurvey.Items

           Dim QName As Label = i.FindControl("lblQName")

            Dim rbYN As RadioButtonList = i.FindControl("rbYN")

            Dim QScore As Integer

            'Get the value of the question from database

            cn.Open()

            cmdSQL.Connection = cn

            sSQL = " Select QID, Value from Questions where QLongName = '" & QName.Text & "'"

            cmdSQL.CommandText = sSQL

            cmdSQL.CommandTimeout = 7200

            drSQL = cmdSQL.ExecuteReader
 

           Do Until drSQL.Read = False

                QID = drSQL.GetInt32(0)                 'Get QuestionID

                QValue = drSQL.GetInt32(1)              'Get value of question

            Loop

            If rbYN.Items(0).Selected = True Then             'Answer is Yes

                QScore = QValue

                TotalScore = TotalScore + QValue

                PossibleScore = PossibleScore + QValue

            End If

            If rbYN.Items(1).Selected = True Then              'Answer is No

                QScore = 0

                TotalScore = TotalScore + 0

                PossibleScore = PossibleScore + QValue

           End If

            If rbYN.Items(2).Selected = True Then              'Answer is N/A

                QScore = 99

            End If

            cn.Close()

       Next

       tbScore.Text = TotalScore

        tbPossPoints.Text = PossibleScore

    End Sub
 

 
 

   Private Sub ClearForm()

        tbComments.Text = ""

        lblPercentage.Visible = False

        FillddlCostCtr()

        FillddlAgentNam()

    End Sub
 

    Private Sub FillGrid()

        Dim sSQL As String

        Dim sConnectionString As String = CustomerService()

        Dim oDS As DataSet

        sSQL = " select top 500 q.*  from Questions as q "

        sSQL &= " join departments as d on q.QID = d.QuestionID "

        sSQL &= " where lawcode = '" & ddlCostCtr.SelectedValue() & "'"

        oDS = RunSQLWithDataSet(sSQL, sConnectionString)

        dgSurvey.DataSource = oDS

        dgSurvey.DataBind()

    End Sub

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

        CalculateScore()

    End Sub

End Class

Open in new window

0
 
LVL 25

Accepted Solution

by:
apeter earned 500 total points
ID: 24762720
Hope ur method, "FillGrid()" actually binds data to the grid. If so, then u will always get false for any selection in page.

For every postback ur datagrid is binded which erases the selection made by the user.

So move the method "FillGrid()" inside  if block(  If Not Page.IsPostBack Then..... )

If this is not working , let me know which method in page_load....binds data to the datagrid.
0
 
LVL 1

Author Comment

by:florida73
ID: 24762823
Thank you, Actually that worked after I moved the fillgrid() inside the if loop, but my datagrid is not refreshinging when I change my drop don but let me test some more may be I  am missing something, Ill test that  and get back
But the functionality I was trying to get working is working now.
Thanks again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now