Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking 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 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 10th, 2007, 02:56 PM
Authorized User
 
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
Default Making an Alteration to an VB Codebehind file

Hello,

I'm trying to make some minor changes to the validation code of a checkout page in an ASP.NET webapp. The original builder of the site stripped out the VB codebehind files, leaving just the .aspx files, .dll, and so on in the FTP site.

I have recently recieved from the site-builder all of the original source code, so now I don't have to try to decompile it in Reflector. I have dumped the original checkout_main.aspx.vb source file into the FTP site, and it shows up nested behind its respective .aspx page.

I have made the minor changes (just commenting out some validation code so it doesn't function), but the webpage doesn't seem to recognize the changes. "Building" the codebehind page fails, and several errors show up in VS2005: almost every "name" on the codebehind page is "not declared". Does this mean that the source page is referencing some other VB page that is missing, and therefore cannot compile into a new dll? Should I replace each and every sourcecode file into the FTP site before it can recompile with changes?

Thanks for any help. I have copied the code below:



Code:
Imports System.Data.SqlClient
Imports System.Web.Mail


Namespace petstorewebsite

Partial Class checkout_main
    Inherits System.Web.UI.Page
    Protected WithEvents loginid As System.Web.UI.WebControls.TextBox
    Protected WithEvents password As System.Web.UI.WebControls.TextBox
    Protected WithEvents password1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents CompareFieldValidator1 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents hint_q As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Hint_answeer As System.Web.UI.WebControls.TextBox
    Protected WithEvents Salutation As System.Web.UI.WebControls.DropDownList
    Dim myconnection As SqlConnection
    Dim mycommand As SqlCommand
    Dim chk_lib As New main()
    Dim retval As String
    Dim insertstr As String
    Dim b_addr As String
    Dim b_city1 As String
    Dim b_state1 As String
    Dim b_pn As String
    Dim ots As String
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myconnection = New SqlConnection(ConfigurationSettings.AppSettings("DSN_PET"))
        myconnection.Open()
        Session("login_session") = Session.SessionID
        If Session("user_name") = "" Then
            user_name.Text = "Guest"
            myprofile.Text = "| <a href=index.aspx class=styleclass>Login</a>"
        Else
            user_name.Text = Session("user_name")
            If Session("login") = "Retailer" Then
                myprofile.Text = "| <a href=edit_retailer_info.aspx class=styleclass>My Profile</a> | <a href=logout.aspx class=styleclass>Logout</a>"
            ElseIf Session("login") = "International" Then
                myprofile.Text = "| <a href=edit_interantional_info.aspx class=styleclass>My Profile</a> | <a href=logout.aspx class=styleclass>Logout</a>"

            End If
        End If
        ' price1.Text = Session("price_pay")
    End Sub
    Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
        myconnection.Close()
    End Sub

    Private Sub submit_ag_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles submit_ag.Click
        'If Trim(loginid.Text) = "" Then
        '    message.Text = "Please Specify  Loginid"
        '    message.Style("color") = "Red"
        '    message.Font.Bold = True
        '    Exit Sub
        'End If
        'If chk_lib.chksearch1(Trim(loginid.Text)) = False Then
        '    message.Text = "loginid length should be 5 characters"
        '    message.Style("color") = "Red"
        '    message.Font.Bold = True
        '    Exit Sub

        'End If
        'If Trim(password.Text) = "" Then
        '    message.Text = "Please Specify Password"
        '    message.Style("color") = "Red"
        '    message.Font.Bold = True
        '    Exit Sub
        'End If
        'If Trim(password1.Text) = "" Then
        '    message.Text = "Please Re Entre Password"
        '    message.Style("color") = "Red"
        '    message.Font.Bold = True
        '    Exit Sub
        'End If

        'If Trim(Hint_answeer.Text) = "" Then
        '    message.Text = "Please Specify  Hint Answer"
        '    message.Style("color") = "Red"
        '    message.Font.Bold = True
        '    Exit Sub
        'End If
        If Trim(fname.Text) = "" Then
                message.Text = "Please specify  your name"
            message.Style("color") = "Red"
            message.Font.Bold = True
            Exit Sub
        End If
        Session("fname_12") = fname.Text
        If Trim(address.Text) = "" Then
                message.Text = "Please specify your street address"
            message.Style("color") = "Red"
            message.Font.Bold = True
            Exit Sub
        End If
        Session("address") = address.Text
        If Trim(city.Text) = "" Then
                message.Text = "Please specify your city"
            message.Style("color") = "Red"
            message.Font.Bold = True
            Exit Sub
        End If
        Session("city") = city.Text
        If Trim(pin.Text) = "" Then
                message.Text = "Please specify your zip code"
            message.Style("color") = "Red"
            message.Font.Bold = True
            Exit Sub
        End If
        Session("pin") = pin.Text
        If Trim(phone.Text) = "" Then
                message.Text = "Please specify your phone number"
            message.Style("color") = "Red"
            message.Font.Bold = True
            Exit Sub
        End If

            If Trim(pin.Text) <> "" Then
                If chk_lib.chknum(Trim(pin.Text)) = False Then
                    message.Text = "Your zip code must be entered as a number (i.e. '01913')"
                    message.Style("color") = "Red"
                    message.Font.Bold = True
                    Exit Sub
                End If
            End If

            'If Trim(phone.Text) <> "" Then
            'If chk_lib.chknum(Trim(phone.Text)) = False Then
            'message.Text = "Phone must have numeric entry "
            'message.Style("color") = "Red"
            'message.Font.Bold = True
            'Exit Sub
            'End If
            'End If
            'If Trim(Phone_w_b.Text) <> "" Then
            'If chk_lib.chknum(Trim(Phone_w_b.Text)) = False Then
            'message.Text = "Phone must have numeric entry "
            'message.Style("color") = "Red"
            'message.Font.Bold = True
            'Exit Sub
            'End If
            'End If
            'If Trim(cell_no.Text) <> "" Then
            'If chk_lib.chknum(Trim(cell_no.Text)) = False Then
            'message.Text = "Cell No must have numeric entry "
            'message.Style("color") = "Red"
            'message.Font.Bold = True
            'Exit Sub
            'End If
            'End If

            If term.Checked = False Then

                message.Text = "It is required that you read and accept our Terms & Conditions"
                message.Style("color") = "Red"
                message.Font.Bold = True
                Exit Sub
            End If


            Dim country1 As String = "country"
            Dim state1 As String = Replace(Trim(state.SelectedItem.Text), "'", "''")
            Dim city1 As String = Replace(Trim(city.Text), "'", "''")
            Dim b_country1 As String = "country1"
            If b_state12.SelectedItem.Text <> "" Then
                b_state1 = Replace(Trim(b_state12.SelectedItem.Text), "'", "''")
            Else
                b_state1 = ""
            End If
            If b_city.Text <> "" Then
                b_city1 = Replace(Trim(b_city.Text), "'", "''")
            Else
                b_city1 = ""
            End If
            Dim fnm As String = Replace(Trim(fname.Text), "'", "''")

            Dim addr As String = Replace(Replace(Trim(address.Text), "'", "''"), vbCrLf, "<br>")
            If b_address.Text <> "" Then
                b_addr = Replace(Replace(Trim(b_address.Text), "'", "''"), vbCrLf, "<br>")
            Else
                b_addr = ""
            End If
            Dim pn As String = Replace(Trim(pin.Text), "'", "''")
            If b_pin.Text <> "" Then
                b_pn = Replace(Trim(b_pin.Text), "'", "''")
            Else
                b_pn = ""
            End If
            Dim tl As String = "t1"
            Dim mob As String = "mob"
            Dim eml As String
            Dim gu As String = "w"
            Dim promotional1 As String
            If promotional.Checked = True Then
                promotional1 = "Y"
            Else
                promotional1 = "N"
            End If
            If Trim(email.Text) = "" Then
                message.Text = "Please Specify Email Address"
                message.Style("color") = "Red"
                message.Font.Bold = True
                Exit Sub
            End If


            If chk_lib.chk_email(Trim(email.Text)) = False Then
                message.Text = "Please correct the format of your email address (i.e. 'jane@mailserver.com')"
                message.Style("color") = "Red"
                message.Font.Bold = True
                Exit Sub
            Else
                eml = Replace(Trim(email.Text), "'", "''")
            End If
            Session("email_id_user") = eml
            If state.SelectedItem.Text <> "Select State in the U.S" Then
                ots = ""

            Else
                If state.SelectedItem.Text = "Select State in the U.S" Then
                    state1 = "Other"
                    ots = Replace(Trim(other_st.Text), "'", "''")
                    Session("other_st") = ots
                End If
            End If

            'If state.SelectedItem.Text = "Select State in the U.S" Then
            '    If Trim(b_firstname.Text) = "" Then
            '        message.Text = "Please Specify Delivery Addrss Name"
            '        message.Style("color") = "Red"
            '        message.Font.Bold = True
            '        Exit Sub
            '    End If
            '    If Trim(b_lastname.Text) = "" Then
            '        message.Text = "Please Specify Delivery Addrss Last Name"
            '        message.Style("color") = "Red"
            '        message.Font.Bold = True
            '        Exit Sub
            '    End If
            '    If Trim(b_address.Text) = "" Then
            '        message.Text = "Please Specify Delivery Addrss "
            '        message.Style("color") = "Red"
            '        message.Font.Bold = True
            '        Exit Sub
            '    End If
            '    If Trim(b_emailid.Text) = "" Then
            '        message.Text = "Please Specify Delivery Addrss Email Id "
            '        message.Style("color") = "Red"
            '        message.Font.Bold = True
            '        Exit Sub
            '    End If
            '    If Trim(b_phone.Text) = "" Then
            '        message.Text = "Please Specify Delivery Addrss Phone Number "
            '        message.Style("color") = "Red"
            '        message.Font.Bold = True
            '        Exit Sub
            '    End If
            '    If Trim(b_city.Text) = "" Then
            '        message.Text = "Please Specify Delivery Addrss city Name "
            '        message.Style("color") = "Red"
            '        message.Font.Bold = True
            '        Exit Sub
            '    End If
            'End If
            Session("state_belongs") = state.SelectedItem.Text
            Session("country") = Country123.SelectedItem.Text
            Replace(Trim(b_emailid.Text), "'", "''")
            Dim st As Integer = 1
            insertstr = "insert into customer_detail (name,email_id,address,city,state,zip,b_address,b_city,b_state,b_zip,phone_no_home,phone_no_work_bus,mobile_no,status,date_of,amount,payment_status,promotional,lname,country,other_state,b_fname,b_lastname,b_phone,b_email,workorbusiness,session_id) values('"
            insertstr = insertstr & fnm & "','" & eml & "','" & addr & "','" & city1 & "','" & state1 & "','" & pn & "','" & b_addr & "','" & b_city1 & "','" & b_state1 & "','" & b_pn & "','" & phone.Text & "','" & Phone_w_b.Text & "','" & cell_no.Text & "'," & st & ",'" & Now().Date & "','0','0','" & promotional1 & "','" & Replace(Trim(Lname.Text), "'", "''") & "','" & Country123.SelectedItem.Text & "','" & ots & "','" & Replace(Trim(b_firstname.Text), "'", "''") & "','" & Replace(Trim(b_lastname.Text), "'", "''") & "','" & Replace(Trim(b_phone.Text), "'", "''") & "','" & Replace(Trim(b_emailid.Text), "'", "''") & "','0','" & Session("login_session") & "')"
            mycommand = New SqlCommand(insertstr, myconnection)
            mycommand.ExecuteNonQuery()

            insertstr = "delete from shoppingcarts where date_of< '" & Now().Date.ToShortDateString & " '"
            mycommand = New SqlCommand(insertstr, myconnection)
            mycommand.ExecuteNonQuery()
            ''''''''''''''''''''''''''''''option_of_product_buying
            ' Response.Redirect("creditcard_detail.aspx")
            Response.Redirect("option_of_product_buying.aspx")
        End Sub



End Class

End Namespace
 
Old April 11th, 2007, 08:28 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Code in a class file can't really reference another page. The class references other classes that are compiled into assemblies whether they come from the their own assembly or a different one. You are likely to have problems if you try to recompile just the one page class file. There is work around.

One trick I use when I need to create a page patch is to change the runtime behavior of the page. I call this method "Source behind" (vs. "Code Behind".

The runtime behavior of a page is dictated by the properties in the "@ Page" directive. "Inherits" specifies the class that the dynamically compiled page will derive from. The "CodeBehind" attribute in the page directive is used by visual studio and has no direct influence on the runtime behavior.

Normally, the code behind class is compiled into the web app assembly. On page execution the runtime constructs a page class from the parsed aspx markup. It marks the dynamic class to inherit from the class defined in the "Inherits" attribute. Usually this class comes from the PRE-compiled assembly. However, with some manipulation of the page directive you can change that.

What I do is change the "CodeBehind" attribute name into "Src" and strip off any preceding namespace qualifiers, thus changing this:

<%@ Page Language="vb" Src="myPage.aspx.vb" Inherits="MyProject.MyPage"%>

into this:

<%@ Page Language="vb" Src="myPage.aspx.vb" Inherits="MyPage"%>

The reason for the namespace stripping is that the VB file by itself does not qualify the class' namespace. A visual studio project has a "Default Namespace" property that provides the full namespace prefix for all classes contained in and compiled as part of the project. When the source behind file is compiled dynamically, it's created without a namespace prefix. (I imagine it's compiled into the same dynamic namespace as the page class from the markup. So you can simply reference the class name alone and it will find it. If you leave the full namespace in you'll get a "Can't find the class name" type error.

Now, when the runtime loads the aspx page and creates a class the file, it sees the Src attribute and it ALSO dynamically compiles the code behind file.

One issue you may encounter with this is the qualification of control class names. Again, this is a side effect of how a visual studio VB project works. There is a list of default imports in a VB project. In a vb class file you can simply declare "Dim x As Button" and it knows to use "System.Web.UI.WebControls.Button" because "System.Web.UI.WebControls" is imported by default. NOT the case with a stand-alone source behind file. You will need to add imports statements to the code file, or fully qualify the control class names.


Of course, after writing this whole thing up, I googled and found this article which explains the same technique:

http://www.asp101.com/articles/john/...vs/default.asp



-Peter
 
Old April 23rd, 2007, 04:48 PM
Authorized User
 
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Great info, thank you very much!
 
Old April 23rd, 2007, 05:30 PM
Authorized User
 
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Ok, I believe I understand your explanation; In the aspx page, I've changed this line:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="checkout_main.aspx.vb" Inherits="mywebsite.checkout_main"%>

...so that "Codebehind=" reads "Src=". Also, I've stripped the project name out of the inherits, so that "mywebsite.checkout_main" just reads "checkout_main". However, now I get a compilation error when I run the page:

Quote:
quote:Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30188: Declaration expected.

Source Error:

Line 5: Namespace mywebsite
Line 6:
***Line 7: Partial Class checkout_main *** [emphasis mine, this line was marked red by the error page]
Line 8: Inherits System.Web.UI.Page
Line 9: Protected WithEvents loginid As System.Web.UI.WebControls.TextBox
Here is the part of the codebehind page the error seems to be referencing:

Code:
Imports System.Data.SqlClient
Imports System.Web.Mail

Namespace mywebsite

Partial Class checkout_main
    Inherits System.Web.UI.Page
    Protected WithEvents loginid As System.Web.UI.WebControls.TextBox
    Protected WithEvents password As System.Web.UI.WebControls.TextBox
    Protected WithEvents password1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents CompareFieldValidator1 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents hint_q As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Hint_answeer As System.Web.UI.WebControls.TextBox
    Protected WithEvents Salutation As System.Web.UI.WebControls.DropDownList
    Dim myconnection As SqlConnection
    Dim mycommand As SqlCommand
    Dim chk_lib As New main()
    Dim retval As String
    Dim insertstr As String
    Dim b_addr As String
    Dim b_city1 As String
    Dim b_state1 As String
    Dim b_pn As String
    Dim ots As String


When I change Src and Inherits back, things work ok, just not with the changes.
 
Old April 24th, 2007, 08:11 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The problem here might be the partial class declaration. I'm not sure how this would be treated in a "source-behind" scenario. You could just take the other partial class file and physically combine them to create complete class file. Give that a try.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
use app_code vb class instead of codebehind keyvanjan ASP.NET 2.0 Professional 2 July 25th, 2007 10:12 AM
what is debug directive for codebehind file? lethe ASP.NET 1.0 and 1.1 Basics 4 May 21st, 2007 06:38 PM
Making a COM object in VB.NET Cohadar Pro VB.NET 2002/2003 0 March 30th, 2006 06:49 AM
Making DLLs in VB.NET loganscott2000 VB.NET 2002/2003 Basics 2 April 10th, 2005 04:40 PM
CodeBehind for .ascx file isn't working lcsgeek General .NET 1 January 21st, 2005 03:50 PM





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