473,748 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.redire cts 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.ControlNam e
is "SecondUC.a scx" 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="def ault.aspx.vb"
Inherits="dynam iccontrolsloadi ngandunloading. _default" trace="True"%>

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

<HTML>

<HEAD>

<title>defaul t</title>

<meta name="GENERATOR " content="Micros oft Visual Studio .NET 7.1">

<meta name="CODE_LANG UAGE" content="Visual Basic .NET 7.1">

<meta name="vs_defaul tClientScript" content="JavaSc ript">

<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">

</HEAD>

<body>

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

<P>

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

<P>

<asp:PlaceHolde r id="PlaceHolder Test"
runat="server"> </asp:PlaceHolder ></P>

</form>

</body>

</HTML>

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

default.aspx.vb :

Public Class _default

Inherits System.Web.UI.P age

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub

Protected WithEvents PlaceHolderTest As
System.Web.UI.W ebControls.Plac eHolder

Protected WithEvents LabelErrorMessa ge As
System.Web.UI.W ebControls.Labe l

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

'Do not delete or move it.

Private designerPlaceho lderDeclaration As System.Object

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

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

'Do not modify it using the code editor.

InitializeCompo nent()

End Sub

#End Region

Private _dyn As DynaKeeper

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

'Put user code to initialize the page here

If Not Page.IsPostBack Then

Session("dynake ep") = New DynaKeeper("Pla ceHolderTest",
"FirstUC.as cx", AddressOf Me.AttachUC)

End If

_dyn = DirectCast(Sess ion("dynakeep") , DynaKeeper)

LabelErrorMessa ge.Text = _dyn.ControlNam e + " - " +
_dyn.PlaceHolde rName + " - " + _dyn.CallBackMe thod.ToString

AttachUC()

End Sub

Private Sub AttachUC()

Try

If Not IsNothing(Sessi on("dynakeep") ) Then

Dim holder As PlaceHolder =
DirectCast(Me.F indControl(_dyn .PlaceHolderNam e), PlaceHolder)

holder.Controls .Clear()

holder.Controls .Add(LoadContro l(_dyn.ControlN ame))

End If

Catch ex As Exception

LabelErrorMessa ge.Text = ex.Message

End Try

End Sub

End Class

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

FirstUC.ascx:

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

Inherits="dynam iccontrolsloadi ngandunloading. FirstUC"
TargetSchema="h ttp://schemas.microso ft.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.U serControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub

Protected WithEvents TextBox1 As System.Web.UI.W ebControls.Text Box

Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on

Protected WithEvents Label1 As System.Web.UI.W ebControls.Labe l

Protected WithEvents Button2 As System.Web.UI.W ebControls.Butt on

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

'Do not delete or move it.

Private designerPlaceho lderDeclaration As System.Object

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

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

'Do not modify it using the code editor.

InitializeCompo nent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Label1.Text = TextBox1.Text.T rim

End Sub

Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click

If Not IsNothing(Sessi on("dynakeep") ) Then

Dim dyn As DynaKeeper = DirectCast(Sess ion("dynakeep") ,
DynaKeeper)

dyn.ControlName = "SecondUC.a scx"

dyn.CallBackMet hod.Invoke()

End If

End Sub

End Class

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

Second.ascx:

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

Inherits="dynam iccontrolsloadi ngandunloading. SecondUC"
TargetSchema="h ttp://schemas.microso ft.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.v b:

Public Class SecondUC

Inherits System.Web.UI.U serControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

End Sub

Protected WithEvents TextBox1 As System.Web.UI.W ebControls.Text Box

Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on

Protected WithEvents Label1 As System.Web.UI.W ebControls.Labe l

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

'Do not delete or move it.

Private designerPlaceho lderDeclaration As System.Object

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

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

'Do not modify it using the code editor.

InitializeCompo nent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Label1.Text = TextBox1.Text.T rim

End Sub

End Class

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

DynaKeeper.vb:

Public Class DynaKeeper

Private _placeHolderNam e As String

Private _controlName As String

Private _callBack As CallBackMethodD elegate

Public Delegate Sub CallBackMethodD elegate()

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

_placeHolderNam e = placeHolderName

_controlName = controlName

_callBack = callBack

End Sub

Public Property PlaceHolderName () As String

Get

Return _placeHolderNam e

End Get

Set(ByVal Value As String)

_placeHolderNam e = 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 CallBackMethodD elegate

Get

Return _callBack

End Get

Set(ByVal Value As CallBackMethodD elegate)

_callBack = Value

End Set

End Property

End Class



Nov 18 '05 #1
1 2323
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******** ******@TK2MSFTN GP12.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.redire cts 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.ControlNam e is "SecondUC.a scx" 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
3387
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 comprised of a DataGrid may have separate permissions for adding, deleting and updating a news item. Problem Up until now, I have been implementing security directly inside the control. I will test directly against the security model to see if...
2
2038
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() method so that the viewstate to works properly. What if I want to let the user select what control to display? For instance say I have a drop down list of user controls and want to load the control the user has selected? When the user selects a control...
6
3380
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 the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
4
1754
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 specific process is done,, but the form which will host the user control has to specify what has to be done Something like this , if the event is fired it should call the event in
8
2265
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 happens. dim filt as string ... build filter string... UserControl.ReportFilter = filt
5
4094
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 both projects. Both controls(dlls) have been signed using SN.exe and I've set up the appropriate .Net assembly permissions using those Strong Names The DLL's have been copied to the /bin directory in both web virtual directories.
4
1688
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 that build a class library dll. Here we have a class called C We have one dependency and that is from the user control to the class library because in the constructor for class B in the user control we have a call to
5
1944
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 that build a class library dll. Here we have a class called C We have one dependency and that is from the user control to the class library because in the constructor for class B in the user control we have a call to
5
1996
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 like this is a bug or that .NET doesn't support what I trying to do. I hope that one that is is microsoft certified read this because this must be a bug.
3
2089
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 that. I have tried making a very basic test rig to isolate the problem, and have illustrated it below. For reference, this is using .NET 2 and Visual Studio 2005. Here's what I did - the first few steps explain what worked, then what I did that...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.