Results 1 to 7 of 7

Thread: [RESOLVED] postback problem?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Resolved [RESOLVED] postback problem?

    I am setting up a webpage on our intranet to search for xray history.
    Using ASP.NET in Visual Studio.
    When I run the program using the First and Last Name search it works fine.
    When I run the program using the Medical Record (MR) search....it works when I hit enter but not when I click on the "Find Patient" button the first time.
    When I click on the Find Patient button the first time it looks like it reloads the page and then the second time I click on the Find Patient button it works.
    So, I enter a different MR number and click Find...there is a little flash or refresh ...then if I click it again it will find the correct information.
    Hopefully I've described it well enough for you to help me.
    I think my problem is I am not checking for Page.IsPostBack.
    But how can I do that if I cannot do that on Load_Page because I need the users input?

    Here is my code:

    Code:
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    
    Public Class WebForm1
        Inherits System.Web.UI.Page
        'Protected WithEvents grdPatient As System.Web.UI.WebControls.DataGrid
        'Declare a global Connection object 
        Dim objConnection As SqlConnection
    
    #Region " Web Form Designer Generated Code "
    
        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
            'Put user code to initialize the page here
    
            'Initialize the Connection Object...
            'Set the connection string in the web.config for better security etc.
            objConnection = New SqlConnection
            objConnection.ConnectionString = ConfigurationSettings.AppSettings.Get("ConnectionString")
    
            'Open the connection to the SQL server
            objConnection.Open()
    
            'Call JavaScript Sub to set focus to the MedRec TextBox
            Set_Focus(txtGetMedRec)
    
        End Sub
    
        'Find Patient Button Execute
        '------------------------------------------------------------------------------------------------
        Private Sub btnFindPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindPatient.Click
            'Declare Objects...
            Dim objDataSet As DataSet
            Dim objDataAdapter As SqlDataAdapter
            Dim objSQLCmd As SqlCommand
            Dim objSQLDR As SqlDataReader
            Dim strLName As String
            Dim strFName As String
            Dim strMed_REC As String
    
            lblCantFindPat.Visible = False
            grdPatient.Dispose()    'Get rid of pre-existing data
            grdPatient.Visible = False  'Hide the grid for now
            grdPatient.DataBind()
    
            strFName = txtGetFName.Text & "%" 'Set the entry on the form to the parameter.  The % is a wildcard.
            strLName = txtGetLName.Text
    
            'Create a SQL Command Object to query Patient by Name
            objSQLCmd = New SqlCommand
    
            If txtGetMedRec.Text <> "" Then  'Search by Med Rec Number
                strMed_REC = txtGetMedRec.Text
                GetPatientByMedRec(strMed_REC)
    
            Else Then 'Search by Name
                'Define the command with the SQL Stored Procedure info
                With objSQLCmd
                    .CommandText = "sp_GetPatientByName" 'Running this Stored Procedure
                    .CommandType = CommandType.StoredProcedure
                    .Connection = objConnection  'Using this global defined connector
                    .Parameters.Add("@LName", SqlDbType.Char).Value = strLName  'set the parameter
                    .Parameters.Add("@FName", SqlDbType.Char).Value = strFName  'set the parameter
                End With
    
                'The next several lines populate the drop down list 
                ddlPatient.Visible = True
                objDataAdapter = New SqlDataAdapter(objSQLCmd)
                objDataSet = New DataSet
                objDataAdapter.Fill(objDataSet)
                ddlPatient.DataSource = objDataSet
                ddlPatient.DataTextField = "Full_Name"
                ddlPatient.DataValueField = "Med_Rec#"
                ddlPatient.DataBind()
                ddlPatient.Items.Insert(0, "Select a Patient")
                ddlPatient.AutoPostBack = True
    
        End Sub
    
        'This sub accepts the users selection on the drop down list for the Patient
        Private Sub ddlPatient_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlPatient.SelectedIndexChanged
            Dim strMed_REC As String
            ddlPatient.Visible = False
            strMed_REC = ddlPatient.SelectedItem.Value  'This sets the what the user selected to the variable
            GetPatientByMedRec(strMed_REC)  'Calling this procedure and passing it the MedRec# we got from the users
        End Sub
    
        Private Sub GetPatientByMedRec(ByVal strMed_Rec As String)
            'Declare objects.....
            Dim objSQLCmd As SqlCommand
            Dim objSQLDR As SqlDataReader
            Dim objDataSet As DataSet
            Dim objDataAdapter As SqlDataAdapter
            Dim strRsltMedRec As String
            Dim strRsltRad As String
            Dim strRsltPatient As String
            Dim strRsltSex As String
            Dim strRsltBirthDate As String
            Dim strRsltSocSec As String
            Dim strRsltLastExam As String
    
            'lblMedRec.Text = strMed_Rec
            objSQLCmd = New SqlCommand
            With objSQLCmd
                .CommandText = "sp_GetPatientByMedRec" 'Running this Stored Procedure
                .CommandType = CommandType.StoredProcedure
                .Connection = objConnection  'Using this global defined connector
                .Parameters.Add("@MedRec", SqlDbType.Char).Value = strMed_Rec  'set the parameter
            End With
            'Load the SQL DataReader
            objSQLDR = objSQLCmd.ExecuteReader
    
            If Not objSQLDR.Read() Then
                'Couldn't find the Medical Record #
                'Inform the user
                Exit Sub
            Else
                'Found a match on the Med Rec #
                'Place the query Results into the result strings
                strRsltMedRec = objSQLDR.GetString(0)
                strRsltRad = objSQLDR.GetString(1)
                strRsltPatient = objSQLDR.GetString(3) & ", " & objSQLDR.GetString(2) 'LName, FName
                strRsltSex = objSQLDR.GetString(4)
                strRsltBirthDate = objSQLDR.GetString(5)
                strRsltSocSec = objSQLDR.GetString(6)
                strRsltLastExam = objSQLDR.GetString(7)
    
                'Display Results on the web form...
                lblMedRec.Text = strRsltMedRec
                lblRad.Text = strRsltRad
                lblPatient.Text = strRsltPatient
                lblPatient.ForeColor = Color.Black
                lblPatient.Font.Bold = False
                lblSex.Text = strRsltSex
                lblBirthDate.Text = strRsltBirthDate
                lblSocSec.Text = strRsltSocSec
                lblLastExam.Text = strRsltLastExam
                objSQLDR.Close() 'Close the DataReader
    
                lblPat.Visible = True  'Enable the labels on the form.
                lblBD.Visible = True
                lblSx.Visible = True
                lblSS.Visible = True
                lblMRNum.Visible = True
                lblRadNum.Visible = True
                lblLastEx.Visible = True
    
                objSQLCmd = New SqlCommand
                With objSQLCmd  'Query for the Procedures
                    .Connection = objConnection
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "sp_GetProcByMedRec" 'Running this stored procedure
                    .Parameters.Add("@MedRec", SqlDbType.Char).Value = strMed_Rec 'This is the parameter
                End With
    
                'Initialize the DataSet object and fill the adapter 
                objDataAdapter = New SqlDataAdapter(objSQLCmd)
                objDataSet = New DataSet
                objDataAdapter.Fill(objDataSet, "Procedures")
                Dim objDataView As DataView = objDataSet.Tables("Procedures").DefaultView
                grdPatient.Visible = True
                grdPatient.DataSource = objDataView
                grdPatient.DataBind()
    
                'Clean up 
                objConnection.Close()   'Close the SQL Connection
                objConnection.Dispose() 'Clears Memory
                objDataAdapter.Dispose()
                objDataSet.Dispose()
    
                txtGetMedRec.Text = ""
                txtGetLName.Text = ""
                txtGetFName.Text = ""
                Set_Focus(txtGetMedRec)
            End If
        End Sub
    
        'This Sub is used to set focus to the control that is passed in (Med Rec TextBox)
        'ASP.NET is server side based...you can't do a normal SetFocus...you have to use JavaScript to do client side controls
        Private Sub Set_Focus(ByVal oControl As Control)
            Dim strScript As String
            strScript = "<script language=javascript> document.all('" & oControl.ID & "').focus() </script>"
            RegisterStartupScript("ClientSideScript", strScript)
        End Sub
    
    
        'This Sub lets you hit enter on the MedRec TextBox and it will act like the FindPatient button was clicked
        Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
            txtGetMedRec.Attributes.Add("onkeydown", String.Format("if(window.event.keyCode == 13) {{{0}.focus();}}", btnFindPatient.ClientID))
            txtGetLName.Attributes.Add("onkeydown", String.Format("if(window.event.keyCode == 13) {{{0}.focus();}}", btnFindPatient.ClientID))
            txtGetFName.Attributes.Add("onkeydown", String.Format("if(window.event.keyCode == 13) {{{0}.focus();}}", btnFindPatient.ClientID))
        End Sub
    End Class

    Yes, I am a Noob so don't feel bad for pointing out the mistakes!
    TIA - Jeff

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Re: postback problem?

    Here is the HTML:
    Code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="XrayHistory.aspx.vb" Inherits="XrayHistory.WebForm1" debug="True"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    	<HEAD>
    		<title>XRAY HISTORY</title>
    		<meta content="True" name="vs_showGrid">
    		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    		<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    		<meta content="VBScript" name="vs_defaultClientScript">
    		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    	</HEAD>
    	<body MS_POSITIONING="GridLayout">
    		<form id="Form1" method="post" runat="server">
    			<asp:datagrid id="grdPatient" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 280px"
    				runat="server" Width="760px" Height="112px" AlternatingItemStyle-BackColor="#ccffff" BackColor="White"
    				CellPadding="3" Font-Name="Verdana" Font-Size="8pt" GridLines="None" HeaderStyle-BackColor="#000099"
    				HeaderStyle-ForeColor="#ffffff" HeaderStyle-Font-Bold="true" Font-Names="Verdana">
    				<AlternatingItemStyle BackColor="#CCFFFF"></AlternatingItemStyle>
    				<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000099"></HeaderStyle>
    			</asp:datagrid>
    			<DIV id="Div1" title="XRAY HISTORY" dataFormatAs="html" style="DISPLAY: inline; FONT-WEIGHT: bold; FONT-SIZE: 20pt; Z-INDEX: 102; LEFT: 8px; WIDTH: 758px; COLOR: navy; POSITION: absolute; TOP: 8px; HEIGHT: 88px"
    				align="center" ms_positioning="FlowLayout">
    				<P>HUTCHINSON HOSPITAL
    				</P>
    				<P>XRAY HISTORY</P>
    			</DIV>
    			<asp:label id="lblPatient" style="Z-INDEX: 103; LEFT: 504px; POSITION: absolute; TOP: 104px"
    				runat="server" Width="222px" Height="16px" Font-Size="10pt" ForeColor="Black"></asp:label><asp:label id="lblMedRec" style="Z-INDEX: 104; LEFT: 504px; POSITION: absolute; TOP: 200px"
    				runat="server" Width="164px" Height="11px" Font-Size="10pt" ForeColor="Black"></asp:label><asp:label id="lblBirthDate" style="Z-INDEX: 105; LEFT: 504px; POSITION: absolute; TOP: 128px"
    				runat="server" Width="216px" Height="16px" Font-Size="10pt" ForeColor="Black"></asp:label><asp:label id="lblSex" style="Z-INDEX: 106; LEFT: 504px; POSITION: absolute; TOP: 152px" runat="server"
    				Width="76px" Height="16px" Font-Size="10pt" ForeColor="Black"></asp:label><asp:label id="lblLastExam" style="Z-INDEX: 107; LEFT: 504px; POSITION: absolute; TOP: 248px"
    				runat="server" Width="164px" Height="8px" Font-Size="10pt" ForeColor="Black"></asp:label><asp:label id="lblPat" style="Z-INDEX: 108; LEFT: 400px; POSITION: absolute; TOP: 104px" runat="server"
    				Width="32px" Height="16px" Font-Size="10pt" ForeColor="Navy">PATIENT:</asp:label><asp:label id="lblBD" style="Z-INDEX: 109; LEFT: 400px; POSITION: absolute; TOP: 128px" runat="server"
    				Width="80px" Height="16px" Font-Size="10pt" ForeColor="Navy">BIRTH DATE:</asp:label><asp:label id="lblSx" style="Z-INDEX: 110; LEFT: 400px; POSITION: absolute; TOP: 152px" runat="server"
    				Width="40px" Height="16px" Font-Size="10pt" ForeColor="Navy">SEX:</asp:label><asp:label id="lblMRNum" style="Z-INDEX: 111; LEFT: 400px; POSITION: absolute; TOP: 200px"
    				runat="server" Width="65px" Height="16px" Font-Size="10pt" ForeColor="Navy">MED REC#:</asp:label><asp:label id="lblLastEx" style="Z-INDEX: 112; LEFT: 400px; POSITION: absolute; TOP: 248px"
    				runat="server" Width="80px" Height="16px" Font-Size="10pt" ForeColor="Navy">LAST EXAM:</asp:label><asp:label id="lblRad" style="Z-INDEX: 113; LEFT: 504px; POSITION: absolute; TOP: 224px" runat="server"
    				Width="160px" Height="8px" Font-Size="10pt"></asp:label><asp:label id="lblRadNum" style="Z-INDEX: 114; LEFT: 400px; POSITION: absolute; TOP: 224px"
    				runat="server" Width="48px" Height="16px" Font-Size="10pt" ForeColor="Navy">RAD#:</asp:label><asp:label id="lblSocSec" style="Z-INDEX: 115; LEFT: 504px; POSITION: absolute; TOP: 176px"
    				runat="server" Width="152px" Height="16px" Font-Size="10pt"></asp:label><asp:label id="lblSS" style="Z-INDEX: 116; LEFT: 400px; POSITION: absolute; TOP: 176px" runat="server"
    				Width="68px" Height="16px" Font-Size="10pt" ForeColor="Navy">SOC SEC#:</asp:label><asp:textbox id="txtGetMedRec" style="Z-INDEX: 117; LEFT: 56px; POSITION: absolute; TOP: 120px"
    				tabIndex="1" runat="server" Width="96px" ToolTip="Enter the 6 digit Medical Record Number (No Dash)" AutoPostBack="True"></asp:textbox><asp:button id="btnFindPatient" style="Z-INDEX: 118; LEFT: 56px; POSITION: absolute; TOP: 216px"
    				tabIndex="4" runat="server" Width="96px" ToolTip="Enter a Med Rec # and click here to search the database." Text="Find Patient"></asp:button><asp:textbox id="txtGetFName" style="Z-INDEX: 119; LEFT: 56px; POSITION: absolute; TOP: 184px"
    				tabIndex="2" runat="server" Width="96px" ToolTip="Enter the patient's first name."></asp:textbox><asp:textbox id="txtGetLName" style="Z-INDEX: 120; LEFT: 160px; POSITION: absolute; TOP: 184px"
    				tabIndex="3" runat="server" Width="96px" ToolTip="Enter the patient's last name.  % is the wildcard.  "></asp:textbox><asp:label id="lblMR" style="Z-INDEX: 121; LEFT: 56px; POSITION: absolute; TOP: 104px" runat="server"
    				Width="96px" Height="16px" Font-Size="10pt" ForeColor="DarkBlue">MEDICAL REC #:</asp:label><asp:label id="Label6" style="Z-INDEX: 122; LEFT: 56px; POSITION: absolute; TOP: 144px" runat="server"
    				Width="16px" Height="15px" Font-Size="10pt" ForeColor="Navy">OR</asp:label><asp:label id="Label8" style="Z-INDEX: 123; LEFT: 56px; POSITION: absolute; TOP: 168px" runat="server"
    				Width="80px" Height="8px" Font-Size="10pt" ForeColor="Navy">FIRST NAME:</asp:label><asp:label id="Label10" style="Z-INDEX: 124; LEFT: 160px; POSITION: absolute; TOP: 168px" runat="server"
    				Width="80px" Height="8px" Font-Size="10pt" ForeColor="Navy">LAST NAME:</asp:label><asp:label id="lblCantFindPat" style="Z-INDEX: 125; LEFT: 160px; POSITION: absolute; TOP: 224px"
    				runat="server" Width="224px" Height="8px" Font-Size="10pt"></asp:label><asp:dropdownlist id="ddlPatient" style="Z-INDEX: 127; LEFT: 56px; POSITION: absolute; TOP: 248px"
    				runat="server" Width="192px" Height="24px" AutoPostBack="True"></asp:dropdownlist></form>
    	</body>
    </HTML>

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: postback problem?

    After a quick glance at the code I'm not sure exactly why, but you can start by placing the code in your form load event in a postback check. If it is NOT a postback, then let that code be executed.

    After that, set a breakpoint on the button click event and try stepping through the code to see what happens the first and second time.

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Re: postback problem?

    Why does it go back through page_load when I click on the Find Patient button?

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: postback problem?

    Because the information is being posted back (submitted) to the same page. The Page_Load event will always fire.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Re: postback problem?

    Ok, that makes sense.

    I stepped through it.
    The first time I search by MedRec and click on the Find Patient button it works fine.
    Then if I try to do another search.
    The first time I try it...the button immediately fires and runs the Page_Load...then the second time I click the button it works correctly. One thing I notice when I click the first time it doesn't react like a normal click...I mean I can't click and hold down the button like a normal click...it just immediately does the Page_Load. Any ideas there?
    It does not react that way when I search by Name.

    One other thing I notice...if I do the search using the "Enter" key (running the PreRender Sub)...it works fine. Searches fine on each "Enter".

    Thanks again for the assistance.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Re: postback problem?

    I found the txtGetMedRec had AutoPostBack=True.
    I changed that to false and it seems to be working like I want it to.
    Thanks for the help.

Posting Permissions

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



Click Here to Expand Forum to Full Width