Results 1 to 3 of 3

Thread: Writing client info to Server Control

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Calgary, AB Canada
    Posts
    42

    Writing client info to Server Control

    The following is the only method I have found to call a DialogBox with ASP.NET

    I have a server side button that has client side code that runs when it is clicked.

    <MainForm.aspx>

    Code:
    <script...
    Sub cmdCust_OnClick
      Dim sCustomers, sOptions
    				
      sOptions = "dialogHeight: 325px; dialogWidth: 400px; Center: Yes; Scroll: No; Status: No; Help: No;"
    	
      sCustomers = window.showModalDialog("customersframe.htm",,sOptions)
      Document.getElementById("lblInfo").innertext = sCustomers
    End Sub
    ..\script>
    The value returned from the dialogbox is written to "lblInfo" which is a server side label.

    The Page_Load event contains the following code

    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      If (IsPostBack) Then
        Dim sCust As String
    
        sCust = lblInfo.Text
      end if
    End Sub
    The lblInfo.Text is empty. Even though it is a server side control, and the text even appears on the form briefly while stepping through the code.

    I don't understand why this control contains no data?

    I have tried to use a txtbox control as well, with the same result. I also tried to read the value of the lable in the .VB cmdCust.Click(...) event. (By stepping through the code, I realized client side code is run first)

    In every case this field is empty and I cannot get the string from the Dialog box to the form. What am I doing wrong??

  2. #2
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: Writing client info to Server Control

    Quote Originally Posted by Burrick
    The following is the only method I have found to call a DialogBox with ASP.NET

    I have a server side button that has client side code that runs when it is clicked.

    <MainForm.aspx>

    Code:
    <script...
    Sub cmdCust_OnClick
      Dim sCustomers, sOptions
    				
      sOptions = "dialogHeight: 325px; dialogWidth: 400px; Center: Yes; Scroll: No; Status: No; Help: No;"
    	
      sCustomers = window.showModalDialog("customersframe.htm",,sOptions)
      Document.getElementById("lblInfo").innertext = sCustomers
    End Sub
    ..\script>
    The value returned from the dialogbox is written to "lblInfo" which is a server side label.

    The Page_Load event contains the following code

    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      If (IsPostBack) Then
        Dim sCust As String
    
        sCust = lblInfo.Text
      end if
    End Sub
    The lblInfo.Text is empty. Even though it is a server side control, and the text even appears on the form briefly while stepping through the code.

    I don't understand why this control contains no data?

    I have tried to use a txtbox control as well, with the same result. I also tried to read the value of the lable in the .VB cmdCust.Click(...) event. (By stepping through the code, I realized client side code is run first)

    In every case this field is empty and I cannot get the string from the Dialog box to the form. What am I doing wrong??
    First use Not Is PostBack..its cleaner and you should ONLY be executing that code when it isnt a post back NOT when it is.

    So change that around and you should get the results...because your code is saying replace the text box on a post back...

    Jon

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2001
    Location
    Calgary, AB Canada
    Posts
    42

    Re: Writing client info to Server Control

    I've done some testing and here is the outcome.

    I am using Microsoft Visual Studio .Net 2003 as my Development Environment

    Create a test form named test.aspx
    Add a server side, Label named "lblTest" to this form
    Add a server side, Button named "cmdTest" to the same form

    In "test.aspx.vb"

    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      if (IsPostBack = True) then
        if (lblTest.Text.Length >0) then
          Dim sText as string
    
          sText = lblTest.Text ' Note: Place a breakpoint at this line
        end if
      end if
    End Sub
    
    Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
      lblTest.Text = "Server Side" ' Note: Place a breakpoint at this line
    End Sub
    In "Test.aspx" HTML

    Code:
    <HTML>
      <HEAD>
        <title>Test</title>
        <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">
        <SCRIPT language="vbscript">
          Sub cmdTest_OnClick()
            Document.getElementByID("lblText").InnerText = "Client Side"
            Msgbox "I am here"
          End Sub
        </SCRIPT>
      </HEAD>
    ... (Remaining HTML CODE)
    Run the form in Debug mode so that you can step through it. If you have placed the breakpoints you should observe the following.

    When you first run it, the Form appears and code does not stop anywhere. This is as expected.

    Click the Test Button

    A Message Box will pop up with the words "I am here"
    The label will display the text "Client Side"
    <Click on the Ok button in the message box to clear it>
    The code will break in the Page_Load event since IsPostBack is now True
    Notice that lblTest.Text is empty "" ..
    <Click on the "continue" button or step through the code>
    The code will now break in the cmdText_Click() event.
    I find it peculiar that the post back is triggered before the click event is serviced
    <Continue stepping through the code>
    The label will now display "Server Side"

    At this time processing is complete

    Click on the cmdTest button a second time.

    Once again the Label text appears to be "Client Side" and the message box appears.

    This time when the code breaks in the Page_Load event you will observe that lblTest.Text is "Server Side" and not empty. This is due to it having been changed by the cmdTest_click event "after the page reloaded last time"

    The last break occurs at cmdTest_click where it overwrites lblTest.Text with "Server Side" (a moot point, since this is the value it is already set to)

    I have tried to use innerTest, InnerHTML, OuterText and OuterHTML, etc.. in the client side code. To no avail.

    Writing to a server side control from client side code will display on the page but not actually "write" the data to any part of the component that can be "seen" by server side code.

    The oddest being that the PostBack event is serviced before the button_click event which triggered it ..

    So what's the point of this whole venture?

    I want to be able to pass a value from client to server. I can get this page to work by creating the returning value as a query and resubmitting the form. Of course this is a drawback because the user can "see" what has been posted, and there is a limitation on how long the string can be.

    Any ideas?

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