473,394 Members | 1,693 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,394 software developers and data experts.

Problem with webcontrol and events

Hi guys

I have a webcontrol that displays an article in multiple parts. You can jump back and forward between the parts (I call them sheets) by clicking on a label. Each sheet has a label.

The control is used like this:

Expand|Select|Wrap|Line Numbers
  1. <john:MyControl runat="server">
  2.     <john:Sheet label="Intro">
  3.         This is the first part of the text...
  4.     </john:Sheet>
  5.     <john:Sheet label="Maintext">
  6.         This is the second part of the text...
  7.     </john:Sheet>
  8. </john:MyControl>
And the control displays the contents of the selected sheet.

This all works great. The problem is when you put a button inside a sheet, and wire the button to an event. This is were it starts to go wrong. It seems that the first time the control is loaded (before the user has jumped to a different sheet), the button works and the event is called. But if the user jumps to a different sheet and then jump back, the button will not work. At least not the first time you click it. If you click it twice, the event gets called the second time.

This is the code:

*** *** *** ***
---
MyControl.cs
---
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.ComponentModel;
  3. using System.Collections;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Web.UI.HtmlControls;
  8.  
  9. namespace John {
  10.     [DefaultProperty("Sheets"),
  11.     ParseChildren(true, "Sheets")]
  12.     public class MyControl : WebControl {
  13.         private ArrayList _sheets;
  14.         private Sheet _selectedSheet = null;
  15.         private HtmlGenericControl rootDiv;
  16.         private HtmlGenericControl _sheetControl;
  17.  
  18.         private Sheet SelectedSheet {
  19.             get {
  20.                 if (_selectedSheet == null) {
  21.                     if (Sheets.Count > 0)
  22.                         return (Sheet)Sheets[0];
  23.                     else
  24.                         return null;
  25.                 }
  26.                 else
  27.                     return _selectedSheet;
  28.             }
  29.             set {
  30.                 _selectedSheet = value;
  31.             }
  32.         }
  33.  
  34.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  35.         PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  36.         public ArrayList Sheets {
  37.             get {
  38.                 if (_sheets == null) {
  39.                     _sheets = new ArrayList();
  40.                 }
  41.                 return _sheets;
  42.             }
  43.         }
  44.  
  45.         protected override void OnInit(EventArgs e) {
  46.             rootDiv = new HtmlGenericControl("div");
  47.             HtmlGenericControl labelsDiv = constructLabels();
  48.             HtmlGenericControl sheet = contructSheet();
  49.  
  50.             rootDiv.Controls.Add(labelsDiv);
  51.             rootDiv.Controls.Add(sheet);
  52.  
  53.             this.Controls.Add(rootDiv);
  54.  
  55.             this.EnableViewState = false;
  56.             base.OnInit(e);
  57.         }
  58.  
  59.         protected override void OnPreRender(EventArgs e) {
  60.             SelectedSheet.LabelControl.Enabled = false;
  61.         }
  62.  
  63.         protected void Label_Click(object sender, CommandEventArgs e) {
  64.             int sheetIndex = int.Parse(e.CommandArgument.ToString());
  65.  
  66.             SelectedSheet = (Sheet)Sheets[sheetIndex];
  67.  
  68.             _sheetControl.Controls.Clear();
  69.             SelectedSheet.Content.InstantiateIn(_sheetControl);
  70.         }
  71.  
  72.         protected override void Render(HtmlTextWriter writer) {
  73.             rootDiv.RenderControl(writer);
  74.         }
  75.  
  76.         private HtmlGenericControl contructSheet() {
  77.             _sheetControl = new HtmlGenericControl("div");
  78.  
  79.             SelectedSheet.Content.InstantiateIn(_sheetControl);
  80.  
  81.             return _sheetControl;
  82.         }
  83.  
  84.         private HtmlGenericControl constructLabels() {
  85.             HtmlGenericControl div = new HtmlGenericControl("div");
  86.             int counter = 0;
  87.  
  88.             foreach (Sheet sheet in Sheets) {
  89.                 counter++;
  90.  
  91.                 LinkButton button = new LinkButton();
  92.  
  93.                 button.Text = sheet.Label;
  94.                 button.ID = "btn" + (counter - 1).ToString();
  95.  
  96.                 button.Command += new CommandEventHandler(Label_Click);
  97.                 button.CommandArgument = (counter - 1).ToString();
  98.                 button.CommandName = "Click";
  99.  
  100.                 sheet.LabelControl = button;
  101.  
  102.                 div.Controls.Add(button);
  103.                 div.Controls.Add(new HtmlGenericControl("br"));
  104.             }
  105.  
  106.             div.Controls.Add(new HtmlGenericControl("hr"));
  107.  
  108.             return div;
  109.         }
  110.     }
  111.  
  112.     [TypeConverter(typeof(ExpandableObjectConverter)),
  113.     ParseChildren(true, "Content")]
  114.     public class Sheet {
  115.         private string _label;
  116.         private ITemplate _content = null;
  117.         public LinkButton LabelControl;
  118.  
  119.         public Sheet()
  120.             : this(String.Empty) {
  121.  
  122.         }
  123.  
  124.         public Sheet(string label) {
  125.             _label = label;
  126.         }
  127.  
  128.         [DefaultValue(""),
  129.         NotifyParentProperty(true)]
  130.         public String Label {
  131.             get { return _label; }
  132.             set { _label = value; }
  133.         }
  134.  
  135.         [DefaultValue(""),
  136.         NotifyParentProperty(true),
  137.         PersistenceMode(PersistenceMode.InnerDefaultProperty),
  138.         TemplateContainer(typeof(INamingContainer))]
  139.         public ITemplate Content {
  140.             get { return _content; }
  141.             set { _content = value; }
  142.         }
  143.     }
  144. }
*** *** *** ***
---
MyPage.aspx
---
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>
  2. <%@ Register Namespace="John" TagPrefix="john" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body id="MyPageBody" runat="server" style="background-color: cyan;">
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <john:MyControl runat="server">
  13.             <john:Sheet label="Intro">
  14.                 This is the first part of the text.
  15.                 This is the first part of the text.
  16.                 This is the first part of the text.<br />
  17.                 <asp:Button runat="server" Text="Change page background" OnCommand="ChangePageBackground" />
  18.             </john:Sheet>
  19.             <john:Sheet label="Maintext">
  20.                 This is the second part of the text.
  21.                 This is the second part of the text.
  22.                 This is the second part of the text.<br />
  23.                 <asp:Button runat="server" Text="Change page background" OnCommand="ChangePageBackground" />
  24.             </john:Sheet>
  25.         </john:MyControl>
  26.     </div>
  27.     </form>
  28. </body>
  29. </html>
*** *** *** ***
---
MyPage.aspx.cs
---
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class MyPage : System.Web.UI.Page {
  13.     protected void ChangePageBackground(object sender, CommandEventArgs e) {
  14.         if (MyPageBody.Style["background-color"] == "cyan")
  15.             MyPageBody.Style["background-color"] = "magenta";
  16.         else
  17.             MyPageBody.Style["background-color"] = "cyan";
  18.     }
  19. }
Jul 5 '07 #1
0 1141

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jim M | last post by:
Is there any way to know what node was clicked in a treeview webcontrol where autopostback is false? Thanks in advance.
0
by: Lucas, Todd | last post by:
Hello everyone! I'm having a problem with a WebControl that I'm designing for a Menu. I've been at it for about 3 weeks now, and can't seem to get around this problem. So I'm hoping that someone...
3
by: J'son | last post by:
Guys, I have created a custom class that derives from DataList so that I can add some custom client side functionality into each new item row (<td>). Heres the class in its simplest form: ...
0
by: Demetri | last post by:
I have created a web control that can be rendered as either a linkbutton or a button. It is a ConfirmButton control that allows a developer to force a user to confirm if they intended to click it...
1
by: Sam | last post by:
Hi all, Why can't i create a validatorControl as a child of the control to validate? It doesn't give any errors, but it just won't work (it's not validating). The reason why i want this is...
3
by: Bob | last post by:
Hi, I 'm starting asp.net and i saw two ways to make buttons. Which way to use? Are the htmlbuttons in specific case better than webbuttons? Are there advantages? Thanks Bob htmlbutton:...
2
by: Peter Rilling | last post by:
Okay, I something weird is happening where I do not know if I am doing something wrong. I have a page that contains a custom webcontrol that I developed. This webcontrol basically loads a...
5
by: ThunderMusic | last post by:
Hi, I always refer to this page to know the order of events in a page : http://weblogs.asp.net/jeff/archive/2004/07/04/172683.aspx but this time, I'm mystified... I have a Control called...
1
by: Lloyd Sheen | last post by:
I add a webcontrol to a form. Then go to events. There is just a short list of events shown in the properties window of which (StatusTextChanged) is not one of them. So them I go to the code...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...

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.