dcsimg
Results 1 to 9 of 9

Thread: A newbie Web Developer question?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    A newbie Web Developer question?

    I'm pretty new to web development. Been trying to teach myself several new tools. I'm pretty familiar with the .Net environment. However, ASP.Net is pretty new to me. With that said, when running a web app, which code is executed first, javascript/html or C# code-behind? I'm asking because I'm having trouble tracking a variable that seems to change and I can't trace it very well. I'm trying to debug javascript code in the Visual Studio and I can't get it to hit the code where breakpoints are set. Can someone point me to some good documentation for this?

    Thanks,
    Blake

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,764

    Re: A newbie Web Developer question?

    ASP.Net "generates" the HTML page - and the PAGE LOAD event code runs prior to that (it's been years since I've done traditional ASP.Net - hope I'm not stating fake facts!).

    That same code fires again for each event - that's why you have Page.IsPostBack flag being true so you KNOW not to run the full page load code.

    IMO, you should be using a WEB DEBUGGER on the WEB side - each browser has a developer tool for debugging.

    I personally stopped using ASP.Net because I found that the "forced-to-be-just-like-a-WINFORM-event-model" to just be plain stupid.

    Under the hood all that ASP.Net is doing is using AJAX to make POST's back to the backend code. It sends a REAL LOT of data back and forth so that the "state of the web page" can be "maintained" (note that web pages are stateless).

    Once you are in some kind of backend code event for a button click, for instance, you then have to deal with the fact that the names of the controls on the HTML page are created at run-time and are cryptic to say the least.

    This book - now a decade old - is a great read.

    https://www.manning.com/books/jquery-in-action

    Here you will learn the underlying technologies used by ASP.Net to accomplish what it's doing. Not at all complicated - and maybe worth considering. jQuery is not the only web library - there are many others that have grown popular in the past 10 years.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Re: A newbie Web Developer question?

    Man there just seems to be so many tools to do the same thing. I would like to do web development without using the .Net environment. What other tool can be used with web development that will interact with MS SQL Server db's?
    Blake

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,764

    Re: A newbie Web Developer question?

    I have web services running that I POST requests to.

    The web services are developed just like you would backend code for ASP.Net - I use VB.Net and it runs under IIS.

    From the browser you need to POST requests for data - that is going to involve JavaScript since it's the only language the browser understands.

    And with that said, there are helpful JavaScript libraries to make life easier.

    Here is a web service that does a LOOKUP on a value that the user just entered - and returns associated data with that lookup.

    Code:
        <WebMethod()> _
        <ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
        Public Function LookupService(ByVal fromwho As String, ByVal fromddtype As String, ByVal toddtype As String, ByVal lookup As String _
                                                    , ByVal lookupextra As Dictionary(Of String, String) _
                                                    , ByVal sguid As String _
                                                    , ByVal username As String) As String
            Dim rtnString As String = ""
    
            Try
                If checkGuid(sguid) Then
                    Dim JsonMaker As JsonWriter = New JsonWriter
                    With JsonMaker
                        .StartObject()
                        Try
                            Using dcn As New SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("LocalSQLServerAWC" & credDB).ToString)
                                Using cmd As New SqlCommand
                                    cmd.CommandType = CommandType.StoredProcedure
                                    If lookup <> "" Then lookup = "_" & lookup
                                    cmd.CommandText = "dbo.awc_" & fromddtype & toddtype & "_Lookup" & lookup
                                    cmd.Connection = dcn
                                    cmd.CommandTimeout = 0
                                    DetermineParameters(dcn, cmd)
                                    SetParameters(cmd, True, fromwho, lookupextra)
                                    dcn.Open()
                                    Using sdrReader As SqlDataReader = cmd.ExecuteReader
                                        While sdrReader.Read
                                            If sdrReader.GetName(0).StartsWith("acs-") Then
                                            Else
                                                For i As Integer = 0 To sdrReader.FieldCount - 1
                                                    .NewObject(sdrReader.GetName(i), sdrReader(i).ToString, True, True)
                                                    If i < sdrReader.FieldCount - 1 Then .Seperate()
                                                Next
                                            End If
                                        End While
                                    End Using
                                End Using
                            End Using
                        Catch ex As Exception
                            .ResetJson()
                            .StartObject()
                            .NewObject("%%dalerror%%", ex.Message.Replace("""", "'").Replace("\", "\\"))
                        End Try
                        .EndObject()
                    End With
                    rtnString = JsonMaker.GetJson()
                Else
                    rtnString = "{""LoginRequired"": true}"
                    LogOutput("GUID not found (LookupService): " & username)
                End If
            Catch ex As Exception
                LogOutput(ex.Message & " (LookupService): " & username)
            End Try
    
            Return rtnString
    
        End Function
    The screen shot shows the web page - the field that I'm going to lookup - and the POST that is being made to the database.

    This is the JSON string that is being returned by the web service - the FEE and DATEPAID values are going to be written to the HTML page.

    Code:
    d: {"Fee":"175.00","DatePaid":"06/18/2018"}
    This is the SPROC that is being called by that lookup web service

    Code:
    Create Procedure awc_TaskTasks_Lookup_LicType
    			  @LicType varchar(100)
    			 ,@EstName_hidden varchar(100)
    As
    Set NoCount On
    Declare @Class varchar(100)
    Declare @Capacity varchar(100)
    Select @Class=Class, @Capacity=Capacity
         From Establishment_T Where EstId=@EstName_hidden
    Select Cast(dbo.GetFee_F(@LicType,@Class,@Capacity) as varchar(100)) "Fee"
          , Convert(varchar(10),GetDate(),101) "DatePaid"
    This is the JavaScript call back function that fires when the JSON string comes back from the web server.

    Code:
    function ajaxLookupFinished(msg, sender, rtnstatus, objWebParam) {
        if (rtnstatus == "error") {
            errorMessage("ajaxLookupFinished", msg.responseText);
        } else if (rtnstatus == "failure") {
            errorMessage("ajaxLookupFinished", msg.responseText);
        } else {
            var objReturn = $.parseJSON(msg.d);
            if (objReturn.LoginRequired || false) {
                errorMessage("Login Required", "Session has timed out - please login again!");
            } else {
                if (!(typeof objReturn["%%dalerror%%"] == "undefined")) {
                    errorMessage("ajaxLookupFinished", objReturn["%%dalerror%%"]);
                } else if (!(typeof objReturn["%%errormessage%%"] == "undefined")) {
                    errorMessage("Lookup Message", objReturn["%%errormessage%%"]);
                } else {
                    fillEditPanel(objReturn, sender, "");
                }
            }
        }
    }
    Attached Images Attached Images  
    Last edited by szlamany; Jun 18th, 2018 at 03:48 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Re: A newbie Web Developer question?

    Maybe I'm getting to old for this stuff anymore.
    Blake

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,764

    Re: A newbie Web Developer question?

    I laid out a very standard UI requirement though - right? User selects a value from a drop down and we want to fill two others fields when that is done.

    In this example the user is selecting a SEASONAL license type and I want to fill in the FEE and set a default for the date paid so that if it was paid today the user doesn't have to fill it in.

    The STORED PROCEDURE is pretty standard. I've been writing SPROC's like this since 2000 - and used similar I/O techniques on mainframes since 1980 - never gets old

    What I've done in my app here is made it "general" so that the same LOOKUP service will work for any old lookup. So I pass a variety of values so that I can build up the NAME of the SPROC. Otherwise it's a standard SqlConnection->SqlCommand->SqlDataReader in a loop - if you've done SQL in VB.Net you should recognize 90% of that VB.Net code.

    What you need to learn new is JSON (it's just a standard like XML for transmitting key/value pairs of data). In the SqlDataReader loop I am using a JSON library to build the JSON string - which I've shown you - it's relatively simple stuff to look at - and once you get used to it, simple stuff to build.

    And of course JavaScript - the small amount of code I've shown just processes the return JSON checking for a variety of conditions (like timed out and what not). I avoided showing you the fillEditPanel function - but all it really does is look for TWO field names - FEE and DATEPAID and fills them with the values in the JSON string.
    Last edited by szlamany; Jun 18th, 2018 at 04:16 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Re: A newbie Web Developer question?

    Thanks brother, I appreciate it. If you were doing this in 1980, then you're at least 1 or 2 years older than me. Guess we're never to old. I'll take a look at your examples more closely.

    Thanks again!
    Blake

  8. #8
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,464

    Re: A newbie Web Developer question?

    which code is executed first, javascript/html or C# code-behind?
    C# First (think of this as your back end), then any front end web page stuff ( HTML / Javascript etc) second

    A variable you declare in C# is not necessary available in your JavaScript, you need to be looking at Session and Session variables if you want persistent data in ASP.Net web forms.

    The Way ASP.Net Web Forms works is by posting back to the server to re-run your C# code (which in turn re-creates your HTML Page), which is why there are various things you can check for instance IsPostback which is a boolean you can check that in your page load to see if it is your initial page load or a postback from one of your controls.

    I personally stopped using ASP.Net because I found that the "forced-to-be-just-like-a-WINFORM-event-model" to just be plain stupid.
    If your coming to Web programming from a desktop background actually it might make more sense to you initially, what you find as you learn more is that the event model is very inflexible and the constant postbacks are really painful when trying to maintain state. The more you learn the more you see the flaws in the model.

    If you have the time though Learning what is wrong with the Web forms approach and then going for something better is not a bad option.

    MS has created a better way of creating web apps which is still ASP.Net and that is MVC, which gets rid of that whole postback ajaxy event model.

    Its much more suited to modern web apps and uses the MVC pattern which was initially created in JAVA many years ago and has now been adopted by many other web languages

    I suspect you would quite like MVC Szlamany but i appreciate you have heavily invested in a JavaScript approach which achieves many of the same things.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,857

    Re: A newbie Web Developer question?

    Thanks for the insight NeedsSomeAnswers. Greatly appreciated!
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width