Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 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 March 4th, 2005, 05:22 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default Moving to ASP.NET

I have a quandary.

I am working through Apress’ book Moving to ASP.NET: Web Development With VB .NET, and I have been taking SkillSoft online courses, reading the posts here (and answering some). After all of that, I got the idea that clicking a server control button submits the page’s form, and resends the page to the browser.
Upon experimentation, I see that this is true.

When I click on a server control button, IE’s globe starts spinning, and it keeps spinning while the server-code is paused.
When I let the server proceed, the web page is sent to the browser all over again. Ie, the text-box has its original value all over again.

Apparently if you want to use server-side code for buttons and such, you need to take the state of the controls, and echo that back to the browser.
(It seems that this in turn will modify what each control’s “defaultValue” is, but that's not a “show-stopper.”)

I created:
Code:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="test_events.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
   <HEAD>
      <title>WebForm1</title>
      <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
      <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
   </HEAD>
   <body MS_POSITIONING="GridLayout" bgColor="#b9e9ff">
      <form id="Form1" method="post" runat="server">
         <asp:Button id="WbFrms_Sect_Btn" 
              style="Z-INDEX: 10; LEFT: 98px; POSITION: absolute; TOP: 50px"
              runat="server" Width="160px" Height="32px" 
              Text="From Web Forms Sect."></asp:Button>
         <INPUT id="Box1" 
             style="Z-INDEX: 20; LEFT: 98px; WIDTH: 160px; POSITION: absolute; TOP: 90px; HEIGHT: 24px"
             type="text" size="21" value="Box 1">
      </form>
   </body>
</HTML>
I put a breakpoint in Sub WbFrms_Sect_Btn_Click() to examine the environment, and ran it.

Sure enough, if I change the contents of the text box, when I click WbFrms_Sect_Btn the breakpoint is triggered (with IE’s globe spinning around). When I let the code finish, the textbox gets it original value put back into it.

So.

If I want to run a click event (or whatever) through the auspices of a server control, how do I maintain the values in the controls that the user has thus far modified?
BTW: I tried to examine the value in the textbox with Request.Form("Box1") to no avail. Thimble, thimble: Where is the thimble? (Where is that value?)

Also, If I don’t want to use data binding for the value of a given control (let’s just say a textbox for simplicity’s sake), how do I dynamically set its value?

I am a skilled VB6 Webclass programmer, but I’m having a little trouble with understanding how to accomplish things that I know can be done, in this new environment.
 
Old March 8th, 2005, 03:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi Brian,

It looks like you are using a standard input box for your textbox. The easiest way for you to manipulate the values within textboxes are to use ASP.Net's WebControls. Within those controls you will find a textbox. Drag one onto your form.

Then from the code behind you should be able to modify it's properties. Just a simple example:

TextBox1.Text = "BlahBlah"

assuming that the name of your textbox is "Textbox1". Hope this helps.

J
 
Old March 9th, 2005, 04:04 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

It does help some.

After posting this Q I started to see that that was where the book was trying to steer people.

Almost everything I have is in VB6 WebClasses, which (of course) use standard HTML controls, filling in the values by changing the contents of a pre-defined tag at the time the page is requested.

Isn’t there a way to duplicate this model of dynamically filling in the contents of a page being sent to the browser? (It sounds like maybe there is, as you say “The easiest way . . . ” is with webcontrols, holding out the possibility that there is a less easy way?)
 
Old March 10th, 2005, 01:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

You could put what I showed you:

TextBox1.Text = "BlahBlah"

in the Page_Load event of your code behind. When your page loads, the text of "TextBox1" would be "BlahBlah". Is this what you are looking for, or am I missing the boat?

J
 
Old March 10th, 2005, 12:58 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Doesn’t that only work with controls that are runat=server? (That makes them behave a little differently, and loads more data into the hidden control that animates that feature.)

What triggered this question is that, if you click a webcontrol button, you make a round trip to the server to run its _Click() event. Any textboxes that have had their value changed will now be reset to their initial value.
So I guess that under those circumstances you would read what values were submitted on this ‘trip,’ and put those values into the controls that are being returned. How do you get that value to return it to the browser?
 
Old March 10th, 2005, 04:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Those values will only be changed if you want them to be changed, otherwise the state of the controls will be maintained. I'll give a little more clarification. If you add code to the Page_Load event to set Textbox1's text to a certain value, then yes it will change back to that value every time you post back to the server.

If, however, you tell it to only set that value the first time posting to the server, it won't. You do that by using the IsPostBack method. Try this. Add another webcontrol textbox to your form and call it TextBox2. Then add this to your Page_Load event:

'----------------------------------------------
If Not IsPostBack Then
    TextBox1.Text = "BlahBlah"
End If
'----------------------------------------------

In your button click event add this:

'----------------------------------------------
TextBox2.Text = "BlahBlah2"
'----------------------------------------------

The first time you load the page, you should see "BlahBlah" in TextBox1, Textbox2 should have nothing. Change the text of TextBox1 to "Test" and click the button. TextBox1 will maintain its state and keep "Test". TextBox2, however, will be set to "BlahBlah2".

If you have used VB6 for Windows applications development, you should love this because it acts more like a Windows application than a Web app. This is possible because ASP.Net puts a hidden field along with your code that helps it to maintain state. If you look at your source code after your run your page you should see something like:

<input type="hidden" name="__VIEWSTATE" value="dDwxMjI3OTM5ODYxOzs+IZ4GE/WtG/2E0qo/i81UJBoAzXo=" />

This is how it maintains the values on your form. And yes, that only works when the control has runat=server specified.

Does this clarify it better?

J
 
Old March 10th, 2005, 07:41 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Yes, that ‘IsPostBack’ is interesting. There is a lot of potential there.

But just for curiosity’s sake:

  1 If a control is not runat=server, then directly setting it on a 'roundtrip' is not supported?
  2 Are the non-runat=server controls’ values available?
     Eg, isn’t there a Request.Form collection when a webcontrol’s _Click() event is triggered?
 
Old March 10th, 2005, 10:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Take a look at this page:

http://www.dotnetbips.com/displayarticle.aspx?id=8

It gives examples of using both web controls and html controls, but yes runat=server attribute is set. Another site here:

http://www.15seconds.com/issue/030211.htm

gives an example of using the Request.Forms collection to post to alternate pages, but I think you will get the idea. I personally can't remember the last time that I used this in one of my pages (I think it was when I was still doing classic ASP). You may be able to get a better answer for this in the ASP.Net forum, but they will likely tell you the same thing - that is, to try and move away from this methodology. Cheers.

J





Similar Threads
Thread Thread Starter Forum Replies Last Post
Turning a regular ASP.NET App into an AJAX ASP.Net donrafeal7 Ajax 2 August 31st, 2007 12:33 AM
ASP.Net Moving Chart to Excel Sheet 2 sili18 General .NET 0 June 15th, 2007 12:39 PM
Moving an asp.net application to server douganders VB.NET 2002/2003 Basics 3 May 13th, 2005 02:54 PM
Moving to ASP.NET BrianWren Classic ASP Basics 0 March 4th, 2005 06:22 PM
Moving Between ASP and ASPX Yehuda ASP.NET 1.0 and 1.1 Basics 1 August 13th, 2003 04:19 PM





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