p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   Trying to pull values from dynamic radiobuttons (http://p2p.wrox.com/showthread.php?t=71466)

kwilliams November 14th, 2008 11:54 AM

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 & "#160;" & strLName
            Else
                strReqName = strFName & "#160;" & 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 & "#160;-#160;" & strEndDate
            End If

            'Assign full times
            Dim strReqTimes As String = strStartTime & "#160;-#160;" & strEndTime

            'Assign email address for responders
            Dim hplMgr As HyperLink = DirectCast(e.Item.FindControl("hplMgr"), HyperLink)
            hplMgr.Text = strMgrFName & "#160;" & 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

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


All times are GMT -4. The time now is 04:03 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.