Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 14th, 2008, 11:54 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Radiobuttons Andrew.Berry ASP.NET 2.0 Professional 6 April 18th, 2008 10:29 AM
Pull data values & assign them to a text control cesemj ASP.NET 2.0 Basics 0 March 12th, 2008 06:54 AM
Dynamic Checkboxes using dynamic Select Values bsheridan Classic ASP Databases 0 March 12th, 2006 10:17 AM
Assigning radiobuttons hydra14 C# 7 June 5th, 2004 12:25 AM



All times are GMT -4. The time now is 08:19 AM.


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