www.webdeveloper.com
Results 1 to 3 of 3

Thread: ASP.NET 2.0: Trying to pull values from dynamic radiobuttons

  1. #1
    Join Date
    May 2003
    Posts
    599

    ASP.NET 2.0: Trying to pull values from dynamic radiobuttons

    I basically need to know how to pull selected radiobutton values from an ItemDataBound event into the RadioButton_CheckedChanged event.

    The radiobutton values are dynamically populated from an ItemDataBound event for three test records, which are located within a SQL Server 2000 database table. The two options are "Y" and "N".

    Each radiobutton already does have the following properties set: AutoPostBack=True, and OnCheckedChanged = "RadioButton_CheckedChanged". When each of the radiobuttons are changed, the RadioButton_CheckedChanged event does fire. But only static values can get inputted into the DB table. The values from the dynamic radiobuttons don't get passed from the ItemDataBound event to the RadioButton_CheckedChanged event.

    I've included my code below. If anyone can let me know what I'm doing wrong, that would be great. Thanks.

    Here's my code:
    form.aspx.vb
    Code:
    Partial Class test_folder_Formprocedure_Formrequests
        Inherits System.Web.UI.Page
    
        Private ds As New DataSet()
        Dim sqlConn As SqlConnection
        Dim sqlCmd As SqlCommand
        Dim strConnection As String
        Private cmd1 As SqlDataAdapter = New SqlDataAdapter
        Private cmd2 As New SqlCommand()
        Dim strApproved As String = Nothing
        Dim strRecordID As String = Nothing
        Dim strUsername As String = Nothing
        'Declare radiobuttons
        Dim rbApproveYes As RadioButton = Nothing
        Dim rbApproveNo As RadioButton = Nothing
    
        Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load
            'Assign connection string
            strConnection = System.Configuration.ConfigurationManager.AppSettings("strConn")
            sqlConn = New SqlConnection(strConnection)
    
            'Pull username
            Dim iPos
            strUsername = "SERVERNAME\USERNAME"
            iPos = Len(strUsername) - InStr(1, strUsername, "\", 1)
            strUsername = Right(strUsername, iPos)
        End Sub
    
        'Bind Data to DataList Populating the Dataset 
        Sub BindstrDbRecordID()
            cmd1 = New SqlDataAdapter("spFormRequests", sqlConn)
    
            'Bind data
            cmd1.Fill(ds, "tblForm")
            dlForm.DataSource = ds
            dlForm.DataBind()
        End Sub
    
        'The ItemDataBound Event
        Public Sub dlForm_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
    
            Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
    
            If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
    
                'Step 1: Assign label values
                strRecordID = drv.Row("RecordID").ToString
                Dim strFName As String = drv.Row("FName").ToString
                Dim strLName As String = drv.Row("LName").ToString
                Dim strOnBehalfOf As String = drv.Row("OnBehalfOf").ToString
                Dim strReason As String = drv.Row("Reason").ToString
                Dim strStartDate As String = drv.Row("StartDate").ToString
                Dim strEndDate As String = drv.Row("EndDate").ToString
                Dim strStartTime As String = drv.Row("StartTime").ToString
                Dim strEndTime As String = drv.Row("EndTime").ToString
                strApproved = drv.Row("Approved").ToString
                Dim strMgrID As String = drv.Row("MgrID").ToString
                Dim strMgrFName As String = drv.Row("MgrFName").ToString
                Dim strMgrLName As String = drv.Row("MgrLName").ToString
                Dim strMgrEmail As String = drv.Row("MgrEmail").ToString
    
                'Assign requester's full name
                Dim strReqName As String = Nothing
                If strOnBehalfOf Is DBNull.Value Or strOnBehalfOf = "" Then
                    'If strOnBehalfOf Is Nothing Then
                    strReqName = strFName & "*" & strLName
                Else
                    strReqName = strFName & "*" & strLName & " (on behalf of " & strOnBehalfOf & ")"
                End If
    
                'Assign full date(s)
                Dim strReqDates As String = Nothing
                If strEndDate Is DBNull.Value Or strEndDate = "" Then
                    strReqDates = strStartDate
                Else
                    strReqDates = strStartDate & "*-*" & strEndDate
                End If
    
                'Assign full times
                Dim strReqTimes As String = strStartTime & "*-*" & strEndTime
    
                'Assign email address for responders
                Dim hplMgr As HyperLink = DirectCast(e.Item.FindControl("hplMgr"), HyperLink)
                hplMgr.Text = strMgrFName & "*" & strMgrLName
                hplMgr.NavigateUrl = "mailto:" & strMgrEmail & strRecordID
    
                rbApproveYes = CType(e.Item.FindControl("rbApproveYes"), RadioButton)
                rbApproveNo = CType(e.Item.FindControl("rbApproveNo"), RadioButton)
    
                'Assign static radiobutton properties
                rbApproveYes.Text = "Y"
                rbApproveYes.GroupName = "rbApproved"
                rbApproveYes.AutoPostBack = True
                'rbApproveYes.OnCheckedChanged = "rbApproved_CheckedChanged"
    
                rbApproveNo.Text = "N"
                rbApproveNo.GroupName = "rbApproved"
                rbApproveNo.AutoPostBack = True
                'rbApproveNo.OnCheckedChanged = "rbApproved_CheckedChanged"
    
                'Approved check: Dynamically populate radiobuttons with "Approved" value, like this:
                If strApproved = "Y" Then
                    rbApproveYes.Checked = True
                End If
    
                If strApproved = "N" Then
                    rbApproveNo.Checked = True
                End If
    
                'Step 3: Assign label properties
                CType(e.Item.FindControl("lblRecordID"), Label).Text = strRecordID
                CType(e.Item.FindControl("lblReqName"), Label).Text = strReqName
                CType(e.Item.FindControl("lblReason"), Label).Text = strReason
                CType(e.Item.FindControl("lblReqDates"), Label).Text = strReqDates
                CType(e.Item.FindControl("lblReqTimes"), Label).Text = strReqTimes
    
            End If
        End Sub
    
        Public Sub RadioButton_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim rbApproveYes As RadioButton = DirectCast(sender, RadioButton)
            Dim rbApproveNo As RadioButton = DirectCast(sender, RadioButton)
    
            'Pull form values from rbApprovedYes and rbApprovedNo
            Dim strApprovedValue As String = Nothing 'live
            If rbApproveYes.Checked Then
                strApprovedValue = "Y"
            ElseIf rbApproveNo.Checked Then
                strApprovedValue = "N"
            End If
    
            'Assign date variables
            Dim dtCurrDate As DateTime = DateTime.Now 'Assign current date
            Dim strDateTimeISO As String = dtCurrDate.ToString("s") 'ISO format
    
            'Insert into database
            cmd2 = New SqlCommand("spFormApproval", sqlConn)
            cmd2.CommandType = CommandType.StoredProcedure
    
            'Assign form paramaters
            cmd2.Parameters.Add("@RecordID", SqlDbType.VarChar, 50).Value = "10" 'strRecordID
            cmd2.Parameters.Add("@Approved", SqlDbType.VarChar, 50).Value = strApprovedValue
            cmd2.Parameters.Add("@MgrID", SqlDbType.VarChar, 50).Value = strUsername
            cmd2.Parameters.Add("@datetime", SqlDbType.VarChar, 50).Value = strDateTimeISO
    
            'Open connection
            sqlConn.Open()
    
            'Execute query
            cmd2.ExecuteNonQuery()
    
            'Close connection
            sqlConn.Close()
    
            'End If
        End Sub
    End Class
    ASP.NET CODE IS IN NEXT POST DUE TO CHARACTER LIMIT
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

  2. #2
    Join Date
    May 2003
    Posts
    599
    form.aspx
    Code:
    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" EnableSessionState="true" EnableViewState="true" CodeFile="formrequests.aspx.vb" Inherits="test_folder_formprocedure_formrequests" title="Form Requests" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        Below is a list of stop work requests (starting with the most recent):
        <br />
        <br />
        <form name="FormRequests" runat="server">
            <asp:DataList ID="dlForm" runat="server" OnItemDataBound="dlForm_ItemDataBound" RepeatLayout="Flow">
                <ItemTemplate>
                    <table class="tableborder_black" width="98%" id="Form Request #<%#DataBinder.Eval(Container, "DataItem.RecordID")%>">
                        <tr class="tablecell_black">
                            <th colspan="3">Form Request #<asp:Label runat="server" ID="lblRecordID"></asp:Label></th>
                        </tr>
                        <tr class="tablecell1" align="center">
    				        <td align="center" width="50%">Requested By</td>
    				        <td align="center" width="25%">Status</td>
    				        <td align="center" width="25%">Responded By</td>
    			        </tr>
                        <tr class="tablecell3" align="center">
                            <td>
                                <asp:Label runat="server" id="lblReqName"></asp:Label>
                            </td>
                            <td>
                                Approve?:
                                <asp:RadioButton ID="rbApproveYes" OnCheckedChanged = "RadioButton_CheckedChanged" runat="server" />
                                <asp:RadioButton ID="rbApproveNo" OnCheckedChanged = "RadioButton_CheckedChanged" runat="server" />
                            </td>
                            <td>
                                <asp:HyperLink ID="hplMgr" runat="server" />
                            </td>
                        </tr>
                        <tr class="tablecell1">
    			            <td align="center">Reason</td>
    			            <td align="center">Requested Date(s)</td>
    				        <td align="center">Requested Time(s)</td>
                        </tr>
                        <tr class="tablecell3" align="center">
    			            <td align="center" valign="top"><asp:Label runat="server" ID="lblReason"></asp:Label></td>
    			            <td align="center"><asp:Label runat="server" ID="lblReqDates"></asp:Label></td>
    				        <td align="center"><asp:Label runat="server" ID="lblReqTimes"></asp:Label></td>
                        </tr>
                        <tr class="tablecell_black">
                            <td colspan="3" align="center">
                                <asp:HiddenField ID="hfRecordID" value='<%#DataBinder.Eval(Container, "DataItem.RecordID")%>' runat="server" />
                                <asp:Button ID="btnPrint" runat="server" Text="Print This Request" />
                            </td>
                        </tr> 
                    </table>
                    <br /><br />
                </ItemTemplate>
            </asp:DataList> 
        </form>
    </asp:Content>
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

  3. #3
    Join Date
    Nov 2008
    Posts
    6
    The only way I can think of doing this is through JavaScript. You can loop through the controls and accumulate the values that are selected in a variable then pass it as a query string.

    You can loop through the controls by using the control id's, where you should have something like;

    dlForm_ctl01_rbApproveYes
    dlForm_ctl01_rbApproveNo
    dlForm_ctl02_rbApproveYes
    dlForm_ctl02_rbApproveNo

    you can use these numbers like
    Code:
    for ( x = 1 ; x < numberOfControls ; x++ )
    {
        if ( x < 10 )
        {
            x = '0' + x; // You need this cause it'll look for ctl1 instead of 01 which is how .NET names it.
        }
        var yes = document.getElementById('dlForm_ctl' + 01 + '_rbApproveYes');
        var no = document.getElementById('dlForm_ctl' + 01 + '_rbApproveNo');
        ... check if either yes or no are selected
    }
    Hop this helps.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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



Recent Articles