473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

user control problem

Hi,

been a while since I posted a question myself instead of trying to help
others out.

I'm refactoring an existing web app that uses dynamic loading of user
controls and a lot of response.redirects to the same page. Because I hate
the overhead by doing this I'm searching for a cleaner option.

But I'm having troubles (off course or I wouldn't be posting this).

The code itself looks ok but when I do a little testing I don't get the
results as I would expect:
test scenario:

- load the page and the first uc will appear, I can type something in the
textbox and clicking the "First button" I get my textbox entry in the label.
Still good.
- when hitting the button "Load other control" I can clearly see that
AttachUC gets called the first time and loads in the first uc and goes to
the buttonevent on the FirstUC. The session gets updated in the method
Button2_Click of FirstUC and the AttachUC is called again. _dyn.ControlName
is "SecondUC.ascx" so that is still good. But when the page renders I still
get to see FirstUC on the page.

- After clicking the "Load other control" for the second time the correct
control gets loaded but I see that the text of the TextBox and Label of
FirstUC is set to the textbox and label of SecondUC.ascx.

Well, if someone knows a good explanation for this problem you can always
reply to this post. I'll be also looking at the problem.

This is my code so far:

default.aspx:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb"
Inherits="dynamiccontrolsloadingandunloading._defa ult" trace="True"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<title>default</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>

<form id="Form1" method="post" runat="server">

<P>

<asp:Label id="LabelErrorMessage" runat="server"></asp:Label></P>

<P>

<asp:PlaceHolder id="PlaceHolderTest"
runat="server"></asp:PlaceHolder></P>

</form>

</body>

</HTML>

----------------------------------------------------------------------------
-----------------------

default.aspx.vb:

Public Class _default

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Protected WithEvents PlaceHolderTest As
System.Web.UI.WebControls.PlaceHolder

Protected WithEvents LabelErrorMessage As
System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private _dyn As DynaKeeper

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

'Put user code to initialize the page here

If Not Page.IsPostBack Then

Session("dynakeep") = New DynaKeeper("PlaceHolderTest",
"FirstUC.ascx", AddressOf Me.AttachUC)

End If

_dyn = DirectCast(Session("dynakeep"), DynaKeeper)

LabelErrorMessage.Text = _dyn.ControlName + " - " +
_dyn.PlaceHolderName + " - " + _dyn.CallBackMethod.ToString

AttachUC()

End Sub

Private Sub AttachUC()

Try

If Not IsNothing(Session("dynakeep")) Then

Dim holder As PlaceHolder =
DirectCast(Me.FindControl(_dyn.PlaceHolderName), PlaceHolder)

holder.Controls.Clear()

holder.Controls.Add(LoadControl(_dyn.ControlName))

End If

Catch ex As Exception

LabelErrorMessage.Text = ex.Message

End Try

End Sub

End Class

----------------------------------------------------------------------------
-----------------------

FirstUC.ascx:

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="FirstUC.ascx.vb"

Inherits="dynamiccontrolsloadingandunloading.First UC"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

<P>

<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

<asp:Button id="Button1" runat="server" Text="First Go"></asp:Button>

<asp:Label id="Label1" runat="server"></asp:Label></P>

<P>

<asp:Button id="Button2" runat="server" Text="Load other
control"></asp:Button></P>

----------------------------------------------------------------------------
-----------------------

FirstUC.ascx.vb:

Public Class FirstUC

Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Protected WithEvents Button1 As System.Web.UI.WebControls.Button

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Protected WithEvents Button2 As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

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

'Put user code to initialize the page here

If Not Page.IsPostBack Then

Label1.Text = "First user control loaded"

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Label1.Text = TextBox1.Text.Trim

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

If Not IsNothing(Session("dynakeep")) Then

Dim dyn As DynaKeeper = DirectCast(Session("dynakeep"),
DynaKeeper)

dyn.ControlName = "SecondUC.ascx"

dyn.CallBackMethod.Invoke()

End If

End Sub

End Class

----------------------------------------------------------------------------
-----------------------

Second.ascx:

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="SecondUC.ascx.vb"

Inherits="dynamiccontrolsloadingandunloading.Secon dUC"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

<P>

<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>

<P>&nbsp;</P>

<P>

<asp:Button id="Button1" runat="server" Text="Second Go"></asp:Button></P>

<P>

<asp:Label id="Label1" runat="server"></asp:Label></P>

----------------------------------------------------------------------------
-----------------------

SecondUC.ascx.vb:

Public Class SecondUC

Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Protected WithEvents Button1 As System.Web.UI.WebControls.Button

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

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

'Put user code to initialize the page here

If Not Page.IsPostBack Then

Label1.Text = "Second user control loaded"

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Label1.Text = TextBox1.Text.Trim

End Sub

End Class

----------------------------------------------------------------------------
-----------------------

DynaKeeper.vb:

Public Class DynaKeeper

Private _placeHolderName As String

Private _controlName As String

Private _callBack As CallBackMethodDelegate

Public Delegate Sub CallBackMethodDelegate()

Public Sub New(ByVal placeHolderName As String, ByVal controlName As
String, ByVal callBack As CallBackMethodDelegate)

_placeHolderName = placeHolderName

_controlName = controlName

_callBack = callBack

End Sub

Public Property PlaceHolderName() As String

Get

Return _placeHolderName

End Get

Set(ByVal Value As String)

_placeHolderName = Value

End Set

End Property

Public Property ControlName() As String

Get

Return _controlName

End Get

Set(ByVal Value As String)

_controlName = Value

End Set

End Property

Public Property CallBackMethod() As CallBackMethodDelegate

Get

Return _callBack

End Get

Set(ByVal Value As CallBackMethodDelegate)

_callBack = Value

End Set

End Property

End Class



Nov 18 '05 #1
1 2255
I am not sure of the exact details of what the code is doing, but
something popped into my head about embedded usercontrols not being 100%
accessible unless they were set up in the Page_Init event. I've been
burned by that before.

Maybe if you moved your control loading in there it would work.

I hope that puts you on the right track.

"Kris van der Mast" <kr*************@skynet.be> wrote in
news:#V**************@TK2MSFTNGP12.phx.gbl:
Hi,

been a while since I posted a question myself instead of trying to
help others out.

I'm refactoring an existing web app that uses dynamic loading of user
controls and a lot of response.redirects to the same page. Because I
hate the overhead by doing this I'm searching for a cleaner option.

But I'm having troubles (off course or I wouldn't be posting this).

The code itself looks ok but when I do a little testing I don't get
the results as I would expect:
test scenario:

- when hitting the button "Load other control" I can clearly see that
AttachUC gets called the first time and loads in the first uc and goes
to the buttonevent on the FirstUC. The session gets updated in the
method Button2_Click of FirstUC and the AttachUC is called again.
_dyn.ControlName is "SecondUC.ascx" so that is still good. But when
the page renders I still get to see FirstUC on the page.

- After clicking the "Load other control" for the second time the
correct control gets loaded but I see that the text of the TextBox and
Label of FirstUC is set to the textbox and label of SecondUC.ascx.

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Earl Teigrob | last post by:
Background: When I create a ASP.NET control (User or custom), it often requires security to be set for certain functionality with the control. For example, a news release user control that is...
2
by: Sam Kuehn | last post by:
There has been a lot of articles on how to load user controls at runtime in the Init() method. UserControl myControl = (UserControl)LoadControl(stringControl); I add the control in the Init()...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
4
by: thomson | last post by:
Hi all, i do have a user control with 4 buttons, and all the events are firing properly, My problem is that i need to right an event handler in the user control, which gets fired after a...
8
by: David Lozzi | last post by:
Howdy, I have a user control that is a report to display data. On the page the control is inserted in, I have filter options to filter the report. When I try to do something like this, nothing...
5
by: Segfahlt | last post by:
I need a little help here please. I have 2 win forms user controls in 2 different projects that I'm hosting in 2 different virtual directories. The controls have been test and operate okay in...
4
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
5
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
5
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
3
by: jonathan.beckett | last post by:
I have been experimenting with placing user controls (using Windows Forms controls) into an ASP.NET webpage - and am completely stuck. I can get a blank user control to work, but nothing beyond...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.