Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 2nd, 2003, 04:57 AM
Registered User
 
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Programmatically Loaded User Control Event Handlin

Hello all.
I have a basic navigation system for one part of a website.
Along the left is a series of linkButtons and the right gets loaded with a different user control depending on which link is clicked.
The basic user controls (ie just displaying images and text) work fine.
However, one link is for "Support" and the corresponding user control has a textbox for email, question and then a submit button which logs the support ticket in a database.
The problem is that when the submit button is clicked, its event handler (btnSubmit_Click) is not being executed.
No errors are raised, but if btnSubmit_Click (s As Obj...) is absent, the compiler raises hell even though it won't execute it when its present.

Has anyone else come across this oddity?
Thank you.

Calvin
 
Old December 2nd, 2003, 01:22 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

how are you adding the control to the page? Weird things can happen when you dynamically add controls to a page. The event handlers and viewstates have a tendency to behave strangely.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 2nd, 2003, 04:48 PM
Registered User
 
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's the "Loader" for the Support ticket user control.
The DirectCast call was an effort to try to force the page to realize the type of the control being loaded and thus handle its events; it didn't work...

<%@ Reference Control="contactUsPage.ascx" %>
<script language="vb" runat="Server">
    Protected Sub btnContactUs_Click (s As Object, e As EventArgs)
        Dim ctrl As Control

        ctrl = LoadControl ("contactUsPage.ascx")
        ctrl = DirectCast (ctrl, contactUsPage)
        plhMain.Controls.Clear ()
        plhMain.Controls.Add (ctrl)
    End Sub
</script>

And the control itself (note I've removed any database stuff to try to simplify):

<%@ Control ClassName="contactUsPage" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<script Language="VB" Runat="Server">
    Protected Sub btnSendQuestion_Click (s As Object, e As EventArgs)
        Response.redirect ("thankyou.aspx")
    End Sub
</script>
<table align=center border=0 width=100%>
<tr><td>
<table cellpadding=3>
<tr><td valign=top><img src="images/wine_web_logo.gif">
<td>&nbsp;&nbsp;&nbsp;<img src="images/title_contactus.gif">
</tr>
</table>
</tr>
<tr><td>
<table width=100%>
<tr><td align=center colspan=2>Please fill out the following form</tr>
<tr><td align=right>Email Address: <td><asp:TextBox id="txtEmail" Size="40" MaxLength="100" Runat="Server" />
    &nbsp;<asp:RequiredFieldValidator ControlToValidate="txtEmail" Display="Dynamic" Text="Required" Runat="Server" />
</tr>
<tr><td align=right>Question/Comment: <td><asp:TextBox id="txtQuestion" Rows="8" Columns="40" TextMode="MultiLine" Runat="Server" />
    &nbsp;<asp:RequiredFieldValidator ControlToValidate="txtQuestion" Display="Dynamic" Text="Required" Runat="Server" />
</tr>
<tr><td colspan=2 align=center><asp:Button id="btnSendQuestion" class="btn" OnClick="btnSendQuestion_Click" Text="Send" Runat="Server" /></tr>
</table>
</tr>
</table>
<br><br>
 
Old December 2nd, 2003, 05:38 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well, it would seem to me that the only time you'll see the ASCX is when you have clicked on the btnContactUs button.

Here's a rule of thumb I use regarding dynamically loaded controls: Load everything you *might* need, and shut if off/disable it if you don't need it.

The rule is kind of counter-intuitive, but that seems to be the way ASP.net wants things to work. What you have going on from what I can see is that the ASCX will ONLY be part of the page on the one postback after you click the btnContactUs button. So on the next postback, the control's no longer there, because the btnContactUs button wasn't pressed and therefore the control's event can't fire.

Probably what you want to do is to put the user control on the page, but set it's visible attribute to false. Then, the btnContactUs handler set's it to true. The state of it will be maintained thru the viewstate so on subsequent postbacks the control doesn't dissappear again. Now, because the control is always there, all it's event will happen as expected.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 2nd, 2003, 09:59 PM
Registered User
 
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter - great advice. Thanks.
Loading all the controls and then toggling visibility works great.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Event Handling Programmatically Wallhood Excel VBA 2 August 30th, 2007 10:42 AM
How to raise event in user control webpart... tqit ASP.NET 2.0 Basics 1 September 13th, 2006 04:37 AM
User Control with my event ALGNET .NET Framework 2.0 1 May 2nd, 2006 05:56 PM
Create SQL Server user programmatically? Dmitriy Pro VB Databases 1 March 8th, 2006 10:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.