ASP.Net网页开发——DataGrid的应用(2)

2. VB.Net代码:

Imports System.Data
Imports OneTransport.DataAccess
Imports OneTransport.Business
Partial Class ubs_fix_price_maintain
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Call Bind_vehicle()
            Me.LoadData()
            Me.btnAdd.Attributes.Add("onclick", "Javascript:return validcheck();")
            Me.ltrErr.Text = ""
        End If
    End Sub
   

Private Sub LoadData()
        Dim objCs As New ClientSpecification
        Dim Ds As New DataSet
        ‘Dim objDv As DataView

        Ds = objCs.GetFixPrice

        If Ds.Tables.Count > 0 AndAlso Ds.Tables(0).Rows.Count > 0 Then
            With Me.dgPrices
                .Visible = True
                .DataSource = Ds.Tables(0).DefaultView
                .DataBind()
            End With

            If Me.dgPrices.PageCount > 1 Then
                Me.dgPrices.PagerStyle.Visible = True
            Else
                Me.dgPrices.PagerStyle.Visible = False
            End If
        Else
            Me.dgPrices.Visible = False
        End If
        Ds.Dispose()
        Ds = Nothing
        objCs = Nothing
    End Sub
   

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAdd.Click
        Dim objCs As New ClientSpecification
        Dim Ds As New DataSet
        Dim objmsg As New MSG
        Dim numback As Integer

        Dim strPickupPostCode As String
        Dim strDesPostCode As String
        Dim intVehicleType As Integer
        Dim sigJourneyCost As Single
        Dim sigGratuity As Single
        Dim sigBookingFee As Single
        Dim sigAdminCost As Single
        Dim charActiveFlag As Char

        strPickupPostCode = txtPuCode.Text
        strDesPostCode = txtDesCode.Text
        intVehicleType = Convert.ToInt32(ddlVehicleType.SelectedItem.Value)
        sigJourneyCost = Convert.ToSingle(txtJourneyCost.Text)
        sigGratuity = Convert.ToSingle(txtGratuity.Text)
        sigBookingFee = Convert.ToSingle(txtBookingFee.Text)
        sigAdminCost = Convert.ToSingle(txtAdminCost.Text)
        If Me.rdbON.Checked Then
            charActiveFlag = "Y"
        Else
            charActiveFlag = "N"
        End If

        numback = objCs.AddFixPrice(strPickupPostCode, strDesPostCode, intVehicleType, sigJourneyCost, sigGratuity, sigBookingFee, sigAdminCost, charActiveFlag)
        If numback = 1 Then
            Page.ClientScript.RegisterStartupScript(GetType(String), "success", "<script language=’javascript’>alert(‘" & objmsg.Messageout(MSG.MessageType.AddSuccessful) & "’);top.GB_hide();top.window.location.href=’ubs_fix_price_maintain.aspx'</script>")
            Me.LoadData()
        Else
            Page.ClientScript.RegisterStartupScript(GetType(String), "failed", "<script language=’javascript’>alert(‘" & objmsg.Messageout(MSG.MessageType.AddFailed) & "’);</script>")
            Exit Sub
        End If
    End Sub

    Protected Sub dgPrices_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgPrices.CancelCommand
        Me.dgPrices.EditItemIndex = -1
        Me.LoadData()
    End Sub
   

Protected Sub dgPrices_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgPrices.EditCommand
        Me.dgPrices.EditItemIndex = e.Item.ItemIndex
        Me.hdnID.Value = e.CommandArgument.ToString.Trim
        Me.LoadData()

    End Sub

    Protected Sub dgPrices_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPrices.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lblJCost As Label = CType(e.Item.FindControl("lblFixedJourneyCost"), Label)
            Dim strJCost As String = DataBinder.Eval(e.Item.DataItem, "fix_journey_cost").ToString
            lblJCost.Text = Convert.ToDouble(strJCost)

            Dim lblGratuity As Label = CType(e.Item.FindControl("lblFixedGratuity"), Label)
            Dim strGratuity As String = DataBinder.Eval(e.Item.DataItem, "fix_gratuity").ToString
            lblGratuity.Text = Convert.ToDouble(strGratuity)

            Dim lblBookingFee As Label = CType(e.Item.FindControl("lblFixedBookingFee"), Label)
            Dim strBookingFee As String = DataBinder.Eval(e.Item.DataItem, "fix_booking_fee").ToString
            lblBookingFee.Text = Convert.ToDouble(strBookingFee)

            Dim lblAdminCost As Label = CType(e.Item.FindControl("lblFixedAdminCost"), Label)
            Dim strAdminCost As String = DataBinder.Eval(e.Item.DataItem, "fix_admin_cost").ToString
            lblAdminCost.Text = Convert.ToDouble(strAdminCost)

            Dim lblFlag As Label = CType(e.Item.FindControl("lblActiveFlag"), Label)
            Dim status As String = DataBinder.Eval(e.Item.DataItem, "active_flag").ToString
            If status = "Y" Then
                lblFlag.Text = "ON"
            ElseIf status = "N" Then
                lblFlag.Text = "OFF"
            End If
        End If

        If e.Item.ItemType = ListItemType.EditItem Then

            Dim txtJCost As TextBox = CType(e.Item.FindControl("txtFixedJourneyCost"), TextBox)
            Dim strJCost As String = DataBinder.Eval(e.Item.DataItem, "fix_journey_cost").ToString
            txtJCost.Text = Convert.ToDouble(strJCost)

            Dim txtGratuity As TextBox = CType(e.Item.FindControl("txtFixedGratuity"), TextBox)
            Dim strGratuity As String = DataBinder.Eval(e.Item.DataItem, "fix_gratuity").ToString
            txtGratuity.Text = Convert.ToDouble(strGratuity)

            Dim txtBookingFee As TextBox = CType(e.Item.FindControl("txtFixedBookingFee"), TextBox)
            Dim strBookingFee As String = DataBinder.Eval(e.Item.DataItem, "fix_booking_fee").ToString
            txtBookingFee.Text = Convert.ToDouble(strBookingFee)

            Dim txtAdminCost As TextBox = CType(e.Item.FindControl("txtFixedAdminCost"), TextBox)
            Dim strAdminCost As String = DataBinder.Eval(e.Item.DataItem, "fix_admin_cost").ToString
            txtAdminCost.Text = Convert.ToDouble(strAdminCost)

            Dim objAdmin As New AdminMaintenance
            Dim ddlVType As DropDownList = CType(e.Item.FindControl("ddlVType"), DropDownList)
            Dim ds As DataSet = objAdmin.GetPassVehicleType("UBS")
            If ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
                ddlVType.DataSource = ds.Tables(0).DefaultView
                ddlVType.DataTextField = "type_name"
                ddlVType.DataValueField = "vehicle_id"
                ddlVType.DataBind()
            End If

            Dim flag As String = DataBinder.Eval(e.Item.DataItem, "active_flag").ToString

            If flag.Equals("Y") Then
                Dim rdbYes As RadioButton = CType(e.Item.FindControl("rdbYes"), RadioButton)
                rdbYes.Checked = True
            Else
                Dim rdbNo As RadioButton = CType(e.Item.FindControl("rdbNo"), RadioButton)
                rdbNo.Checked = True
            End If
        End If
    End Sub
   

Protected Sub dgPrices_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgPrices.UpdateCommand
        Dim objMsg As New MSG
        Dim objClientSpecification As New ClientSpecification
        Me.hdnID.Value = e.CommandArgument.ToString.Trim

        Dim id As String = Me.hdnID.Value.Trim
        Dim strPickupPostCode As String
        Dim strDesPostCode As String
        Dim intVehicleType As Integer
        Dim decJourneyCost As Decimal
        Dim decGratuity As Decimal
        Dim decBookingFee As Decimal
        Dim decAdminCost As Decimal
        Dim charActiveFlag As Char
        Dim numback As Integer

        If CType(e.Item.FindControl("txtPickupPostCode"), TextBox).Text.Length < 3 _
        Or CType(e.Item.FindControl("txtDestinationPostCode"), TextBox).Text.Length < 3 _
        Or CType(e.Item.FindControl("txtFixedJourneyCost"), TextBox).Text.Trim = "" _
        Or CType(e.Item.FindControl("txtFixedGratuity"), TextBox).Text.Trim = "" _
        Or CType(e.Item.FindControl("txtFixedBookingFee"), TextBox).Text.Trim = "" _
        Or CType(e.Item.FindControl("txtFixedAdminCost"), TextBox).Text.Trim = "" Then
            Me.ltrErr.Text = "<script>alert(‘Invalid input!’);</script>"
        Else
            strPickupPostCode = CType(e.Item.FindControl("txtPickupPostCode"), TextBox).Text.Trim
            strDesPostCode = CType(e.Item.FindControl("txtDestinationPostCode"), TextBox).Text.Trim
            intVehicleType = Convert.ToInt16(CType(e.Item.FindControl("ddlVType"), DropDownList).SelectedValue)
            decJourneyCost = Convert.ToDecimal(CType(e.Item.FindControl("txtFixedJourneyCost"), TextBox).Text.Trim)
            decGratuity = Convert.ToDecimal(CType(e.Item.FindControl("txtFixedGratuity"), TextBox).Text.Trim)
            decBookingFee = Convert.ToDecimal(CType(e.Item.FindControl("txtFixedBookingFee"), TextBox).Text.Trim)
            decAdminCost = Convert.ToDecimal(CType(e.Item.FindControl("txtFixedAdminCost"), TextBox).Text.Trim)

            Dim rdbYes As RadioButton = CType(e.Item.FindControl("rdbYes"), RadioButton)
            If rdbYes.Checked = True Then
                charActiveFlag = "Y"
            Else
                charActiveFlag = "N"
            End If

            numback = objClientSpecification.UpdateFixPrice(id, strPickupPostCode, strDesPostCode, _
                                                                intVehicleType, decJourneyCost, _
                                                                decGratuity, decBookingFee, _
                                                                decAdminCost, charActiveFlag)
            Select Case numback
                Case 1
                    Page.ClientScript.RegisterStartupScript(GetType(String), "success", "<script language=’javascript’>alert(‘" + objMsg.Messageout(MSG.MessageType.UpdateSuccessful) + "’);</script>")
                    Me.dgPrices.EditItemIndex = -1
                    Me.LoadData()
                Case -1
                    Page.ClientScript.RegisterStartupScript(GetType(String), "success", "<script language=’javascript’>alert(‘" + objMsg.Messageout(MSG.MessageType.UpdateFailed) + "’);</script>")
            End Select
        End If
    End Sub
   

Private Sub Bind_vehicle()
        Dim objAdmin As New AdminMaintenance
        Dim ds As DataSet = objAdmin.GetPassVehicleType("UBS")
        If ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            Me.ddlVehicleType.DataSource = ds.Tables(0).DefaultView
            Me.ddlVehicleType.DataTextField = "type_name"
            Me.ddlVehicleType.DataValueField = "vehicle_id"
            Me.ddlVehicleType.DataBind()
        End If
    End Sub
End Class

 

3. 辅助类(GetFixPrice为例):

Public Function GetUBSFixPrice() As DataSet
        Me.SelectCommand = Nothing
        With Me.SelectCommand
            .CommandType = CommandType.StoredProcedure
            .CommandText = "ot_ubs_fixed_price_get"
        End With
        Return Me.QueryData("GetFixPrice")
    End Function

 

4. 辅助类中调用的存储过程:

select * from ubs_fix_price

 

总结:web控件与数据库中字段绑定;在网页或控件事件中,调用存储过程,获取数据库中数据,并由dataGrid显示。

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s