Professional Web Applications Themes

Manipulating a User Control from another User Control - ASP.NET General

This one has me a bit confused and I'm not finding what I need in the MSDN or by searching these forums, so here goes... I have a rather large, complex code-in-page WebForm (don't ask) and a section of that Form is 4 or 5 ASP:Panels pretending to be a set of Tabs, each with its own section of the form. In order to simplify managing this page, since code- behind is not an option (don't ask, not my fault), I thought I had the solution... I would convert each Panel into a User Control. The problem is that the ...

  1. #1

    Default Manipulating a User Control from another User Control

    This one has me a bit confused and I'm not finding what I
    need in the MSDN or by searching these forums, so here
    goes...


    I have a rather large, complex code-in-page WebForm
    (don't ask) and a section of that Form is 4 or 5
    ASP:Panels pretending to be a set of Tabs, each with its
    own section of the form.

    In order to simplify managing this page, since code-
    behind is not an option (don't ask, not my fault), I
    thought I had the solution...

    I would convert each Panel into a User Control.
    The problem is that the panels usually depend on at least
    one Form Control from at least one other Panel.

    I already know about RaiseBubbleEvent() and OnBubbleEvent
    () and have used them elsewhere, but they only work going
    UP the container heirarchy.

    I need to :
    1 - (partially) handle an event in the UserControls
    (panels), then (partially handle the same event in
    ANOTHER UserControl(panel) on the same form/page.

    2 - handle an event in a UserControl(panel) BUT be able
    to retrieve values from form fields(Controls) in OTHER
    UserControls(panels) to help determine how to handle the
    event.

    3 - (partially) handle an event in the Form(aspx)
    depending on values of form controls in the UserControls

    4 - manipulate values of form controls in the
    UserControls


    Like I said, I already know how to Bubble events, but
    that only goes up.
    The part I am missing is how to "Elbbub" events (bubble,
    backwards) or manipulate data in child objects.

    The trick is, this is all Code-in-Page and nothing will
    convince those in control to return to Code Behind.

    Anyone know how to do what I want/need to do?

    I have a set of aspx/ascx pages that I can email or ftp
    for anyone interested.
    Tom Rowton Guest

  2. #2

    Default Re: Manipulating a User Control from another User Control

    Not sure what you mean by "codebehind is not an option." It SOUNDS like what
    you mean is that compiled CodeBehind DLLs are not an option. You can still
    use CodeBehind, just put it in the same file as the Page Template. You can
    put it right at the top of the Page, in a "runat=server" script. Example:

    <% Page language="C#" %>
    <script runat="server" >
    public void Page_Load()
    {
    // Page_Load code goes here
    }
    </script>
    <html>
    ....Page Template HTML and Code Goes here
    </html>

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    [url]http://www.takempis.com[/url]
    Complex things are made up of
    lots of simple things.

    "Tom Rowton" <tomrowtonhotmail.com> wrote in message
    news:0a9001c35832$a9db5a80$a501280aphx.gbl...
    > This one has me a bit confused and I'm not finding what I
    > need in the MSDN or by searching these forums, so here
    > goes...
    >
    >
    > I have a rather large, complex code-in-page WebForm
    > (don't ask) and a section of that Form is 4 or 5
    > ASP:Panels pretending to be a set of Tabs, each with its
    > own section of the form.
    >
    > In order to simplify managing this page, since code-
    > behind is not an option (don't ask, not my fault), I
    > thought I had the solution...
    >
    > I would convert each Panel into a User Control.
    > The problem is that the panels usually depend on at least
    > one Form Control from at least one other Panel.
    >
    > I already know about RaiseBubbleEvent() and OnBubbleEvent
    > () and have used them elsewhere, but they only work going
    > UP the container heirarchy.
    >
    > I need to :
    > 1 - (partially) handle an event in the UserControls
    > (panels), then (partially handle the same event in
    > ANOTHER UserControl(panel) on the same form/page.
    >
    > 2 - handle an event in a UserControl(panel) BUT be able
    > to retrieve values from form fields(Controls) in OTHER
    > UserControls(panels) to help determine how to handle the
    > event.
    >
    > 3 - (partially) handle an event in the Form(aspx)
    > depending on values of form controls in the UserControls
    >
    > 4 - manipulate values of form controls in the
    > UserControls
    >
    >
    > Like I said, I already know how to Bubble events, but
    > that only goes up.
    > The part I am missing is how to "Elbbub" events (bubble,
    > backwards) or manipulate data in child objects.
    >
    > The trick is, this is all Code-in-Page and nothing will
    > convince those in control to return to Code Behind.
    >
    > Anyone know how to do what I want/need to do?
    >
    > I have a set of aspx/ascx pages that I can email or ftp
    > for anyone interested.

    Kevin Spencer Guest

  3. #3

    Default Re: Manipulating a User Control from another User Control

    >Not sure what you mean by "codebehind is not an
    option."

    By "code-behind", I am referring to the "strict"
    definition of code-behind, i.e. a two-file page - *.as*x
    + *.as*x.vb.

    I am using single-file or code-in-page pages, similar to
    old-style ASP 3.0.

    It sounds like maybe you know how to do this with
    the "normal" MS-promoted code-behind setup, so if this is
    easily converted to a single-file, code-in-page page,
    please share the love. ;) I've found plenty of articles
    on how to get/set values in User Controls from a page
    that contains them using code-behind, but nothing about
    doing this from one user control to another with single-
    file pages.

    I've been banging on this for two days now and can't seem
    to retrieve info from one user control with code in
    another without Bubbling the event to the parent page
    (which works fine, but will only further complicate the
    code).
    Tom Rowton Guest

  4. #4

    Default Re: Manipulating a User Control from another User Control

    Hi Tom,

    I already DID "share the love," but apparently you didn't notice. Let me
    reproduce it for you:

    <% Page language="C#" %>
    <script runat="server" >
    public void Page_Load()
    {
    // Page_Load code goes here
    }
    </script>
    <html>
    ....Page Template HTML and Code Goes here
    </html>

    Note the server-side script at the top. All you need to do is put the code
    that would normally go in the CodeBehind class definition in a separate file
    inside the CodeBehind script at the top of the page. I've already added the
    Page_Load script as an example.

    You definitely DON'T want to try programming ASP./Net with the old ASP
    style. It's not designed for that, and will cause you beaucoups trouble if
    you try to work with this Object-Oriented technology in a procedural way.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    [url]http://www.takempis.com[/url]
    Complex things are made up of
    lots of simple things.

    "Tom Rowton" <tomrowtonhotmail.com> wrote in message
    news:001d01c35840$3a529df0$a301280aphx.gbl...
    > >Not sure what you mean by "codebehind is not an
    > option."
    >
    > By "code-behind", I am referring to the "strict"
    > definition of code-behind, i.e. a two-file page - *.as*x
    > + *.as*x.vb.
    >
    > I am using single-file or code-in-page pages, similar to
    > old-style ASP 3.0.
    >
    > It sounds like maybe you know how to do this with
    > the "normal" MS-promoted code-behind setup, so if this is
    > easily converted to a single-file, code-in-page page,
    > please share the love. ;) I've found plenty of articles
    > on how to get/set values in User Controls from a page
    > that contains them using code-behind, but nothing about
    > doing this from one user control to another with single-
    > file pages.
    >
    > I've been banging on this for two days now and can't seem
    > to retrieve info from one user control with code in
    > another without Bubbling the event to the parent page
    > (which works fine, but will only further complicate the
    > code).

    Kevin Spencer Guest

  5. #5

    Default Re: Manipulating a User Control from another User Control

    Hi Tom,

    Yes, post your sample code. I, for one, am having a hard time understanding
    the exact nature of the problem.

    I understand your confusion re my use of the term "CodeBehind." There are a
    number of different configurations for implementing what is often referred
    to as "CodeBehind code," and this has a tendancy to muddy the water when
    discussing how to implement the Class code that completes the Page Template
    code. Taken literally, "CodeBehind" does refer to a separate file. I (and
    some others) tend to use the term to talk about the "Code Behind the Page,"
    which, of course, can be in a separate file or in the same file as the Page
    Template (as we've been discussing). In terms of functionality and
    optimization, it makes very littel difference whether you use a separate
    file, DLLs, or just a single file with the Page Template and Page Class Code
    in it.

    The most important point, which you seem to understand, is that the code in
    the Page Template (between the <html> and </html> tags) have as little
    executable code in it as possible, and that you do your manipulation of the
    Page through the CodeBehind Class. And I wouldn't be embarrassed to say that
    I was including the "CodeBehind" code in the same file as the Page Template.
    That is perfectly legitimate. I believe that Microsoft is considering taking
    a similar approach in the future with the next version of ASP.Net. It
    doesn't necessarily make sense to have ALL of the Pages in a project
    compiled into a single DLL, especially when the first request for the page
    results in the code being compiled and cached.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    [url]http://www.takempis.com[/url]
    Complex things are made up of
    lots of simple things.


    "Tom Rowton" <tomrowtonhotmail.com> wrote in message
    news:0cdb01c35848$0261f690$a501280aphx.gbl...
    > >I already DID "share the love," but apparently you
    > Ahh, I had to view source, as I've viewing this via
    > msdn.microsoft.com, so your asp tags disappeared.
    >
    > What you describe is basically what I'm doing - code AND
    > html in the same file, as opposed to a separate code-
    > behind file with an HTML file.
    >
    > I have no say in how this thing is built, so I have to
    > work with it the way it is - single-file webform.
    >
    > I have an example that shows how I am failing to do what
    > I want, does it make sense to post them in my message?
    >
    >
    > Thanks for your help so far, I think I just was unclear
    > about meaning _separate_ code-behind files when I
    > said "code-behind".

    Kevin Spencer Guest

  6. #6

    Default Re: Manipulating a User Control from another User Control

    Okay, let me see if I understand you correctly. You've got an Event Handler
    for one UserControl that needs to do something with another UserControl,
    right? It looks to me like your error is in using Page.FindControl(...). The
    FindControl() method of a Control looks for controls in that Control's
    Controls Collection (was that a confusing sentence or what?). Your
    UserControls are not in the Page's Controls Collection; they are in your
    Form's Controls collection. Does that help?

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    [url]http://www.takempis.com[/url]
    Complex things are made up of
    lots of simple things.

    "Tom Rowton" <tomrowtonhotmail.com> wrote in message
    news:6bd801c3585b$7613c060$a001280aphx.gbl...
    > Ok, I hope this works. I'll just post entire files, and
    > separate them by a line of dashes.
    > MyCaseSub.ascx was trying to get the value of
    > MyCaseMain.ascx's label, but it wouldn't work. I could
    > get the ID of the Case:MainPanel usercontrol, but I can't
    > access the Property Method _UNLESS_ I bubble the event to
    > MyCase.aspx and let MyCase.aspx call the Get.
    >
    > Clearly, this is a matter of not understanding the object
    > model, but I can't seem to find anyone to tell me how to
    > do it without Bubbling, or I'm asking in an unclear
    > manner.
    >
    > TIA,
    > Tom
    >
    > MyCase.aspx
    > ------------------------
    > <% Register TagPrefix="Case" TagName="MainPanel"
    > Src="MyCaseMain.ascx" %>
    > <% Register TagPrefix="Case" TagName="SubPanel"
    > Src="MyCaseSub.ascx" %>
    > <% Register TagPrefix="Case" TagName="SubPanel2"
    > Src="MyCaseSub2.ascx" %>
    > <% Import Namespace = "System.Web.UI.WebControls" %>
    > <% Import Namespace = "System.Data.SqlTypes" %>
    > <% Import Namespace = "System.Data.SqlClient" %>
    > <% Import Namespace = "System.Drawing" %>
    > <% Import Namespace = "System.Data" %>
    > <% Import Namespace = "System.Web" %>
    > <% Import Namespace = "System" %>
    > <% Page Language="vb" Inherits="System.Web.UI.Page" %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
    > Transitional//EN">
    > <HTML>
    > <HEAD>
    > <title>User Controls Test Page</title>
    > <script language="VB" runat="server">
    >
    > Protected Overrides Function OnBubbleEvent(ByVal
    > source As Object, ByVal args As System.EventArgs) As
    > Boolean
    > If source.GetType.Equals (GetType
    > (Button)) Then
    > Dim oButton As Button = source
    > Select Case oButton.ID
    > Case "btnMainButton"
    > 'Change text of
    > Subsection Label
    >
    > SubPanel1.SubLabelValue = "Changed by Main Panel
    > via RaiseBubbleEvent()"
    > Case "btnSubButton"
    > 'Change text of
    > Main Label
    >
    > MainPanel1.MainLabelValue = "Changed by Sub Panel
    > via RaiseBubbleEvent()"
    > Case "btnGetMain"
    > dim temp as
    > string = MainPanel1.MainLabelValue
    >
    > SubPanel1.lblParentValue = temp
    > Case "btnSub2Button"
    > 'Change text of
    > Main Label and Subsection Label
    >
    > SubPanel1.SubLabelValue = "**Changed by Sub2
    > Panel via RaiseBubbleEvent()**"
    >
    > MainPanel1.MainLabelValue = "**Changed by Sub2
    > Panel via RaiseBubbleEvent()**"
    > End Select
    > End If
    > End Function
    >
    > sub btnReset_Click(sender as Object, e as
    > EventArgs)
    > SubPanel1.SubLabelValue = "Sub Panel"
    > SubPanelB1.Sub2LabelValue = "Sub2 Panel"
    > MainPanel1.MainLabelValue = "Main Panel"
    > end sub
    >
    > Sub CaseTabBtn_Click(sender As Object, e As
    > System.EventArgs)
    > ' CasePanel.Visible = True
    > ' AssignmentPanel.Visible = False
    > end sub
    > Sub AssignmentTabBtn_Click(sender As Object, e As
    > System.EventArgs)
    > ' CasePanel.Visible = False
    > ' AssignmentPanel.Visible = True
    > end sub
    > sub page_load(sender As Object, e As System.EventArgs)
    > if not page.ispostback
    > end if
    > end sub
    > </script>
    > <META content="True" name="vs_showGrid">
    > <META content="Microsoft Visual
    > Studio.NET 7.0" name="GENERATOR">
    > <META content="Visual Basic 7.0"
    > name="CODE_LANGUAGE">
    > <META content="JavaScript"
    > name="vs_defaultClientScript">
    > <META
    > content="http://schemas.microsoft.com/intellisense/ie5"
    > name="vs_targetSchema">
    > </HEAD>
    > <body runat=server>
    > <FORM id="Form1" method="post"
    > encType="multipart/form-data" runat="server">
    > <asp:literal id="litScript"
    > runat="server" EnableViewState="False"></asp:literal>
    > <asp:panel id="MainPanel"
    > runat="server" borderwidth="1" borderstyle="ridge"
    > backcolor="PaleGoldenrod" HorizontalAlign="Center">&nbsp;
    > <CASE:MainPanel
    > id="MainPanel1" runat="server"
    > NAME="MainPanel1"></CASE:MainPanel>
    > </asp:panel>
    > <P></P>
    >
    > <asp:linkbutton id="CaseTabBtn"
    > onclick="CaseTabBtn_Click" runat="server"
    > BackColor="White" BorderWidth="1"
    > BorderColor="Goldenrod">Case</asp:linkbutton>&nbsp;&nbsp;
    > <asp:linkbutton
    > id="AssignmentTabBtn" onclick="AssignmentTabBtn_Click"
    > runat="server" BackColor="White" BorderWidth="1"
    > BorderColor="Goldenrod">Assignment</asp:linkbutton>&nbsp;&
    > nbsp;
    >
    > <asp:panel id="CasePanel"
    > runat="server" borderwidth="1" borderstyle="ridge"
    > backcolor="PaleGoldenrod" HorizontalAlign="Center">&nbsp;
    > <CASE:SubPanel
    > id="SubPanel1" runat="server"
    > NAME="SubPanel1"></CASE:SubPanel>
    > </asp:panel>
    >
    > <asp:panel id="AssignmentPanel"
    > runat="server" borderwidth="1" borderstyle="ridge"
    > backcolor="PaleGoldenrod" HorizontalAlign="Center">&nbsp;
    > <CASE:SubPanel2
    > id="SubPanelB1" runat="server"
    > NAME="SubPanelB1"></CASE:SubPanel2>
    > </asp:panel>
    > <asp:button id="btnReset"
    > onclick="btnReset_Click" runat="server" Text="Reset all
    > Panels"></asp:button>
    > </FORM>
    > </BODY>
    > </HTML>
    >
    > ----------------------
    > MyCaseMain.ascx
    > ----------------------
    > <% Control Language="vb" AutoEventWireup="false"
    > Inherits="System.Web.UI.UserControl"%>
    > <% Import Namespace = "System.Web.UI.WebControls" %>
    > <% Import Namespace = "System.Data.SqlClient" %>
    > <% Import Namespace = "System.Drawing" %>
    > <% Import Namespace = "System.Data" %>
    > <% Import Namespace = "System.Web" %>
    > <% Import Namespace = "System" %>
    > <script language="VB" runat="server">
    > Property MainLabelValue() as String
    > Get
    > Return lblMainLabel.Text
    > end Get
    > Set
    > lblMainLabel.Text = value
    > end Set
    > end Property
    > sub btnMainButton_Click(sender as Object, e as
    > EventArgs)
    > RaiseBubbleEvent(sender, e)
    > end sub
    > </script>
    > <hr>
    > Main Section<BR>
    > <asp:Label id="lblMainLabel" runat="server"
    > Visible="True">Main Label(MyCaseMain.ASCX)
    > </asp:Label><BR><BR>
    > <asp:button id="btnMainButton"
    > onclick="btnMainButton_Click" runat="server" Text="Change
    > Subsection Panel"></asp:button>
    > <hr>
    >
    > -----------------
    > MyCaseSub.ascx
    > -----------------
    >
    > <% Control Language="vb" AutoEventWireup="false" %>
    > <% Import Namespace = "System.Web.UI.WebControls" %>
    > <% Import Namespace = "System.Data.SqlClient" %>
    > <% Import Namespace = "System.Drawing" %>
    > <% Import Namespace = "System.Data" %>
    > <% Import Namespace = "System.Web" %>
    > <% Import Namespace = "System" %>
    > <script language="VB" runat="server">
    > Property SubLabelValue() as String
    > Get
    > Return lblSubLabel.Text
    > end Get
    > Set
    > lblSubLabel.Text = value
    > end Set
    > end Property
    > Property lblParentValue() as String
    > Get
    > Return lblParent.Text
    > end Get
    > Set
    > lblParent.Text = value
    > end Set
    > end Property
    > sub btnSubButton_Click(sender as Object, e as
    > EventArgs)
    > RaiseBubbleEvent(sender, e)
    > end sub
    > sub btnGetMain_Click(sender as Object, e as
    > EventArgs)
    > RaiseBubbleEvent (sender, e)
    > ' Dim MyControl as Control =
    > Page.FindControl("MainPanel1")
    > ' if (not MyControl is Nothing) then
    > ' lblParent.Text =
    > MyControl.MainLabelValue()
    > ' else
    > ' lblParent.Text = "MainPanel1 not
    > found"
    > ' end if
    > end sub
    > </script>
    > <hr>
    > Sub Section<BR>
    > <asp:Label id="lblSubLabel" runat="server"
    > Visible="True">Sub Label(MyCaseSub.ASCX)</asp:Label><BR>
    > Main Panel's ID:<asp:Label id="lblParent"
    > runat="server" Visible="True"></asp:Label><BR>
    > <asp:button id="btnSubButton"
    > onclick="btnSubButton_Click" runat="server" Text="Change
    > Main section Panel"></asp:button>
    > <asp:button id="btnGetMain"
    > onclick="btnGetMain_Click" runat="server" Text="Get Main
    > Panel label"></asp:button>
    > <hr>
    >
    > --------------

    Kevin Spencer Guest

  7. #7

    Default Re: Manipulating a User Control from another User Control

    >Form's Controls collection. Does that help?
    It's good info to have, but doesn't really fix my
    problem.

    in MyCaseSub.ascx where I have commented out
    ---------------------------------------------
    Dim MyControl as Control =
    Page.FindControl("MainPanel1")
    if (not MyControl is Nothing) then
    lblParent.Text =
    MyControl.MainLabelValue()
    else
    lblParent.Text = "MainPanel1 not
    found"
    end if
    ------------------------------------------------

    The "else" never happens.
    MyControl is _never_ Nothing.
    I can print the ID out just fine.
    But I am unable to access the "MainLabelValue()" Property
    GET - which works just fine from the bubbled event in
    MyCase.aspx.

    To sum up, I know that Page.FindControl() finds the
    usercontrol, because I can print out the ID I've given it
    in the HTML, but I cannot access the Property I've given
    in the script block of the UserControl.

    Or did I just misunderstand your point?

    Tom Rowton Guest

Similar Threads

  1. user control problem access value from user control to a page
    By gauravkg via DotNetMonster.com in forum ASP.NET Building Controls
    Replies: 3
    Last Post: September 18th, 01:06 AM
  2. Replies: 0
    Last Post: April 6th, 11:13 PM
  3. Dynamically Adding User Control with Child User Control
    By Travis Pruitt in forum ASP.NET Building Controls
    Replies: 0
    Last Post: January 13th, 08:49 PM
  4. Replies: 5
    Last Post: February 24th, 02:07 PM
  5. Page Load fired 3 times Web user control is embedded in a custom control
    By Thomas Ekegren in forum ASP.NET Building Controls
    Replies: 0
    Last Post: September 3rd, 05:37 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139