User Control Postback problem

Last post 09-12-2003 4:50 PM by schilm. 1 replies.

Sort Posts:

  • User Control Postback problem

    09-12-2003, 3:45 PM
    • Member
      60 point Member
    • schilm
    • Member since 07-12-2002, 6:54 AM
    • Boston, MA
    • Posts 12
    I've run into a problem with user controls that I cannot for the life of me figure out. I have simplified it and removed all the extraneous code in the example below

    In this example, I dynamically load a user control. The user control has a checkbox, a label and a button that posts back. In the pageload event I set the label equal to the checked value of the checkbox. However, no matter whether the checkbox is checked or not, the label will show the value false. When this exact same code is put into a webform the label will show the correct value.

    Has anyone ever seen this before? Are there any work arounds, or, even better, can anyone explain why this is happening??

    Thanks in advance,
    Max

    CODE for user control:
    --------------------------

    Public Class UCTest
    Inherits System.Web.UI.UserControl

    Protected WithEvents UC_CBox As System.Web.UI.WebControls.CheckBox
    Protected WithEvents UC_Lbl As System.Web.UI.WebControls.Label
    Protected WithEvents UC_Btn As System.Web.UI.WebControls.Button

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    UC_Lbl.Text = UC_CBox.Checked
    End Sub

    End Class


    DISPLAY for user control
    -----------------------------

    <%@ Control Language="vb" AutoEventWireup="false" Codebehind="usercntrl.ascx.vb" Inherits="UCTest" TargetSchema="http://schemas.microsoft.com/intellisense/ie3-2nav3-0" %>
    <asp:CheckBox id=UC_CBox text="User Control CBox" runat="server"></asp:CheckBox>
    <br>
    <br>
    CheckBox Value:&nbsp;<asp:Label id=UC_Lbl runat="server"></asp:Label>
    <br>
    <br>
    <asp:Button id=UC_Btn runat="server" Text="Update"></asp:Button>
    <br>


    CODE for web form
    -----------------------

    Public Class FormTest
    Inherits System.Web.UI.Page

    Protected WithEvents WebFormCBox As System.Web.UI.WebControls.CheckBox
    Protected WithEvents WebFormLbl As System.Web.UI.WebControls.Label
    Protected WithEvents WebFormBtn As System.Web.UI.WebControls.Button
    Protected WithEvents WebFormPnl As System.Web.UI.WebControls.Panel

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim myusercontrol As Control
    myusercontrol = Page.LoadControl("usercntrl.ascx")
    WebFormPnl.Controls.Add(myusercontrol)

    WebFormLbl.Text = WebFormCBox.Checked

    End Sub

    End Class


    DISPLAY for Web Form
    --------------------------

    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="FormTest"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
    <meta content="Visual Basic .NET 7.1" name=CODE_LANGUAGE>
    <meta content=JavaScript name=vs_defaultClientScript>
    <meta content=http://schemas.microsoft.com/intellisense/ie3-2nav3-0 name=vs_targetSchema>
    </HEAD>
    <body MS_POSITIONING="FlowLayout">
    <form id=Form1 method=post runat="server">
    <h1>Stuff From User Control</h1>
    <asp:panel id=WebFormPnl runat="server"></asp:panel>
    <h1>Stuff From Web Form</h1>
    <asp:CheckBox id=WebFormCBox text="WebForm CBox" runat="server"></asp:CheckBox>
    <br>
    <br>
    CheckBox Value:&nbsp;<asp:Label id=WebFormLbl runat="server"></asp:Label>
    <br>
    <br>
    <asp:Button id=WebFormBtn runat="server" Text="Update"></asp:Button>
    </form>
    </body>
    </HTML>

  • Re: User Control Postback problem

    09-12-2003, 4:49 PM
    • Member
      60 point Member
    • schilm
    • Member since 07-12-2002, 6:54 AM
    • Boston, MA
    • Posts 12
    I was able to find a solution to my problem.

    I moved the code that dynamically loads the control in to the OnInit Sub in the Web Form rather than the Page_Load Sub.


    So the code for the working WebForm is:
    ---------------------------------------------


    Public Class FormTest
    Inherits System.Web.UI.Page

    Protected WithEvents WebFormCBox As System.Web.UI.WebControls.CheckBox
    Protected WithEvents WebFormLbl As System.Web.UI.WebControls.Label
    Protected WithEvents WebFormBtn As System.Web.UI.WebControls.Button
    Protected WithEvents WebFormPnl As System.Web.UI.WebControls.Panel

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
    Dim myusercontrol As Control
    myusercontrol = Page.LoadControl("usercntrl.ascx")
    WebFormPnl.Controls.Add(myusercontrol)

    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    WebFormLbl.Text = WebFormCBox.Checked

    End Sub

    End Class
Page 1 of 1 (2 items)