Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 2nd, 2011, 12:25 PM
Registered User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Question Chapter 8 user control properties

As an exercise with no practical value, I created: a user control with a label and a textbox; a user control property to control the width of the textbox (shorter or longer); and code to make the width (and backcolor) of the textbox alternate from shorter to longer each time there is a postback. It works if the code tests the current backcolor value to determine how to set the new user control property, but it does not work if the code tests the current user control property value to determine how to set the new user control property. What am I missing? Here is the code:

Created the user control ucLabelTextboxSeven.ascx:
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ucLabelTextboxSeven.ascx.vb" Inherits="Controls_ucLabelTextboxSeven" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
&nbsp;
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Created typeWidth.vb:
Code:
Public Enum typeWidth
  Shorter = 0
  Longer = 1
End Enum
Created page 7UserControlProperty.aspx:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="7UserControlProperty.aspx.vb" Inherits="_7UserControlProperty" %>

<%@ Register src="Controls/ucLabelTextboxSeven.ascx" tagname="ucLabelTextboxSeven" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <br /><br />
      <asp:Button ID="buttonPostback" runat="server" Text="Postback" />
      <br /><br />
      <uc1:ucLabelTextboxSeven ID="ucLabelTextboxSeven1" runat="server" />
      <br /><br />
    </div>
    </form>
</body>
</html>
Created ucpTextboxWidth Property in ucLabelTextboxSeven.ascx.vb:
Code:
Partial Class Controls_ucLabelTextboxSeven
    Inherits System.Web.UI.UserControl
  Public Property ucpTextboxWidth As typeWidth
End Class
Initialized ucpTextboxWidth property in the page 7UserControlProperty.aspx:
Code:
<uc1:ucLabelTextboxSeven ID="ucLabelTextboxSeven1" runat="server"  ucpTextboxWidth="Shorter" />
Coded the Page_Load event in ucLabelTextboxSeven.ascx.vb:
Code:
Partial Class Controls_ucLabelTextboxSeven
    Inherits System.Web.UI.UserControl
  Public Property ucpTextboxWidth As typeWidth

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack Then
      If TextBox1.BackColor = Drawing.Color.Yellow Then
        ucpTextboxWidth = typeWidth.Shorter
      Else : ucpTextboxWidth = typeWidth.Longer
      End If
    End If
    Select Case ucpTextboxWidth
      Case typeWidth.Shorter
        TextBox1.Width = "200"
        TextBox1.BackColor = Drawing.Color.White
        Label1.BackColor = Drawing.Color.White
      Case typeWidth.Longer
        TextBox1.Width = "400"
        TextBox1.BackColor = Drawing.Color.Yellow
        Label1.BackColor = Drawing.Color.Yellow
    End Select
  End Sub
End Class
It worked as expected.

However, changing the above code to test the user control property instead of testing the backcolor does not succeed in alternating the textbox width with each click of buttonPostback!
Code:
Partial Class Controls_ucLabelTextboxSeven
    Inherits System.Web.UI.UserControl
  Public Property ucpTextboxWidth As typeWidth

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack Then
      If ucpTextboxWidth = typeWidth.Longer Then  ' *** testing user control property here does not work.  Why?
        ucpTextboxWidth = typeWidth.Shorter
      Else : ucpTextboxWidth = typeWidth.Longer
      End If
    End If
      Select Case ucpTextboxWidth
        Case typeWidth.Shorter
          TextBox1.Width = "200"
          TextBox1.BackColor = Drawing.Color.White
          Label1.BackColor = Drawing.Color.White
        Case typeWidth.Longer
          TextBox1.Width = "400"
          TextBox1.BackColor = Drawing.Color.Yellow
          Label1.BackColor = Drawing.Color.Yellow
      End Select
  End Sub
End Class
Thank you for your help.
Marshall

Last edited by MarshallN; December 3rd, 2011 at 04:54 PM.. Reason: highlighted visual basic line in question.
Reply With Quote
  #2 (permalink)  
Old December 4th, 2011, 06:24 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Can you define "does not work"? Do you get an error? Or does it have another value than expected? Did you debug the code and look at the property?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old December 4th, 2011, 04:14 PM
Registered User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Question user control problem - continued

When I wrote "It worked as expected" I meant:
when I opened the page 7UserControlProperty.aspx in Internet Explorer, I saw the buttonPostback, the Label with a white backcolor, and the Textbox with a white backcolor and a width of 200px;
When I clicked the Postback button, I saw the Label with a yellow backcolor, and the Textbox with a yellow backcolor and a width of 400;
When I clicked the Postback button again, I saw the Label return to its original white backcolor, and the Textbox return to a white backcolor and a width of 200;
Each time I clicked the Postback button, the Label's backcolor and the Textbox's backcolor and length changed.

When I changed the code in the Page_Load event in ucLabelTextboxSeven.ascx.vb
from "If TextBox1.BackColor = Drawing.Color.Yellow Then"
to "If ucpTextboxWidth = typeWidth.Longer Then",
then opening the page 7UserControlProperty.aspx in Internet Explorer behaved differently. Now, when I repeatedly click the Postback button, the Label's backcolor remains yellow and the Textbox's backcolor remains yellow and its width remains Longer (400px). That's what I meant by "not working" – shouldn't the page behave in the same way no matter which If-clause I use?.

As for debugging, I jumped to Chapter 18 and your description of setting a breakpoint. I set the breakpoint on the line "If Page.IsPostBack Then" and opened the page 7UserControlProperty.aspx in Internet Explorer by using the key F5. The page opens with ucpTextboxWidth = typeWidth.Shorter, as expected because the Textbox is initialized with this value in the page. When I clicked on buttonPostback and look at the breakpoint ucpTextboxWidth again equals typeWidth.Shorter and then, stepping through the Page_Load routine, I saw the ucpTextboxWidth change to Longer. After stepping through the rest of the Page_Load routine, IE displayed the Textbox with a width of 400px (Longer) and backcolor = yellow as expected. Clicking on buttonPostback a second time brought me back to the Page_Load rountine and ucpTextboxWidth should have been equal to typeWidth.Longer. But it was reset back to the Shorter value and therefore resulted in the Textbox's width being set to Longer again and again with each click of the Postback button.

I see the user control ucLabelTextboxSeven is initialized in the page 7UserControlProperty.aspx with ucpTextboxWidth="Shorter" and so, I assume, every Page_Load finds the user control property ucpTextboxWidth re-initialized with the Shorter value. On the other hand, the backcolor of the Textbox remains with the value that Page_Load set it to previously and so toggling works if TextBox1.backcolor is tested instead of ucpTextboxWidth.

So, I removed initializing ucpTextboxWidth="Shorter" from 7UserControlProperty.aspx:
Code:
      <uc1:ucLabelTextboxSeven ID="ucLabelTextboxSeven1" runat="server"  />
I added code to the Page_Load routine of ucLabelTextboxSeven.ascx.vb to initialize ucpTextboxWidth the first time, before postback:
Code:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack Then
      If ucpTextboxWidth = typeWidth.Longer Then  
        ucpTextboxWidth = typeWidth.Shorter
      Else : ucpTextboxWidth = typeWidth.Longer
      End If
    Else : ucpTextboxWidth = typeWidth.Shorter ' initial setting.
    End If
    Select Case ucpTextboxWidth
      Case typeWidth.Shorter
        TextBox1.Width = "200"
        TextBox1.BackColor = Drawing.Color.White
        Label1.BackColor = Drawing.Color.White
      Case typeWidth.Longer
        TextBox1.Width = "400"
        TextBox1.BackColor = Drawing.Color.Yellow
        Label1.BackColor = Drawing.Color.Yellow
    End Select
  End Sub
It still does not work! That is, repeated clicking of buttonPostback does not result in the toggling of the Textbox width and backcolor, i.e., once the Postback button is clicked, the Textbox width is Longer and the backcolor is yellow and the Textbox remains that way.

I again set the breakpoint to the first line of the Page_Load event and opened the page 7UserControlProperty.aspx in IE. I saw that even the very first time, before any postback, the user control property ucpTextboxWidth was already initialized to typeWidth="Shorter" before taking any steps through the routine. With every click of buttonPostback the Page_Load routine immediately begins with typeWidth="Shorter".

Thanks for your help.
Marshall
Reply With Quote
  #4 (permalink)  
Old December 4th, 2011, 04:38 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
shouldn't the page behave in the same way no matter which If-clause I use?.
If I understand you correctly (which I am not sure off; all these funky control, type and variable names make your code and problem description a bit hard to read), then no: it shouldn't and what you see actually makes sense.

The Web Control properties such as Color persist their value in ViewState so its maintained across postbacks. Your property loses its value after each postback.

Take a look at the NavigateUrl property on page 289 to see how to convert your property to a ViewState property so it can maintain its value as well.

Cheers.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old December 4th, 2011, 07:02 PM
Registered User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Smile used the view state collection

Dear Imar,

My apologies for the long names.

I added this code to use the ViewState collection in ucLabelTextboxSeven.ascx.vb (the user control's code behind file):
Code:
  Public Property ucpTextboxWidth As typeWidth
    Get
      Dim _ucpTextboxWidth As Object = ViewState("ucpTextboxWidth")
      If _ucpTextboxWidth IsNot Nothing Then
        Return _ucpTextboxWidth
      Else : Return typeWidth.Shorter
      End If
    End Get
    Set(ByVal value As typeWidth)
      ViewState("ucpTextboxWidth") = value
    End Set
  End Property
It worked as expected.

Learning ASP may be a struggle, but your book and the p2p forum that you monitor so frequently make the struggle enjoyable. Thank you very much.
Marshall
Reply With Quote
  #6 (permalink)  
Old December 5th, 2011, 06:18 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
My apologies for the long names.
No need to. It's *you* who has to look at it the most.... ;-)

Quote:
I added this code to use the ViewState collection in ucLabelTextboxSeven.ascx.vb
Yep, that makes sense. You may want to add a CType to the code:

Code:
 
Return CType(_ucpTextboxWidth, typeWidth)
Quote:
Learning ASP may be a struggle, but your book and the p2p forum that you monitor so frequently make the struggle enjoyable. Thank you very much.
You're welcome!

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 8 User control avs BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 18 December 29th, 2009 09:17 PM
Chapter 8:User Control Arya BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 December 18th, 2009 04:37 PM
setting Collection properties for user control PurpleHaze VS.NET 2002/2003 2 December 30th, 2008 02:41 AM
Chane Properties of a user control from MasterPage slim182 ASP.NET 2.0 Basics 1 February 26th, 2007 09:54 PM
Referencing a user control properties trekmp General .NET 2 April 7th, 2004 08:40 AM



All times are GMT -4. The time now is 01:52 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.